问题所在
#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;
}
点击工具4可以调用槽函数
点击搜索栏没有任何反应。。。
解决办法
在工具栏中添加QAcion组件可以通过返回值来指定调用的槽函数
但如果工具栏中添加的不是QAcion组件,则不能同过返回值来指定了。
正确的方式是,先创建控件对象,然后将控件添加到工具栏中。
#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;
}