我要创建一个用户槽,下面是我的cpp文件:
#include "slot.h"
class MyMainWindow:public QWidget
{
Q_OBJECT
public:
MyMainWindow();
public slots:
int MyExitSlot(); //声明的用户槽
private:
QPushButton *b1;
QPushButton *b2;
QPushButton *b3;
QLineEdit *ledit;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,400,200);
b1=new QPushButton("Click here to mark the text",this);
b1->setGeometry(10,10,350,40);
b1->setFont(QFont("Times",18,QFont::Bold));
b2=new QPushButton("Click here to cut the text",this);
b2->setGeometry(10,60,350,40);
b2->setFont(QFont("Times",18,QFont::Bold));
b3=new QPushButton("Click here to remove the text",this);
b3->setGeometry(10,110,350,40);
b3->setFont(QFont("Times",18,QFont::Bold));
ledit=new QLineEdit("This is a line of text",this);
ledit->setGeometry(10,160,350,30);
//The following three lines connects each button to a predefined slot on
//the ledit object.Test the program to find out what happens
connect(b1,SIGNAL(clicked()),ledit,SLOT(selectAll()));
connect(b2,SIGNAL(clicked()),ledit,SLOT(MyMainWindow::MyExitSlot()));
connect(b3,SIGNAL(clicked()),ledit,SLOT(clear()));
}
int MyMainWindow::MyExitSlot() //定义用户槽
{
exit(0);
}
int main(int argc,char **argv)
{
QApplication a(argc,argv);
MyMainWindow w;
w.show();
a.exec();
return 0;
}
头文件:
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlineedit.h>
#include <qstring.h>
#include <stdlib.h>
请问如果我要编译这个文件,需要怎么实现?
怎么使用moc?
谢谢各位大侠