English 中文(简体)
如何获得具有信号/插槽机制的发送器小部件?
原标题:How to get sender widget with a signal/slot mechanism?
  • 时间:2010-10-28 20:18:23
  •  标签:
  • qt4

可以将多个信号绑定到一个插槽(不是吗?)。那么,有没有一种方法可以理解哪个小部件发送信号?我正在寻找类似于.NET中事件的<code>sender</code>参数的东西

最佳回答

QObject::sender()将完成此工作。

问题回答

使用QObject::sender()在插槽中,如以下示例所示:

void MainWindow::someSetupFunction( void )
{
   ...
   connect( _foobarButton, SIGNAL(clicked()), this, SLOT(buttonPressedSlot()) );
}

void MainWindow::buttonPressedSlot()
{
   // e.g. check with member variable _foobarButton
   QObject* obj = sender();
   if( obj == _foobarButton )
   { 
      ...
   }

   // e.g. casting to the class you know its connected with
   QPushButton* button = qobject_cast<QPushButton*>(sender());
   if( button != NULL ) 
   { 
      ...
   }

}

是的,您可以将多个信号连接到一个插槽。在这种情况下,您将使用QSignalMapper来区分信号源。此解决方案仅限于无参数信号。您可以看到一个示例此处





相关问题
Qt (Creator) with WinSocks (ws2_32)

I want to use an older code-fragment in my Qt-project, which is using WinSocks. I created my program with Qt Creator and I don t know, how I can link to the ws2_32-Library. I already added LIBS += -...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

PRECOMPILED_HEADER and Subdirs template on Qt

I have a big Qt project, splitted on several static libraries (around 70) and one application, and for that I m using a .pro file with the subdirs template. To speed up the compilations time, I want ...

how do i place QTableWidgetItem Icon in center of cell

i want a table cell to have just an icon without any text. i see the QTableWidgetItem class has a method to align the text (int QTableWidgetItem::textAlignment () const) i find no way to adjust the ...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Hiding a QWidget on a QToolbar?

I have directly added some QWidgets to a QToolbar but simply going widget->setVisible(false) did not work. Can someone please give me an example of how to show and hide a widget that is on a QToolbar? ...

热门标签