1.绘制好ui界面
2.跳转到槽函数
2.1先添加两个头文件
#include <QMessageBox> // 弹出小窗
#include <QProcess> // 新建进程
然后点ui界面“确认”右键跳转到槽;
以此类推;
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QProcess> // 启动新的进程
#include <QMessageBox> // 建立小窗
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
// 连接信号与槽
connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(on_pushButton_clicked()));
//connect(ui->pushButton_2,&QPushButton::clicked,this,&Widget::on_pushButton_2_clicked);
/*
connect(ui->pushButton_3,&QPushButton::clicked,[this]()
{
QMessageBox::information(this,"消息","哈哈");
});*/
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked() // 确认
{
// 创建QSrting program
QString program = ui->lineEdit->text();
// 创建process 对象
QProcess *myProcess = new QProcess(this);
myProcess->start(program);
}
void Widget::on_pushButton_2_clicked()
{
this->close();
}
void Widget::on_pushButton_3_clicked()
{
QMessageBox::information(this,"消息","确认");
}
3.运行结果
输入notepad;
确认后弹出文本;
成功!