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

来源:互联网 发布:苹果查询网络 编辑:程序博客网 时间:2024/05/14 23:40
一、通过 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  

同样实现一样的功能
0 0