Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Qt-1-Semnale-si-sloturi
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 <QMainWindow&gt;<br />4: #include <QPushButton&gt;<br />5: #include <QMessageBox&gt;<br />6: //#include <QMainWindow&gt;<br />7: #include <QHBoxLayout&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->setText("Butonul1&quot;);<br />23: button2->setText("Butonul2&quot;);<br />24: QObject::connect(button, SIGNAL (clicked()),this, SLOT (clickedSlot()));<br />25: QObject::connect(button2, SIGNAL (clicked()),this, SLOT (clickedSlot2()));<br />26: button->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);<br />27: button2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);<br />28: QWidget''' centralWidget = new QWidget(window);<br />29: centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);<br />30: QHBoxLayout* layout = new QHBoxLayout(centralWidget);<br />31: layout->addWidget(button);<br />32: layout->addWidget(button2);<br />33: window->setCentralWidget(centralWidget);<br />34: window->setWindowTitle("Semnale si Sloturi&quot;);<br />35: window->show();<br />36: };<br />37: public slots:<br />38: void clickedSlot() {<br />39: QMessageBox *msgBox = new QMessageBox();<br />40: msgBox->setWindowTitle("Salut!");<br />41: msgBox->setText("Ati apasat Butonul1&quot;);<br />42: msgBox->exec&amp;#40;&#41;;<br />43: }<br />44: void clickedSlot2() {<br />45: QMessageBox *msgBox = new QMessageBox();<br />46: msgBox->setWindowTitle("Salut!");<br />47: msgBox->setText("Ati apasat Butonul2&quot;);<br />48: msgBox->exec&amp;#40;&#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 "mywindow.h&quot;<br />6: #include <QApplication&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->decorate();<br />12: return a.exec&amp;#40;&#41;;<br />13: }<br />
Rezultatul compilarii acestui program: