Qt 自定义槽

来源:互联网 发布:k是什么意思网络用语 编辑:程序博客网 时间:2024/05/22 06:39

转载地址:http://zero66.blog.51cto.com/2729872/920758/

增加一个自定义的槽函数,用来改变QDlabel的内容.

改后的hello.h

 

  1. #ifndef _HELLO_H_ 
  2. #define _HELLO_H_ 
  3. #include<QtGui/QtGui> 
  4. #include<QDialog> 
  5. class hello:public QDialog 
  6.     Q_OBJECT 
  7. public: 
  8.     hello(QWidget* =0); 
  9.     QLabel* label; 
  10. public slots: 
  11.     void myslot(); 
  12. }; 
  13. #endif 

改后的hello.cpp

 

  1. #include"hello.h" 
  2. #include<QtCore/QTextCodec> 
  3. hello::hello(QWidget* parent):QDialog(parent) 
  4.     QPushButton* closeBtn = new QPushButton(tr("关闭")); 
  5.     QPushButton* changeBtn = new QPushButton(tr("请点击一下测试")); 
  6.     label = new QLabel(tr("wait for test!")); 
  7.     label->setFont(QFont("ubuntu",35,QFont::Bold)); 
  8.     QVBoxLayout* dlgLayoutnew QVBoxLayout; 
  9.     dlgLayout->addWidget(label); 
  10.     dlgLayout->addWidget(changeBtn); 
  11.     dlgLayout->addWidget(closeBtn); 
  12.     connect(closeBtn,SIGNAL(clicked()),this,SLOT(close())); 
  13.     connect(changeBtn,SIGNAL(clicked()),this,SLOT(myslot())); 
  14.     setLayout(dlgLayout); 
  15. void hello::myslot() 
  16.     label->setText(tr("hello,qt!"));