qt creator 信号与槽 代码实现 (二)

2157阅读 0评论2011-04-23 yuweixian4230
分类:C/C++


本文记录了qt creator 的一些简单的使用方法。本篇中,我

们通过两钟代码实现  信号槽 功能实现



我们接着 “qt creator 信号与槽 ui 实现” 第八步


一、通过 go to  slot 选项实现

1.单击 “今天”,选择 go to slots

2.在 mianwindow.h 文件下产生了
    
  1. private slots:
  2.     void on_todayButton_clicked();

3.在 mianwindow.cpp 文件产生了
  1. void MainWindow::on_todayButton_clicked()
  2. {
  3.    
  4. }

4.我们在 上述文件中添加为如下,也是显示 下一个月的功能
  1. void MainWindow::on_todayButton_clicked()
  2. {
  3.     ui->calendarWidget->showNextMonth();
  4. }
5.我们通过ui 界面设置 “close”  信号槽 功能

6.编译,运行 和 上一篇 一样,实现了相同的功能

***********************************************************************


二.通过 自己代码实现
 

1.在mianwindow.h中 添加自己

  1. private:
  2.     Ui::MainWindow *ui;

  3. private slots:
  4.     void on_todayButton_clicked();
  5.     void gototoday();  //自己添加槽函数
  6. };

2.在 mainwindow.cpp中实现 gototoday()


  1. void MainWindow::on_todayButton_clicked()
  2. {
  3.   // ui->calendarWidget->showNextMonth();
  4. }

  5. void MainWindow::gototoday()   实现 gototoday 函数
  6. {
  7.     ui->calendarWidget->showNextMonth();
  8. }

3.mainwindow.cpp 的构造函数中添加 connect 连接函数


  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5.     ui->setupUi(this);   //添加代码 红色
  6.     connect(ui->todayButton,SIGNAL(clicked()),this,SLOT(gototoday()));
  7. }

4. qmake 

同样实现一样的功能




上一篇:qt creator 信号与槽 ui 使用 (一)
下一篇:qt creator label和 text 简单实验 (三)