Other components added in QMainwindow cannot send messages to call slot functions

Other components added in QMainwindow cannot send messages to call slot functions

problem lies in

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "QMessageBox"
#include "QLineEdit"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);

    QAction* a1;
    QAction* a2;
    QAction* a3;
    QAction* a4;
    a1 = ui->menu->addAction("打开老项目");
    //给工具栏添加单行输入框
    a2 = ui->toolBar->addWidget(new QPushButton("搜索栏"));
    a3 = ui->toolBar->addWidget(new QLineEdit);
    a4 = ui->toolBar->addAction("工具4");

    //菜单栏
    connect(a1,&QAction::triggered,this,[=]{
    
    
        qDebug()<<"点击了一下打开项目";
        QMessageBox::information(this, "提示框",
                                 "成功打开老项目",
                                 QMessageBox::Ok,
                                 QMessageBox::Save);
    });

    connect(ui->createprogect1,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下创建项目";
                QMessageBox::information(this, "提示框",
                                         "成功创建项目",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(ui->open_action,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下打开项目";
                QMessageBox::information(this, "提示框",
                                         "成功打开项目",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    //工具栏
    connect(ui->action_4,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下工具4";
                QMessageBox::information(this, "提示框",
                                         "点击了一下工具4",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(ui->action_1,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下工具1";
                QMessageBox::information(this, "提示框",
                                         "点击了一下工具1",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(a2,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"请输入搜索类容";
                QMessageBox::information(this, "提示框",
                                         "请输入搜索类容",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(a2,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"请输入搜索类容";
                QMessageBox::information(this, "提示框",
                                         "请输入搜索类容",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(a4,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了工具4";
                QMessageBox::information(this, "提示框",
                                         "点击了工具4",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });

}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}


Click Tool 4 to call the slot function

insert image description here
Clicking on the search bar does nothing. . .

Solution

Adding a QAcion component to the toolbar can specify the slot function to call by returning a value

But if the QAcion component is not added to the toolbar, it cannot be specified by the return value.

The correct way is to create the control object first, and then add the control to the toolbar.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "QMessageBox"
#include "QLineEdit"
#include "QPushButton"
#include "QLineEdit"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);

    QAction* a1;
    QAction* a2;

    QAction* a4;
   //QPushButton* b1= new QPushButton(this);
   // b1->setText("搜索按钮");
   // QLineEdit* e1 =  new QLineEdit();

    a1 = ui->menu->addAction("打开老项目");
    //给工具栏添加单行输入框
    ui->toolBar->addWidget(new QPushButton("搜索按钮"));
    ui->toolBar->addWidget(new QLineEdit);
    a4 = ui->toolBar->addAction("工具4");


    //菜单栏
    connect(a1,&QAction::triggered,this,[=]{
    
    
        qDebug()<<"点击了一下打开项目";
        QMessageBox::information(this, "提示框",
                                 "成功打开老项目",
                                 QMessageBox::Ok,
                                 QMessageBox::Save);
    });

    connect(ui->createprogect1,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下创建项目";
                QMessageBox::information(this, "提示框",
                                         "成功创建项目",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(ui->open_action,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下打开项目";
                QMessageBox::information(this, "提示框",
                                         "成功打开项目",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    //工具栏
    connect(ui->action_4,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下工具4";
                QMessageBox::information(this, "提示框",
                                         "点击了一下工具4",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    connect(ui->action_1,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了一下工具1";
                QMessageBox::information(this, "提示框",
                                         "点击了一下工具1",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
//    connect(a2,&QAction::triggered,this,[=]{
    
    
//                qDebug()<<"请输入搜索类容";
//                QMessageBox::information(this, "提示框",
//                                         "请输入搜索类容",
//                                         QMessageBox::Ok,
//                                         QMessageBox::Save);
//            });
//    connect(b1,&QAction::triggered,this,[=]{
    
    
//                qDebug()<<"请输入搜索类容";
//                QMessageBox::information(this, "提示框",
//                                         "请输入搜索类容",
//                                         QMessageBox::Ok,
//                                         QMessageBox::Save);
//            });
    connect(a4,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击了工具4";
                QMessageBox::information(this, "提示框",
                                         "点击了工具4",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });

    //对于toolbar2的操作
    QAction* a5;
    a5 = ui->toolBar_2->addAction("新建项目");
    connect(a5,&QAction::triggered,this,[=]{
    
    
                qDebug()<<"点击新建项目";
                QMessageBox::information(this, "提示框",
                                         "点击了新建项目",
                                         QMessageBox::Ok,
                                         QMessageBox::Save);
            });
    QPushButton *b5 = new QPushButton(this);
    b5->setText("^_^");
    //添加小控件
    ui->toolBar_2->addWidget(b5);
    connect(b5,&QPushButton::clicked,this,[=]{
    
    
        qDebug()<<"点击了^_^";
        QMessageBox::information(this, "提示框",
                                 "点击了^_^",
                                 QMessageBox::Ok,
                                 QMessageBox::Save);
    });
    QLineEdit* l5 = new QLineEdit();
    //添加小控件
    ui->toolBar_2->addWidget(l5);
    connect(l5,&QLineEdit::editingFinished,this,[=]{
    
    
        qDebug()<<"点击了^_^";
        QMessageBox::information(this, "提示框",
                                 "完成了输入",
                                 QMessageBox::Ok,
                                 QMessageBox::Save);
    });


}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}


insert image description here
insert image description here

reference article

Guess you like

Origin blog.csdn.net/qq_41701723/article/details/132223711