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
Next revisionBoth sides next revision
qt:wiring_up_signals_and_slots [2011/03/31 13:00] – [Do it in the *.ui file] mithatqt:wiring_up_signals_and_slots [2011/04/01 11:13] – [Creating signal→slot connections] mithat
Line 2: Line 2:
  
 ===== Creating signal→slot connections ===== ===== Creating signal→slot 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. If you use Qt Creator for development, 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 are discussed below. 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 are discussed below.
Line 16: Line 16:
  
 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 23:
  
 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> 
 +== 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.
  
 ==== Do it "visually" ==== ==== Do it "visually" ====
Line 105: Line 110:
 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 ===== ===== Runtime signal→slot 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.txt · Last modified: 2011/04/03 13:14 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki