thequark
thequark

Reputation: 756

Qt: Best mechanism to capture and pass signals

I have a class extending QWidget class and this class uses a Ui class generated by QtDesigner. Something like following:

class MyWidget : public QWidget
{
   signals:
            void pushButtonClicked();
   // ....
   private:
            Ui::MyUi ui;

}

MyWidget::MyWidget()
{
    ui = new Ui::MyUi();
    ui->setupUi(this);
}

Now lets suppose there is a QPushButton * pushButton field inside Ui::MyUi. The UI for this widget has no complex business logic.

Now it is required that whenever pushButton is clicked this widget should emit a signal (lets call it pushButtonClicked(). Now the only way I can imagine this to be achieved is to:

  1. connect clicked() signal of pushButton object to a local slot and emit signal pushButtonClicked()from there.

  2. Install MyWidget as mouseEventFilter for pushButton and handle it inside MyWidget::mouseClickedEvent

Are there other options? Like somehow letting Qt framework to use 'MyWidget::pushButtonClicked()' instead of pushButton->clicked()

In the project I have many scenarios like this where such signal passing is needed from the wrapper classes like MyWidget, which wrap/abstract the UI by aggregation or inheritance. In general which is considered the best approach for better design, code reuse esp. when the project is at nascent stage and it is not known which of these UIs might need complex business logic in the future.

Please note that extending QPushButton is not an option.

Upvotes: 2

Views: 622

Answers (1)

B. D
B. D

Reputation: 7798

You can connect signals directly =)

connect(pushButton , SIGNAL(clicked()), this, SIGNAL(pushButtonClicked()));

Upvotes: 7

Related Questions