在QT编程中有时候需要调用一个已经写好的程序来处理正在编写的程序中的数据,这个时候就
需要用到进程来调用那个已经写好的程序。
事先编写好一个layout程序:
#include<QApplication>
#include<QPushButton>
#include<QGridLayout>
#include<QLabel>
#include<QLineEdit>
#include<QMainWindow>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
QGridLayout *vLayout = new QGridLayout(window);
QLabel *label1 = new QLabel("Query",window );
vLayout->addWidget(label1,0,0);
QLineEdit *edit1 = new QLineEdit(window);
vLayout->addWidget(edit1,0,1);
QPushButton *button1 = new QPushButton("Query", window);
vLayout->addWidget(button1,0,2);
QLabel *label2 = new QLabel("Answer",window );
vLayout->addWidget(label2,1,0);
QLineEdit *edit2 = new QLineEdit(window);
vLayout->addWidget(edit2,1,1);
QPushButton *button2 = new QPushButton("Clear", window);
vLayout->addWidget(button2,1,2);
window->setLayout(vLayout);
window->show();
return app.exec();
}
qmake -project
qmake
make
得到layout可执行文件
在写另一个程序时添加如下代码即可调用lauot程序:
QString program = "../layout/layout";//指定要调用的可执行程序的路径
QProcess *myProcess = new QProcess(this);
//this表示myprocess的父窗口是本程序中的主窗口,当主窗口关闭时,进程也会终止,
//如果不填加this,myprocess是独立的进程,主窗口关闭,进程依然运行
myProcess->start(program);如果程序带参数,则写成如下形式:
QString program = "../layout/layout";
QStringList arguments;
arguments << "*" << "*"; //添加参数
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);参考:QT帮助文档
本文介绍如何在QT中调用已编写的程序,并通过示例展示了如何使用QProcess类来启动一个带有或不带参数的外部程序。
1434

被折叠的 条评论
为什么被折叠?



