eklavay.tonoy
eklavay.tonoy

Reputation: 23

Signal and Slot understanding in Qt

I am having problem with "Signal and Slot" understanding. Here below is my task description.

  1. Select an item from Combo Box
  2. Display that selected item, by a Label in the mainwindow.

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

Here below is my code:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui { class MainWindow; }

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void activated(QString);

private slots:
     void setLabelValue(const QString &text);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H    
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->comboBox, &QComboBox::activated(QString),
            this, &MainWindow::setLabelValue(QString));
}

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

MainWindow::setLabelValue(const QString &text)
{
    //text = ui->comboBox->currentText();
text = static_cast<void (QComboBox::*)(QString)>(ui->comboBox->currentText());
    ui->label->setText(text);
}

I hope you will forgive this novice's coding and understanding style.

Upvotes: 0

Views: 270

Answers (2)

eklavay.tonoy
eklavay.tonoy

Reputation: 23

Thanks all for your support. Here below the code, that has worked.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
     void setLabelValue(const QString &text);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated), this,
            &MainWindow::setLabelValue);

}

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

void MainWindow::setLabelValue(const QString &text)
{
    ui->label->setText(text);
}

Upvotes: -1

Nicolas Holthaus
Nicolas Holthaus

Reputation: 8273

The problem is you need to use the QLabel accessor function to change the text value. text() is a 'getter', not a 'setter'.

change MainWindow::setLabelValue() to

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

Also, your connection isn't quite right. setLabelValue is a function of MainWindow, not the label, so change the connection to:

void (QComboBox::* activatedOverloadPtr)(const QString&) = &QComboBox::activated;
connect(ui->comboBox,activatedOverloadPointer, this, &MainWindow::setLabelValue);

Activated overload pointer is used so that the compiler can resolve which specific activated connection you are looking for.

The complete code would look something like:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui { class MainWindow; }

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

//signals: // this is defined by QComboBox, not you (in this case)
//    void activated(QString);

private slots:
     void setLabelValue(const QString &text);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H  

.

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    void (QComboBox::* activatedOverloadPtr)(const QString&) = &QComboBox::activated;
    connect(ui->comboBox,activatedOverloadPointer, this, &MainWindow::setLabelValue);
}

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

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

Upvotes: 3

Related Questions