Search code examples
c++qtqdialogqt-signalsslot

QT connect Signal from QDialog with Main Window


I'm probably missing something so basic, that I'll be ashamed once you point it out to me, but right now I've been working on this matter for nearly 3 days, so I think it might be time to get some help.

I have a QWizardProject on qt5 and for one of these pages I want a QPushButton, that launches a new Dialog, where I have a QCheckbox (among others). Signals from the QCheckbox should change the QLineEdit on the QWizardPage. Sounds simple enough, I just can't get it to work - at all. I've tried out so many different versions of that (most of them failed building). The most recent version just does nothing. Thanks for any help in advance!

main.h

#ifndef main_H
#define main_H

#include <QWizard>
#include <QtWidgets>

class QCheckBox;
class QLabel;
class QLineEdit;
class QRadioButton;

class OMVGguiWizard : public QWizard
{
    Q_OBJECT

public:
    enum { Page_Main1, Page_Main2, Page_Main3};

    OMVGguiWizard(QWidget *parent = 0);
};

class Page_Main3 : public QWizardPage
{
    Q_OBJECT;

public:
    Mainpage_3(QWidget *parent = 0);

private slots:
    void btnOptionsbuttonsClicked(QString mode);
private:
    QPushButton *OptionsButton;
    QLineEdit *receiver;
};

class FindDialog : public QDialog
{
     Q_OBJECT

public:
     FindDialog(QWidget *parent = 0);

private:
     QCheckBox *checkbox;
     QGridLayout *mainLayout;
};

#endif

main.cpp

Page_Main3::Page_Main3(QWidget *parent)
    : QWizardPage(parent)
{
    // ...

    DensifyOptionsButton = new QPushButton(tr("Densify Settings"));
    receiver = new QLineEdit(tr("empty"));

    connect(OptionsButton, &QPushButton::clicked, [this]() { btnOptionsbuttonsClicked("field1"); });

    // ...
}

void Page_Main3::btnOptionsbuttonsClicked(QString mode)
{
     FindDialog *dialog = new FindDialog;
     dialog->show();
}

void Page_Main3::checkboxClicked()
{
receiver->setText("test");
}

FindDialog::FindDialog(QWidget *parent)
      : QDialog(parent)
{
     checkbox = new QCheckBox(tr("Advanced Options"));
     mainLayout = new QGridLayout;
     mainLayout->addWidget(checkbox);
     setLayout(mainLayout);
     connect(checkbox, SIGNAL(clicked()), this, SLOT(Page_Main3::checkboxClicked() ) );
}

Solution

  • One problem is here:

     connect(checkbox, SIGNAL(clicked()), this, SLOT(Page_Main3::checkboxClicked() ) );
    

    The third argument - receiver - must be object whose slot called. In your case this must be Page_Main3, but you pass "this" - FindDialog.

    You must pass signal from QcheckBox through FindDialog and connect to signal from FindDialog. Example:

    class Page_Main3 : public QWizardPage
    {
      ...
    private slots:
        ...
        void OnCheckBoxClicked();
        ...
    };
    
    class FindDialog : public QDialog
    {
       ...
    signals:
        void checkboxClicked();
       ...
    };
    
    FindDialog::FindDialog(QWidget *parent)
    {
         checkbox = new QCheckBox(tr("Advanced Options"));
         connect(checkbox, &QCheckBox::clicked, this, &FindDialog::checkboxClicked);
         ...
    }
    
    void Page_Main3::btnOptionsbuttonsClicked(QString mode)
    {
        FindDialog *dialog = new FindDialog(this);
        connect(dialog, &FindDialog::checkboxClicked, this, &Page_Main3::OnCheckBoxClicked);
        ...
    }
    
    void Page_Main3::OnCheckBoxClicked()
    {
        receiver->setText("Checkbox clicked!");
    }