Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

c++ - QT 5.5 embed external application into QWidget

I'm interested in embedding an external application inside of my QT 5.5 Widget based application. I'm only concerned with it working on Linux. I'm using CentOS 7 with GNOME.

This is the code I have tried:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QWindow *window = QWindow::fromWinId(125829124);
    QWidget *widget = QWidget::createWindowContainer(window);
    widget->setParent(this);
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(widget);
    this->setLayout(layout);
}

In this example I'm getting the WinId separately and just hard-coding the value for testing. The application to be embedded is running.

When I execute my Application it runs with no errors. And the application to be embedded changes screen position and resizes, however it does not embed inside my application. It is still a separate window. If I kill my application, the embedded application is killed as well.

So is there a way to actually embed the application inside of my application?

*************** UPDATE ****************

Something interesting I just uncovered. When I run my application (container application) the second application (the one I want embedded) remains an independent Window outside of my application. However if I resize my application window (click the lower right corner to resize the window) the second application (to be embedded) resizes as well, but remains an independent Window outside of my container application.

Even more interesting is that if I kill my application, both applications "disappear" from the desktop. However System Monitor shows the second application (the one I want embedded) is still running (however with no GUI). Now if I launch my application again the second application is in fact embedded in my container application, just the way I would like!

So I guess I have to figure out why killing my application and then relaunching it embeds the second application correctly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following achieves the desired result, the key was adding the FramelessWindowHint:

QWindow *window = QWindow::fromWinId(211812356);
window->setFlags(Qt::FramelessWindowHint);

QWidget *widget = QWidget::createWindowContainer(window);

QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(widget);
this->setLayout(layout);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...