Qt在Qt gridLayout中启动一个外部程序

时间:2016-09-22 10:08:17

标签: linux qt qgridlayout

我想知道,是否可以通过Qt启动外部程序并在Qt gridLayout(或Qt窗口内)中显示程序?

目前,我可以通过Qt启动外部程序,但我还没有找到在QtWindow中显示程序的方法。换句话说,程序只出现在Qt窗口之外。

Qt Pro file
#-------------------------------------------------
#
# Project created by QtCreator 2016-09-21T16:31:30
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = WifiProject
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork/QNetworkConfigurationManager>
#include <QtNetwork/QNetworkSession>
#include <QtNetwork/QNetworkInterface>
#include <QDebug>
#include <QList>

#include <QProcess>
#include <QWidget>
#include <QBoxLayout>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_StartWicd_clicked();

private:
    Ui::MainWindow *ui;
    void searchForNetwork();
    QProcess *wicdProgram;

    void addWicdProgram();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWindow>


QString program = "/usr/bin/wicd-gtk";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    searchForNetwork();
    addWicdProgram();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::searchForNetwork(){

}

void MainWindow::addWicdProgram(){
    wicdProgram = new QProcess(this);
    wicdProgram->setProgram(program);


    WId winid = this->winId();
    QWindow *container = QWindow::fromWinId(winid);
    QWidget *program_start = createWindowContainer(container);
    setCentralWidget(program_start);


    ui->wifiGridLayout->addWidget(program_start);
    wicdProgram->start();


    qDebug()<<"wicd addded";
    //ui->wifiGridLayout->addWidget(program_start);
}

void MainWindow::on_StartWicd_clicked()
{
    //wicdProgram->start(program);
    qDebug()<<"the wicd should have started";
    wicdProgram->terminate();


}

1 个答案:

答案 0 :(得分:1)

解决方案是通过qt检索应用程序启动的窗口ID,您可以这样做:

QWindow *window = QWindow::fromWinId("Id of the application");
window->setFlags(Qt::FramelessWindowHint);
QWidget *widget = QWidget::createWindowContainer(window);