7

I have an inherited QTreeWidget (called PackList) class and its parent is a KXmlGuiWindow. How can I access to the parent's slots?

I've tried getParent()->mySlot() from the QTreeWidget class but I've got

error: no matching function for call to 'PackList::mySlot()'

Does anybody know the correct way? Thanks

JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87

2 Answers2

15

If you know the parent's class, you will have to cast parentWidget() to that class and then call your slot. Keep in mind whether or not it's a slot makes no difference in this case. You are just calling a method.

((KXmlGuiWindow*)parentWidget())->mySlot();

You can make the call without casting by wiring up your signal to the slot.

connect( this, SIGNAL(mySignal()), parentWidget(), SLOT(mySlot()) );

Lastly, you can use QMetaObject::invokeMethod to call it if you don't want to cast it. That's probably overkill.

Michael Bishop
  • 4,240
  • 3
  • 37
  • 41
  • This is the correct way to do it. I have used it without typecast `parentWidget())->setEnabled();` and I override the parent's setEnabled function. And then, my modification did not work. well, I realized later that this is so silly mistake, but I think it can be a point many fellows may waste their time. anyway; DO NOT FORGET THE TYPECAST. – meakgoz Oct 14 '15 at 09:43
1

I'm not sure I fully understand your question.

However, you can access the parent widget of a widget with parentWidget().

Then, you should be able to call any public slot :

parentWidget()->a_slot();
Jérôme
  • 26,567
  • 29
  • 98
  • 120