1
2 MyWindow::MyWindow() : QWidget()
3 {
4 // Create button1 and connect button1->clicked() to this->slotButton1()
5 button1 = new QPushButton("Button1", this);
6 connect(button1, SIGNAL(clicked()), this, SLOT(slotButton1()));
7
8 // Create button2 and connect button2->clicked() to this->slotButton2()
9 button2 = new QPushButton("Button2", this);
10 connect(button2, SIGNAL(clicked()), this, SLOT(slotButton2()));
11
12 // When any button is clicked, call this->slotButtons()
13 connect(button1, SIGNAL(clicked()), this, SLOT(slotButtons()));
14 connect(button2, SIGNAL(clicked()), this, SLOT(slotButtons()));
15 }
16
17 void MyWindow::slotButton1() // Slot is called when button1 is clicked.
18 { cout << "Button1 was clicked" << endl; }
19
20 void MyWindow::slotButton2() // Slot is called when button2 is clicked
21 { cout << "Button2 was clicked" << endl; }
22
23 void MyWindow::slotButtons() // Slot is called when 1 or 2 were clicked
24 { cout << "A button was clicked" << endl; }
25
|