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
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: