Question

I have three classes inherited from QWidget. Clicking First object's button creates Second object. Clicking Seconds object's button creates Third object. Third object has one button "Quit". Clicking this button should close Third object and Second object. How can I know in Second object that Third object's button was clicked?

class First : public QWidget {
    Q_OBJECT
    public:
       First();
       virtual ~First();

    private slots:
       void quit();
       void createSecond();

    private:
       Ui::First widget;
       Second *second;
};

class Second : public QWidget {
    Q_OBJECT
    public:
       Second();
       virtual ~Second();

    private slots:
       void createThird();
       void quit();

    private:
       Ui::Second widget;
};

class Third : public QWidget {
    Q_OBJECT
    public:
       Third();
       virtual ~Third();

    private slots:
       void quit();

    private:
       Ui::Third widget;
};
Was it helpful?

Solution

You simply can connect the buttonClicked signal (should be declared) of the Third object's button with the quit() slot of the Second widget:

Second::createThird()
{
    [..]
    Third *third = new Third;
    connect(third, SIGNAL(buttonClicked()), this, SLOT(quit());
    [..]    
}

You can emit buttonClicked() signal when you click on the button, or do that in your Third widget's Third::closeEvent(QCloseEvent *) virtual function's implmentation:

Third::closeEvent(QCloseEvent *event)
{
    emit buttonClicked();
    QWidget::closeEvent(event);
}

OTHER TIPS

Or, you can create in Second:

void Second::process() {
  if (!widget->isVisible())
    close();
}

And:

Second::Second() {
  QTimer *timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), this, SLOT(process()));
  timer->start();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top