1

Let's say we have more than one widgets and all of them have main menu button. so I connected all of the main menu buttons clicked signal to one slot, which is supposed to h

connect(widget1->mainMenuButton, SIGNAL(clicked()),this, SLOT(mainClicked()));
connect(widget1->mainMenuButton, SIGNAL(clicked()),this, SLOT(mainClicked()));

and I have

private slots:
    void mainClicked();

in mainClicked I want to hide whoever triggered the clicked signal. frame1 or frame2 in the above example.

I could use sender() to retrieve the QObject that triggered the signal but then how can I call hide which is QWidget function?

your help is appreciated.

gehad
  • 1,205
  • 3
  • 12
  • 17

2 Answers2

2

I think this should work:

dynamic_cast<QWidget*>(sender()) -> hide() ;
TonyK
  • 16,761
  • 4
  • 37
  • 72
  • thx I did this QObject *caller = sender()->parent(); ((QWidget*)caller)->hide(); because the sender would be the child of the widget – gehad May 23 '11 at 05:39
  • qobject_cast(sender())->window()->hide(); if you want to hide whole window of sender – Kamil Klimek May 24 '11 at 12:14
0

Take a look at the QSignalMapper class. Basically you connect your two widget's clicked() signal to the map() slot of the signal mapper and it emits a single mapped(QWidget*) signal, that you connect to your mainClicked(QWidget*) slot and you get the widget passed, that emited the clicked signal (actually you can configure the parameter yourself). The Qt documentation can tell you more.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185