Qt-1-Semnale-si-sloturi

From Qt Wiki
Revision as of 06:49, 24 February 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Semnalele si sloturile sunt utilizate pentru comunicarea intre obiecte. Mecanismul semnalelor si sloturilor este piesa centrala a Qt si probabil partea care il diferentiaza cel mai mult fata de alte framework-uri.

Un exemplu simplu:

1: #ifndef MYWINDOW_H<br />2: #define MYWINDOW_H<br />3: #include &lt;QMainWindow&amp;gt;<br />4: #include &lt;QPushButton&amp;gt;<br />5: #include &lt;QMessageBox&amp;gt;<br />6: //#include &lt;QMainWindow&amp;gt;<br />7: #include &lt;QHBoxLayout&amp;gt;<br />8: namespace Ui {<br />9: class myWindow;<br />10: }<br />11: class myWindow : public QMainWindow<br />12: {<br />13: Q_OBJECT<br />14: public:<br />15: explicit myWindow(QWidget *parent = 0);<br />16: ~myWindow();<br />17: void decorate()<br />18: {<br />19: myWindow *window = new myWindow();<br />20: QPushButton *button = new QPushButton();<br />21: QPushButton '''button2 = new QPushButton();<br />22: button-&gt;setText(&quot;Butonul1&amp;quot;);<br />23: button2-&gt;setText(&quot;Butonul2&amp;quot;);<br />24: QObject::connect(button, SIGNAL (clicked()),this, SLOT (clickedSlot()));<br />25: QObject::connect(button2, SIGNAL (clicked()),this, SLOT (clickedSlot2()));<br />26: button-&gt;setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);<br />27: button2-&gt;setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);<br />28: QWidget''' centralWidget = new QWidget(window);<br />29: centralWidget-&gt;setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);<br />30: QHBoxLayout* layout = new QHBoxLayout(centralWidget);<br />31: layout-&gt;addWidget(button);<br />32: layout-&gt;addWidget(button2);<br />33: window-&gt;setCentralWidget(centralWidget);<br />34: window-&gt;setWindowTitle(&quot;Semnale si Sloturi&amp;quot;);<br />35: window-&gt;show();<br />36: };<br />37: public slots:<br />38: void clickedSlot() {<br />39: QMessageBox *msgBox = new QMessageBox();<br />40: msgBox-&gt;setWindowTitle(&quot;Salut!&quot;);<br />41: msgBox-&gt;setText(&quot;Ati apasat Butonul1&amp;quot;);<br />42: msgBox-&gt;exec&amp;amp;#40;&amp;#41;;<br />43: }<br />44: void clickedSlot2() {<br />45: QMessageBox *msgBox = new QMessageBox();<br />46: msgBox-&gt;setWindowTitle(&quot;Salut!&quot;);<br />47: msgBox-&gt;setText(&quot;Ati apasat Butonul2&amp;quot;);<br />48: msgBox-&gt;exec&amp;amp;#40;&amp;#41;;<br />49: }<br />50: private:<br />51: Ui::myWindow '''ui;<br />52: };<br />53: #endif // MYWINDOW_H


1: /'''<br />2: *Program: main.cpp<br />3: '''Data: 23.02.2014<br />4:'''/<br />5: #include &quot;mywindow.h&amp;quot;<br />6: #include &lt;QApplication&amp;gt;<br />7: int main(int argc, char *argv[])<br />8: {<br />9: QApplication a(argc, argv);<br />10: myWindow *window = new myWindow();<br />11: window-&gt;decorate();<br />12: return a.exec&amp;amp;#40;&amp;#41;;<br />13: }<br />

Rezultatul compilarii acestui program:

Semnale si sloturi Butonul1

Semnale si sloturi Butonul2