简体   繁体   中英

X11 window does not put into QWidget for embedding in a Qt application

There is a Qt application. GL-window created into this application by calling XCreateWindow function and I can't edit it. I need put Xwindow in QWidget inside my Qt applications.

In the documentation:

void QWidget::create ( WId window = 0, bool initializeWindow = true, 
    bool destroyOldWindow = true ) [protected]

Creates a new widget window if the window is 0, otherwise sets the widget ' s window to window.Initializes the window sets the geometry etc.) if initializeWindow is true. If initializeWindow is false, no initialization is performed. This parameter only makes sense if window is a valid window.

...

For verifying the result of function QWidget::create there is the following code:

class ParentWindow : public QWidget
{
  Q_OBJECT

  public:
  ParentWindow(WId id)
  {
     create(id);
  }
};

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QPushButton* button = new QPushButton("MEGA BUTTON");
  button->show();
  ParentWindow w(button->winId());
  w.show();

  return a.exec();
}

When application start a single blank window appears. Although expected window containing a button (or to be a button). How can I put X11 window into my QWidget?

The problem was resolved:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Display* display = XOpenDisplay(NULL);

    XSynchronize(display, True);
    XSetErrorHandler(myErrorHandler);

    Window x11root = XDefaultRootWindow(display);

    int x = 500;
    int y = 500;
    unsigned int width = 150;
    unsigned int height = 150;
    unsigned int borderWidth = 0;
    long colorBlue = 0xff0000ff;

    Window x11w = XCreateSimpleWindow(display, x11root, x, y, 
        width, height, borderWidth, 1 /*magic number*/, colorBlue);

    QWidget w;
    w.resize(300, 300);
    w.show();

    XReparentWindow(display, x11w, w.winId(), 0, 0);
    XMapWindow(display, x11w); // must be performed after XReparentWindow, 
                               // otherwise the window is not visible.

    return a.exec();
}

To solve the problem through a widget ParentWindow failed - xwindow is embedded in QWidget, but have problems with resizing the window and closing it (it doesn't close).

QX11EmbedContainer可能就是您所需要的。

Let Qt create your Window and then use the Qt X11 Drawable with your X11/GL code.

With OpenGL and Qt you must use the Qt OpenGL context, if Qt is rendering using OpenGL. Just be aware that Qt expects the OpenGL state to be put back to what it was when it last used it.

You can get access to the Drawable using QX11Info (also check Compiler does not see QX11Info as this covers a common problem when including X11 with Qt).

The way Qt provides access to both X11 and OpenGL seems to change between major and minor versions so you may need to do a little bit of digging.

I know that the above works with Qt5.1 upto 5.5. Qt5.6 has problems with this approach that I've not yet resolved.

You should not touch window IDs in your first Qt program. Window IDs are a low level concept and a Qt programmer normally needs them only to do something outside of the Qt framework. Managing widgets as children of other widgets is not that kind of task.

I recommend you start with one of the tutorials . Look in particular here to see how you make a widget a child of another widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM