QT菜单栏绑定槽--执行事件
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->action_about,&QAction::triggered,this,[=]() { QMessageBox::information(this,tr("关于"),"合作联系:txwtech@163.com"); }); connect(ui->action_open,&QAction::triggered,this,[=]() { }); connect(ui->action_exit,&QAction::triggered,this,[=]() { QMessageBox::information(this,tr("tips"),"即将退出"); close(); }); // { // }; } MainWindow::~MainWindow() { delete ui; } //void MainWindow::on_action_about_clicked() //{ // connect(ui->action_about,SIGNAL(triggered()),this,SLOT(show_message)); //} void MainWindow::show_message() { QMessageBox::information(this,tr("关于"),"合作联系123:txwtech@163.com"); QMessageBox::information(this,tr("tips"),"关于宇宙的故事"); } void MainWindow::on_menu_About_triggered() { connect(ui->menuAbout,SIGNAL(triggered()),this,SLOT(show_message)); } void MainWindow::on_action_open_triggered() { QMessageBox::information(this,tr("关于"),"点击了打开"); }
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; private slots: //void on_amenubar_clicked(); //void on_action_about_clicked(); void show_message(); void on_menu_About_triggered(); void on_action_open_triggered(); }; #endif // MAINWINDOW_H
欢迎讨论,相互学习。
cdtxw@foxmail.com