Qt-1-Semnale-si-sloturi: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 3: Line 3:
Un exemplu simplu:
Un exemplu simplu:


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


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


Rezultatul compilarii acestui program:
Rezultatul compilarii acestui program:

Revision as of 12:39, 25 February 2015

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


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

Rezultatul compilarii acestui program:

Semnale si sloturi Butonul1

Semnale si sloturi Butonul2