Reputation: 756
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:
connect clicked()
signal of pushButton
object to a local slot and emit signal pushButtonClicked()
from there.
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
Reputation: 7798
You can connect signals directly =)
connect(pushButton , SIGNAL(clicked()), this, SIGNAL(pushButtonClicked()));
Upvotes: 7