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

一、通过 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  

同样实现一样的功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值