以下功能是用两个button在终端机下印出字串
/*ButtonWindow.h*/
#include<qmainwindow.h>
class ButtonWindow : public QMainWindow
{
Q_OBJECT
public:
ButtonWindow (QWidget *parent = 0 , const char *name=0);
virtual ~ButtonWindow();
private slots:
void Clicked();
void Clicked2();
};
/*ButtonWindow.cpp*/
#include "ButtonWindow.h"
#include <qpushbutton.h>
#include <qapplication.h>
#include <iostream>
ButtonWindow::ButtonWindow(QWidget *parent , const char *name):QMainWindow(parent,name)
{
this->setCaption("17-8..test"); //設定標題
QPushButton *button = new QPushButton("click me!",this,"Button1");
QPushButton *button2 = new QPushButton("hello",this,"Button2");
//QPushButton的建構元 1.Button的文字 2..parent 3.name button是QPushButton指標
button->setGeometry(20,50,90,30);
button2->setGeometry(20,10,90,30);
//button用來設定大小
connect (button,SIGNAL(clicked()),this,SLOT(Clicked()));//連接信號
connect (button2,SIGNAL(clicked()),this,SLOT(Clicked2()));//連接信號
}
ButtonWindow::~ButtonWindow() //呼叫解構元
{
}
void ButtonWindow::Clicked(void) //接收到信號時要做的動做
{
std::cout<<"clicked!\n";
}
void ButtonWindow::Clicked2(void) //接收到信號時要做的動做
{
std::cout<<"hello!!\n";
}
int main(int argc, char **argv)
{
QApplication app(argc,argv);
ButtonWindow *window = new ButtonWindow();
//宣告一個 類別指標 *window 指向 ButtonWindow()
app.setMainWidget(window);
window->show();
return app.exec();
}
想请教各位,为什么在button2的connect的信号函数一定要设为SIGNAL(clicked())
不能设成SIGNAL(clicked2()),还有程式中并没有设定signals,那又是怎么做signals/slot的功能呢?!
麻烦老大们多指教,初学问多多..