Results 1 to 4 of 4

Thread: Singal and Slot related simple problem!!!

  1. #1
    Join Date
    Sep 2015
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Singal and Slot related simple problem!!!

    Hello to All,

    I just have started to learn Qt with a very little C++ knowledge. Anyhow, I am having problem to slove a very simple problem. I am having problem with "Signal and Slot" understanding.

    Task:
    1. By selecting an item from Combo Box
    2. That Item will be showed by a Label in the mainwindow.

    It is very simple thing but I am not able to solve it.

    Here below is my code:

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

    connect(ui->comboBox,&QComboBox::activated,ui->label,&QLabel::setLabelValue);

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    MainWindow::setLabelValue()
    {
    ui->label->text() = ui->comboBox->currentText();
    }

    It is not working. Will there be anyone to suggest me so that I could solve it.

    Thanks in advance!

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Singal and Slot related simple problem!!!

    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
    Qt Code:
    1. 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:
    Qt Code:
    1. 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).

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Singal and Slot related simple problem!!!

    Or use QComboBox::currentTextChanged (unless the combobox allows user input).
    Or use the SIGNAL/SLOT macro based connect().

    Cheers,
    _

  4. #4
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Singal and Slot related simple problem!!!

    Additionally, you can't set the label text with ui->label->text(). You need to use the setText() method. C++ is not C# where you set "properties" using that syntax.

    As already posted, you can connect to setText directly.

Similar Threads

  1. Singal and slot...!
    By prakashmaiya in forum Qt Programming
    Replies: 27
    Last Post: 4th May 2015, 20:33
  2. Replies: 4
    Last Post: 21st January 2013, 12:12
  3. Debugging singal-slot connections
    By gnik in forum Qt-based Software
    Replies: 0
    Last Post: 11th July 2009, 19:50
  4. Workload to the Application:Related to Signal and Slot
    By soumyadeep_pan in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2009, 11:08
  5. Replies: 11
    Last Post: 3rd September 2007, 17:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.