用代码举例Linux系统下qt开发的可执行程序界面嵌入另一个可执行程序widget中
在Linux系统下,可以使用Qt开发一个可执行程序,并将其界面嵌入到另一个可执行程序的widget中。以下是一个示例代码:
主程序main.cpp:
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QMainWindow>
#include <QWidget>
#include <QProcess>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 创建主窗口
QMainWindow mainWindow;
QWidget *centralWidget = new QWidget(&mainWindow);
mainWindow.setCentralWidget(centralWidget);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
// 创建一个标签和按钮
QLabel *label = new QLabel("Hello World");
QPushButton *button = new QPushButton("Launch Widget");
layout->addWidget(label);
layout->addWidget(button);
// 创建另一个可执行程序的widget
QWidget *widget = new QWidget;
QHBoxLayout *widgetLayout = new QHBoxLayout(widget);
QProcess *process = new QProcess;
process->start("./another_executable"); // 启动另一个可执行程序
process->waitForStarted();
process->setProcessChannelMode(QProcess::MergedChannels);
widgetLayout->addWidget(process);
widget->setLayout(widgetLayout);
// 点击按钮时将widget作为子窗口显示在主窗口中
QObject::connect(button, &QPushButton::clicked, [&]() {
QMainWindow *subWindow = new QMainWindow(&mainWindow);
subWindow->setCentralWidget(widget);
subWindow->show();
});
mainWindow.show();
return app.exec();
}
另一个可执行程序的main.cpp:
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label("Another Executable");
label.show();
return app.exec();
}
在这个示例中,我们创建了一个主程序,其中包含一个标签和一个按钮。当点击按钮时,会将另一个可执行程序的widget作为子窗口显示在主窗口中。
注意:为了使这个示例正常运行,需要将另一个可执行程序的源代码编译为可执行文件,并将其命名为"another_executable"
原文地址: https://www.cveoy.top/t/topic/ikOM 著作权归作者所有。请勿转载和采集!