User Tools

Site Tools


qt:wiring_up_signals_and_slots

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
qt:wiring_up_signals_and_slots [2011/03/31 12:52] mithatqt:wiring_up_signals_and_slots [2011/04/03 13:14] (current) mithat
Line 1: Line 1:
 ====== Wiring up signals and slots ====== ====== Wiring up signals and slots ======
  
-===== Creating signal->slot connections ===== +===== Creating signalslot connections ===== 
-Qt uses a signals and slots system to process events. If you use Qt Creator for development, there are at least four different ways to make signal->slot connections: +Qt uses a [[signals and slots]] system to process events. There are at least four different ways to make signal->slot connections: 
-  * Create an "event handler" +  * [[#Create an "event handler"]] 
-  * Do it "visually" +  * [[#Do it "visually"]] 
-  * Do it in the *.ui file +  * [[#Do it in the *.ui file]] 
-  * Do it in the constructor +  * [[#Do it in the constructor ]]
  
-If you use Qt Designer with some other IDE for development, three of these are available to you. If you are hand-coding everything, then two are.+Each of the above listed approaches and when they are available are discussed below.
  
-==== Create an "event handler" ==== +==== The alternatives ==== 
-//Availability: Qt Creator//+ 
 +=== Create an "event handler" === 
 +//Availability: Qt Creator and hand-coded projects (apparently)//
  
 Users of Visual Studio, Delphi, and Lazarus will be most familiar with this approach. In Qt Creator, right click on a control and select //Go to slot ...//. A dialog box giving you a list of signals available for the control will appear; select the signal for which you wish to create a handler. If the handler doesn't already exist, Qt Creator will create it and take you to it. If it does, then Qt Creator will just take you to it. Users of Visual Studio, Delphi, and Lazarus will be most familiar with this approach. In Qt Creator, right click on a control and select //Go to slot ...//. A dialog box giving you a list of signals available for the control will appear; select the signal for which you wish to create a handler. If the handler doesn't already exist, Qt Creator will create it and take you to it. If it does, then Qt Creator will just take you to it.
  
 A handler you make this way for a ''button_quit'' widget might look like: A handler you make this way for a ''button_quit'' widget might look like:
-<code cpp>+<code cpp-qt>
 void MainWindow::on_button_quit_clicked() void MainWindow::on_button_quit_clicked()
 { {
Line 23: Line 25:
  
 The handlers you create this way are actually a private slots. You can confirm this by looking at the header file for the class you are editing: The handlers you create this way are actually a private slots. You can confirm this by looking at the header file for the class you are editing:
-<code cpp>+<code cpp-qt>
 private slots: private slots:
     void on_button_quit_clicked();</code>     void on_button_quit_clicked();</code>
  
-=== Behind the scenes === +== Behind the scenes == 
-How does the build system know that that ''button_quit'''s ''clicked()'' signal connects to ''MainWindow'''s ''on_button_quit_event_clicked()'' slot? It appears that the connection between signals and slots created this way is done purely via a naming convention along the following lines: ''//<widget-name>//'' ''//<signal>()//'' automatically connects to any slot of the form ''//<ClassName>//::on_//<widget-name>_<signal>()//''.+How does the build system know that that ''button_quit'''s ''clicked()'' signal connects to ''MainWindow'''s ''on_button_quit_event_clicked()'' slot? It appears that the connection between signals and slots created this way is done purely via a naming convention along the following lines: 
 +<code cpp-qt><widget-name> <signal-name>()</code> automatically connects to any slot of the form <code cpp-qt><ClassName>::on_<widget-name>_<signal-name>()</code>
  
-==== Do it "visually" ====+== Question == 
 +If the above is true, then it should be possible to create event handlers just by writing code. Two tests are indicated: 
 +  * Write the handlers manually in Qt Creator. 
 +  * Write the handlers manually in a project managed by something other than Qt Creator. 
 +I've done both and assuming I didn't overlook something, indeed you can hand-add handlers as suggested. 
 + 
 +=== Do it "visually" ===
 //Availability: Qt Creator, Qt Designer// //Availability: Qt Creator, Qt Designer//
  
Line 50: Line 59:
 When you set signal/slot connections this way, you will see arrows connecting widgets indicating signal/slot relationships. When you set signal/slot connections this way, you will see arrows connecting widgets indicating signal/slot relationships.
  
-=== Special cases ===+== Special cases ==
 There are a couple special cases that are worth mentioning. There are a couple special cases that are worth mentioning.
  
-== Connecting to the window ==+**Connecting to the window**
 If you want to connect a signal to the slot of a window, just drag onto the window rather than onto another widget. You will know you are connecting to the main window because the slot end of the connector will turn into an electrical earth ground symbol {{:qt:50px-earth_ground.svg.png?8|}} rather than an arrow. If you want to connect a signal to the slot of a window, just drag onto the window rather than onto another widget. You will know you are connecting to the main window because the slot end of the connector will turn into an electrical earth ground symbol {{:qt:50px-earth_ground.svg.png?8|}} rather than an arrow.
  
-== Connecting a widget to itself ==+**Connecting a widget to itself**
 You can also connect widgets to themselves. If you do this, you can set up all sorts of hilarity. Sometimes you can even do useful things. You can also connect widgets to themselves. If you do this, you can set up all sorts of hilarity. Sometimes you can even do useful things.
  
-=== Editing signals and slots ===+== Editing signals and slots ==
 You can edit existing signal/slot connections made in Edit Signal/Slots mode by using the Signal/Slot Editor that appears at the bottom of the screen in Qt Creator and the left bottom on Qt Designer. Each column of the editor is actually a drop-down list that you need to double click on to delete. You can edit existing signal/slot connections made in Edit Signal/Slots mode by using the Signal/Slot Editor that appears at the bottom of the screen in Qt Creator and the left bottom on Qt Designer. Each column of the editor is actually a drop-down list that you need to double click on to delete.
  
 You can add new relationships and delete existing ones using the **+** and **–** icons. The **–** icon seems a bit temperamental, so if it isn't activated for you, try clicking on various bits on your form until it activates. (And if you figure out what the logic that going on here is, please let me know.) You can add new relationships and delete existing ones using the **+** and **–** icons. The **–** icon seems a bit temperamental, so if it isn't activated for you, try clicking on various bits on your form until it activates. (And if you figure out what the logic that going on here is, please let me know.)
  
-=== Behind the scenes ===+== Behind the scenes ==
 Connections made this way are stored in a form's ''//<form-name>//.ui'' file in the ''<connections>'' section. For example, here is the snippet from a file ''mainwindow.ui'' that connects ''button_quit'''s ''clicked()'' signal to the window's ''close()'' slot:<code xml> Connections made this way are stored in a form's ''//<form-name>//.ui'' file in the ''<connections>'' section. For example, here is the snippet from a file ''mainwindow.ui'' that connects ''button_quit'''s ''clicked()'' signal to the window's ''close()'' slot:<code xml>
  <connections>  <connections>
Line 86: Line 95:
  
 Note: If you are using Qt Creator to develop your project, the Qt gods discourage you from manually editing ''*.ui'' files. Note: If you are using Qt Creator to develop your project, the Qt gods discourage you from manually editing ''*.ui'' files.
-==== Do it in the *.ui file ====+=== Do it in the *.ui file ===
 //Availability: any kind of project// //Availability: any kind of project//
  
-If your project uses ''*.ui'' files to specify UI layout, then you can specify signal->slot connections here in the same way that Qt Creator and Qt Designer do(See example above.+If your project uses ''*.ui'' files to specify UI layout, then you can specify signal->slot connections here in the same way that Qt Creator and Qt Designer do--by specifying the connections in the ''<connections>'' section: 
-  +<code xml> 
-==== Do it in the constructor ====+ <connections> 
 +  <connection> 
 +   <sender>pushButton</sender> 
 +   <signal>clicked()</signal> 
 +   <receiver>HelloForm</receiver> 
 +   <slot>close()</slot> 
 +  </connection> 
 + </connections> 
 +</code> 
 + 
 +=== Do it in the constructor ===
 //Availability: any project// //Availability: any project//
  
 You can explicitly connect signals and slots programatically. This is typically done in a form's constructor.  You can explicitly connect signals and slots programatically. This is typically done in a form's constructor. 
  
-<code cpp>+<code cpp-qt>
 HelloForm::HelloForm() HelloForm::HelloForm()
 { {
     widget.setupUi(this);     widget.setupUi(this);
  
-    connect(widget.button_quit, SIGNAL(clicked()), +    connect(widget.button_quit, SIGNAL(clicked()), this, SLOT(close()));
-            this, SLOT(close()));+
 }</code> }</code>
  
-===== Runtime signal->slot connection modifications =====+==== Recommendation ==== 
 +While the [[#Create an "event handler"]] approach is the fastest, my current thinking is that using the Qt Designer approach of [[#Do it "visually"]] is actually the best way to do this--using the Signal/Slot editor panel if needed to add custom slots to the forms. The downside to this is that you have to hand-code the custom slots. The upsides are that the connections are well documented and that this approach simplifies moving your program from C++ to some other language (Python, Ruby, Java, etc.). 
 + 
 +===== Runtime signalslot connection modifications =====
 I don't know. But it should be possible. I don't know. But it should be possible.
qt/wiring_up_signals_and_slots.1301575961.txt.gz · Last modified: 2011/03/31 12:52 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki