最接在做一个项目,觉得原来的项目特垃圾(O(∩_∩)O),就想到多进程的方法。可是怎么将多个进程的界面(主进程),集合到一起就成了一个问题。查了些资料,基友们还是很给力的。在前人的基础上基本完成了自己的目标。话不多说,展示代码和结果:

结果:

 单击QProcessContainer.exe运行容器程序。

单击loadProcess1按钮

单机loadProcess2按钮

这样就把两个进程发到一个容器中了。

看代码:

容器代码:

#include "QProcessContainer.h"
#include <qsettings.h>
#include <QWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QProcess>
QProcessContainer::QProcessContainer(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	m_pvLayout = new QVBoxLayout(this);

	connect(ui.pushButton_loadProcess1, SIGNAL(clicked()), this, SLOT(btnLoadProcess1()));
	connect(ui.pushButton_loadProcess2, SIGNAL(clicked()), this, SLOT(btnLoadProcess2()));
}

QProcessContainer::~QProcessContainer()
{}

void QProcessContainer::btnLoadProcess1()
{
	QProcess *process1 = new QProcess(this);
	process1->start("MyWidget1.exe");
	process1->waitForFinished(2000);

	QString strExeFilePath = QCoreApplication::applicationDirPath();
	strExeFilePath = strExeFilePath + "\\SettingInfo.ini";
	QSettings settings(strExeFilePath, QSettings::IniFormat);
	settings.beginGroup("Process");
	int UE1 = settings.value("pro1").toInt();
	settings.endGroup();


	QWindow *childWin1 = QWindow::fromWinId(UE1);  //根据子窗口id获取QWindow指针
	if (childWin1)//
	{

		childWin1->setFlags(childWin1->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //| Qt::CustomizeWindowHint | Qt::WindowTitleHint
		QWidget *widget1 = QWidget::createWindowContainer(childWin1, this, Qt::Widget); //根据获取的QWindow指针创建一个容器来放子窗口界面
		ui.verticalLayout_Process->addWidget(widget1);
	}
}

void QProcessContainer::btnLoadProcess2()
{
	QProcess *process2 = new QProcess(this);
	process2->start("MyWidget12.exe");
	process2->waitForFinished(2000);


	QString strExeFilePath = QCoreApplication::applicationDirPath();
	strExeFilePath = strExeFilePath + "\\SettingInfo.ini";
	QSettings settings(strExeFilePath, QSettings::IniFormat);
	settings.beginGroup("Process");
	int UE2 = settings.value("pro2").toInt();
	settings.endGroup();
	 

	QWindow *childWin2 = QWindow::fromWinId(UE2);  //根据子窗口id获取QWindow指针
	if (childWin2)//
	{

		childWin2->setFlags(childWin2->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //| Qt::CustomizeWindowHint | Qt::WindowTitleHint
		QWidget *widget2 = QWidget::createWindowContainer(childWin2, this, Qt::Widget); //根据获取的QWindow指针创建一个容器来放子窗口界面
		ui.verticalLayout_Process->addWidget(widget2);
	}
	 
	

}

 两外两个窗口代码(其实一样,只是参数不同):

#include "MyWidget1.h"
#include <QSettings>
#include <qapplication.h>
MyWidget1::MyWidget1(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	ui.m_pushButton->setText("test2");
	QString strExePath = QCoreApplication::applicationDirPath();
	QString strSettingInfo = strExePath + "\\SettingInfo.ini";
	QSettings settings(strSettingInfo, QSettings::IniFormat);// 当前目录的SetInfo.ini文件,一定要添加第二个参数为IniFormat
	settings.beginGroup("Process");
	WId windowID = this->winId();
	settings.setValue("pro2", windowID);
	settings.endGroup();

	connect(ui.m_pushButton, SIGNAL(clicked()), this, SLOT(btnExit()));
}

MyWidget1::~MyWidget1()
{}

void MyWidget1::btnExit()
{
	qApp->exit();
}

可以看到:MyWidget1.exe启动后,会将自己的WId 写进一个SettingInfo.ini文件中。而容器类通过

QWindow *childWin1 = QWindow::fromWinId(UE1);

根据子窗口id获取QWindow指针

然后根据QWindow指针,创建一个容器窗口界面,最后放到容器界面的verticalLayout_Process布局上去。就会显示在QProcessContainer.exe中。
    if (childWin1)//
    {   

        childWin1->setFlags(childWin1->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint);

        //根据获取的QWindow指针创建一个容器来放子窗口界面
        QWidget *widget1 = QWidget::createWindowContainer(childWin1, this, Qt::Widget);
        ui.verticalLayout_Process->addWidget(widget1);
    }

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐