First of all, could you please describe what "is not working"? For instance, explaining "my program cannot be compiled; the compiler outputs the following error message: ..." or "my program runs, but has behavior X when I expected behavior Y" would be very helpful.
I expect the line
connect(ui->comboBox,&QComboBox::activated,ui->label,&QLabel::setLabelValue);
connect(ui->comboBox,&QComboBox::activated,ui->label,&QLabel::setLabelValue);
To copy to clipboard, switch view to plain text mode
to produce a compilation error. Is that right? I can see several problems:
- The reference to QComboBox::activated is ambiguous, because the signal is overloaded; you can use static_cast to specify a type and select the right overload.
- There is no QLabel::setLabelValue method. The method is in your own MainWindow class.
- The object whose slot is connected should be the MainWindow instance, not the QLabel ui->label.
You could fix this line and go through with your original plan (connect the combobox' activated signal to a slot in the main window that sets the label's text), but you can do something simpler: connect the combobox' QComboBox::activated(const QString &) signal to the label's setText(const QString &) slot. The signal will directly pass the text of the selected item to the slot setting the label's text. This should look something like:
connect(ui
->comboBox, static_cast<void
(QComboBox::*)(const QString &)>
(&QComboBox
::activated), ui
->label,
&QLabel
::setText);
connect(ui->comboBox, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), ui->label, &QLabel::setText);
To copy to clipboard, switch view to plain text mode
(I have not tested or even compiled this code, it may well be broken). Notice the use of static_cast to disambiguate between QComboBox::activated(const QString &) and QComboBox::activated(int).
Bookmarks