R3J3CT R3J3CT - 2 months ago 28
C++ Question

Pass UI in parameter in Qt

I have an windows ui in a Qt project and I want pass this ui in parameter to another function, like that:

ConfWindows::ConfWindows(QWidget *parent) :
QDialog(parent),
ui(new Ui::ConfWindows)
{
ui->setupUi(this);

connect(ui->add_button, SIGNAL(clicked()), this, SLOT(add_elem(ui->name_edit)));
}

void add_elem(QLabel test)
{
qDebug() << test.text();
}


But I get an error when I try to do that:

'QLabel::QLabel(const QLabel&)' is private
Q_DISABLE_COPY(QLabel)


It's impossible to pass an ui element in parameters ?

Thank for you help.

Answer

First of all it is not a good practice to pass your ui as a parameter. Secondly you should have can not have more parameters in a slot's function prototype than your signal has.

I should do it like this: I would make add_elem function, a member function of ConfWindows class, in order to have access to ui inside it's body and get the text of the label.

ConfWindows::ConfWindows(QWidget *parent) :
QDialog(parent), ui(new Ui::ConfWindows) {
    ui->setupUi(this);

    connect(ui->add_button, SIGNAL(clicked()), this, SLOT(add_elem()));
}

void ConfWindows::add_elem() {
    QString text = ui->label->text();
    qDebug() << test.text();
}