初学Qt之--带参数的信号和槽的实现(入门级)

来源:互联网 发布:网络系统集成质量控制 编辑:程序博客网 时间:2024/06/01 07:59

初次接触Qt,由于只有C语言的基础,弄起来很是头疼。下面这个Qt带参数的信号与槽的实例仅供入门之用,高手免观(Qt 4.4.0 实现)

废话不多说,直接上代码:

[cpp] view plaincopy
  1. /*********MyMainWindows.h**************/  
  2.   
  3. #ifndef MYMAINWINDOWS_H_  
  4.     #define MYMAINWINDOWS_H_  
  5.     #include <QWidget>                          
  6.     #include <QPushButton>                     
  7.     #include <QLineEdit>                        
  8.     #include <QLabel>    
  9.   
  10.     //申明一个MyMainWindows类,这个类用于实现窗体                        
  11.     class MyMainWindows:public QWidget         
  12.     {                                                        
  13.      Q_OBJECT //Q_OBJECT是在定制信号和槽和必须包含的一条宏语句                               
  14.     public:                                                     
  15.         MyMainWindows();   //类构造函数(用于实现程序的主体部份)                    
  16.     public slots:          //所有的槽函数都必须申明public slots语句内                       
  17.           void SlotTest(); //这个槽函数用于显应点击按钮时改变文本框内容                      
  18.   private:  
  19.         QPushButton * pb;  //申请一个按钮QPushButton                     
  20.         QLineEdit * ledit; //申请一个文本框ledit                       
  21.         QLabel * label;    //申请一个文本标签label                        
  22.      signals:              //所有信号都需要申明在signals语句内                    
  23.        void SigTest(QString text);   //自定义的带参数的信号SigTest(QString text)。                       
  24.     };   
  25.     #endif      

[cpp] view plaincopy
  1. /*************MyMainWindows.cpp****************/  
  2.   
  3. #include "MyMainWindows.h"  
  4.   
  5. MyMainWindows::MyMainWindows()  
  6. {  
  7.   setGeometry(90,90,300,200);  
  8.   pb = new QPushButton("Modify",this);  
  9.   pb->setGeometry(10,10,100,20);  
  10.   ledit = new QLineEdit("what's your name",this);  
  11.   ledit->setGeometry(10,30,200,150);  
  12.   label = new QLabel("I am a Label",this);       
  13.   label->setGeometry(115,10,100,20);  
  14.   connect(pb,SIGNAL(clicked()),this,SLOT(SlotTest()));   
  15.      
  16.   //连接自定义信号SigTest(QString)和QLineEdit中的预定义槽setText(QString)  
  17.   connect(this,SIGNAL(SigTest(QString)),ledit,SLOT(setText(QString)));   
  18. }  
  19.   
  20.  //实现用于接受pb点击信号的槽     
  21. void MyMainWindows::SlotTest()                       
  22. {  
  23.   label->setText("clicked");  
  24.     
  25.   //发送自定义的信号,请注意信号的参数类型和个数要和槽一样  
  26.   emit SigTest("Hello,I am Keyunchuan");     
  27. }  

[cpp] view plaincopy
  1. /**************Main.cpp**************/  
  2.   
  3. #include <QApplication>           //所有QT应用程序都要包含QApplication头文件     
  4. #include "MyMainWindows.h"  
  5.                 
  6. int main(int argc,char * argv[])  
  7. {  
  8.   QApplication app(argc,argv);    //申明一个QT应用程序对像app      
  9.   MyMainWindows w;                //申明我们实现MyMainWindows窗体对像 w.        
  10.   w.show();                       //显示这个w 窗体           
  11.   return app.exec();  
  12. }  

然后附上运行时截图:


点击pb后


谢谢围观,如有改进之处,敬请留言。


FROM:  http://blog.csdn.net/zgrjkflmkyc/article/details/8516452



0 0