Qt - Call parent widget slots

I wrote a small program to check access to the widget’s parent slot. Basically, it has two classes:

Widget:

namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); QLabel *newlabel; QString foo; public slots: void changeLabel(); private: Ui::Widget *ui; }; Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); customWidget *cwidget = new customWidget(); newlabel = new QLabel("text"); foo = "hello world"; this->ui->formLayout->addWidget(newlabel); this->ui->formLayout->addWidget(cwidget); connect(this->ui->pushButton,SIGNAL(clicked()),cwidget,SLOT(callParentSlot())); connect(this->ui->pb,SIGNAL(clicked()),this,SLOT(changeLabel())); } void Widget::changeLabel(){ newlabel->setText(this->foo); } 

and customWidget:

 class customWidget : public QWidget { Q_OBJECT public: customWidget(); QPushButton *customPB; public slots: void callParentSlot(); }; customWidget::customWidget() { customPB = new QPushButton("customPB"); QHBoxLayout *hboxl = new QHBoxLayout(); hboxl->addWidget(customPB); this->setLayout(hboxl); connect(this->customPB,SIGNAL(clicked()),this,SLOT(callParentSlot())); } void customWidget::callParentSlot(){ ((Widget*)this->parentWidget())->changeLabel(); } 

in the main function, I just created an instance of Widget and called show () on it. This Widget instance has a label, QString, an instance of the customWidget class, and two buttons (inside the ui, pushButton, and pb class). One of the buttons calls a slot in its own class called changeLabel (), which, as the name implies, changes the label to everything that is installed in the QString contained in it. I did this to test changeLabel () work. This button works fine. Another button calls the slot in the customWidget instance called callParentSlot (), which in turn tries to call the changeLabel () slot in its parent. Since in this case I know that its parent instance is actually an instance of Widget, I return the return value of parentWidget () to Widget *. This button causes the program to crash. I made a button in customWidget to try to call the parent customWidget slot, but also the program crashed. I followed what was on this question. What am I missing?

+7
qt signals-slots
source share
1 answer

You never set a parent widget for an instance of customWidget . So this->parentWidget() will most likely return a NULL pointer. Make the following changes:

 customWidget *cwidget = new customWidget(this); ... customWidget(QWidget *parent); ... customWidget::customWidget(QWidget *parent) : QWidget(parent) 

I would also recommend using dynamic_cast and checking the return value. This prevented your crash, as in the case where the parent is NULL, and where the parent class does not match the class.

 void customWidget::callParentSlot() { Widget *w = dynamic_cast<Widget *> (this->parentWidget()); if (0 != w) w->changeLabel(); /* else handle the error */ } 

Another approach would be to call the parent slot through the signals and slots interface. Connect the new customWidget signal to the Widget slot in the Widget constructor. Then you can call the slot from customWidget as follows.

 emit callParentSignal(); 
+2
source share

All Articles