Loading

Qt嵌入子Qt程序窗口到当前程序

子程序主要代码

#include "ChildWidgets.h"
#include <QtWidgets/QApplication>
#include <QWindow>
#include <iostream>
#include <QMessageBox>
#include <thread>
int main(int argc, char* argv[])
{
	QApplication a(argc, argv);
	ChildWidgets w;
	w.show();
	w.createWinId();
	// 输出
	std::cerr << w.winId() << std::endl;
	return a.exec();
}

主程序主要代码

InWidgets::InWidgets(QWidget* parent)
	: QMainWindow(parent)
{
	this->resize(600, 400);
	// 获取程序所在路径
	QString path = QCoreApplication::applicationDirPath();
	path += "/ChildWidgets.exe";

	process = new QProcess(this);

	connect(process, &QProcess::readyReadStandardError, [&]() {
		quint64 winId = process->readAllStandardError().toLongLong();
		QWindow* childWin = QWindow::fromWinId(winId);
		if (childWin)
		{
			QWidget* widget = QWidget::createWindowContainer(childWin);
			this->setCentralWidget(widget);
		}
		});
	process->start(path);

	// 子窗口嵌入当前窗口
	process->setParent(this);

}

结果

QQ截图20220627161313.png

地址:https://download.csdn.net/download/qq_44575789/85800705

posted @ 2022-06-27 16:17  WindSnowLi  阅读(77)  评论(0编辑  收藏  举报