Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Layout issue with QWidget converted form win32 window
Forum Update on Monday, May 27th 2025

Layout issue with QWidget converted form win32 window

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 637 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gaojinhsu
    wrote on 5 May 2023, 07:14 last edited by gaojinhsu 5 May 2023, 07:46
    #1

    I have to create a win32 window to render video stream, so I try to convert this win32 window to a QWidget control, Also I want to show some buttons on this win32 window. but the win32 window is always on the top no matter how I configure the layout.

    HINSTANCE hInstance = GetModuleHandle(NULL);
    WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) };
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszClassName = L"RenderWindow";
    
    static ATOM wnd_class_ = RegisterClassExW(&wcex);
    HWND hwnd = ::CreateWindowExW(WS_CHILDWINDOW, L"RenderWindow", L"Engineer", WS_CHILDWINDOW,
            CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
    
    QWindow *native_wnd = QWindow::fromWinId((WId)hwnd);
    QWidget *a = QWidget::createWindowContainer(native_wnd);
    a->setParent(this);
    a->setGeometry(QRect(0, 0, 800, 400));
    
    QPushButton* btn = new QPushButton(this);
    btn->setGeometry(QRect(780, 380, 100, 50));
    btn->raise();
    

    12617969-c9bf-4d7b-bfea-94f3140d80e0-image.png

    C J 2 Replies Last reply 5 May 2023, 07:17
    0
    • G gaojinhsu
      5 May 2023, 07:14

      I have to create a win32 window to render video stream, so I try to convert this win32 window to a QWidget control, Also I want to show some buttons on this win32 window. but the win32 window is always on the top no matter how I configure the layout.

      HINSTANCE hInstance = GetModuleHandle(NULL);
      WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) };
      wcex.cbSize = sizeof(WNDCLASSEX);
      wcex.style = CS_HREDRAW | CS_VREDRAW;
      wcex.lpfnWndProc = WndProc;
      wcex.cbClsExtra = 0;
      wcex.cbWndExtra = 0;
      wcex.hInstance = hInstance;
      wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
      wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
      wcex.lpszClassName = L"RenderWindow";
      
      static ATOM wnd_class_ = RegisterClassExW(&wcex);
      HWND hwnd = ::CreateWindowExW(WS_CHILDWINDOW, L"RenderWindow", L"Engineer", WS_CHILDWINDOW,
              CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
      
      QWindow *native_wnd = QWindow::fromWinId((WId)hwnd);
      QWidget *a = QWidget::createWindowContainer(native_wnd);
      a->setParent(this);
      a->setGeometry(QRect(0, 0, 800, 400));
      
      QPushButton* btn = new QPushButton(this);
      btn->setGeometry(QRect(780, 380, 100, 50));
      btn->raise();
      

      12617969-c9bf-4d7b-bfea-94f3140d80e0-image.png

      J Offline
      J Offline
      JonB
      wrote on 5 May 2023, 07:51 last edited by JonB 5 May 2023, 07:57
      #5

      @gaojinhsu said in Layout issue with QWidget converted form win32 window:

      QWidget::createWindowContainer

      https://doc.qt.io/qt-6/qwidget.html#createWindowContainer

      The window container has a number of known limitations:

      Stacking order; The embedded window will stack on top of the widget hierarchy as an opaque box. The stacking order of multiple overlapping window container instances is undefined.

      BTW https://forum.qt.io/topic/89472/qwidget-createwindowcontainer-how-to-put-this-widget-createdby-this-api-to-stackunder-another-qwidget asked the same question, but was never answered.

      You might try adding Qt::WindowStaysOnTopHint to the QPushButton, but I don't know whether it will make any difference.

      G 1 Reply Last reply 5 May 2023, 13:08
      0
      • G gaojinhsu
        5 May 2023, 07:14

        I have to create a win32 window to render video stream, so I try to convert this win32 window to a QWidget control, Also I want to show some buttons on this win32 window. but the win32 window is always on the top no matter how I configure the layout.

        HINSTANCE hInstance = GetModuleHandle(NULL);
        WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) };
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc = WndProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
        wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wcex.lpszClassName = L"RenderWindow";
        
        static ATOM wnd_class_ = RegisterClassExW(&wcex);
        HWND hwnd = ::CreateWindowExW(WS_CHILDWINDOW, L"RenderWindow", L"Engineer", WS_CHILDWINDOW,
                CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
        
        QWindow *native_wnd = QWindow::fromWinId((WId)hwnd);
        QWidget *a = QWidget::createWindowContainer(native_wnd);
        a->setParent(this);
        a->setGeometry(QRect(0, 0, 800, 400));
        
        QPushButton* btn = new QPushButton(this);
        btn->setGeometry(QRect(780, 380, 100, 50));
        btn->raise();
        

        12617969-c9bf-4d7b-bfea-94f3140d80e0-image.png

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 5 May 2023, 07:17 last edited by
        #2

        @gaojinhsu Don't use hard-coded geometry stuff but use a layout and add your widgers in there.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        G 1 Reply Last reply 5 May 2023, 07:42
        0
        • C Christian Ehrlicher
          5 May 2023, 07:17

          @gaojinhsu Don't use hard-coded geometry stuff but use a layout and add your widgers in there.

          G Offline
          G Offline
          gaojinhsu
          wrote on 5 May 2023, 07:42 last edited by
          #3

          @Christian-Ehrlicher it's just a demo to show how this happens, using layout can't fix it.

          C 1 Reply Last reply 5 May 2023, 07:49
          0
          • G gaojinhsu
            5 May 2023, 07:42

            @Christian-Ehrlicher it's just a demo to show how this happens, using layout can't fix it.

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 5 May 2023, 07:49 last edited by
            #4

            @gaojinhsu said in Layout issue with QWidget converted form win32 window:

            it's just a demo to show how this happens, using layout can't fix it.

            So how should we help when you don't post your original code? Your code you show is missing a layout so it won't work.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            G 1 Reply Last reply 5 May 2023, 13:06
            0
            • G gaojinhsu
              5 May 2023, 07:14

              I have to create a win32 window to render video stream, so I try to convert this win32 window to a QWidget control, Also I want to show some buttons on this win32 window. but the win32 window is always on the top no matter how I configure the layout.

              HINSTANCE hInstance = GetModuleHandle(NULL);
              WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) };
              wcex.cbSize = sizeof(WNDCLASSEX);
              wcex.style = CS_HREDRAW | CS_VREDRAW;
              wcex.lpfnWndProc = WndProc;
              wcex.cbClsExtra = 0;
              wcex.cbWndExtra = 0;
              wcex.hInstance = hInstance;
              wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
              wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
              wcex.lpszClassName = L"RenderWindow";
              
              static ATOM wnd_class_ = RegisterClassExW(&wcex);
              HWND hwnd = ::CreateWindowExW(WS_CHILDWINDOW, L"RenderWindow", L"Engineer", WS_CHILDWINDOW,
                      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
              
              QWindow *native_wnd = QWindow::fromWinId((WId)hwnd);
              QWidget *a = QWidget::createWindowContainer(native_wnd);
              a->setParent(this);
              a->setGeometry(QRect(0, 0, 800, 400));
              
              QPushButton* btn = new QPushButton(this);
              btn->setGeometry(QRect(780, 380, 100, 50));
              btn->raise();
              

              12617969-c9bf-4d7b-bfea-94f3140d80e0-image.png

              J Offline
              J Offline
              JonB
              wrote on 5 May 2023, 07:51 last edited by JonB 5 May 2023, 07:57
              #5

              @gaojinhsu said in Layout issue with QWidget converted form win32 window:

              QWidget::createWindowContainer

              https://doc.qt.io/qt-6/qwidget.html#createWindowContainer

              The window container has a number of known limitations:

              Stacking order; The embedded window will stack on top of the widget hierarchy as an opaque box. The stacking order of multiple overlapping window container instances is undefined.

              BTW https://forum.qt.io/topic/89472/qwidget-createwindowcontainer-how-to-put-this-widget-createdby-this-api-to-stackunder-another-qwidget asked the same question, but was never answered.

              You might try adding Qt::WindowStaysOnTopHint to the QPushButton, but I don't know whether it will make any difference.

              G 1 Reply Last reply 5 May 2023, 13:08
              0
              • C Christian Ehrlicher
                5 May 2023, 07:49

                @gaojinhsu said in Layout issue with QWidget converted form win32 window:

                it's just a demo to show how this happens, using layout can't fix it.

                So how should we help when you don't post your original code? Your code you show is missing a layout so it won't work.

                G Offline
                G Offline
                gaojinhsu
                wrote on 5 May 2023, 13:06 last edited by gaojinhsu 5 May 2023, 13:09
                #6

                @Christian-Ehrlicher Thanks for trying to help, original code is not necessary ,this demo can reproduce this issue, you can create a blank new Qt Widgets Application project,here is the complete code.

                #include "MainWidget.h"
                #include <QHBoxlayout>
                #include <Windows.h>
                #include <qwindow.h>
                #include <qpushbutton.h>
                
                LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
                {
                    switch (message)
                    {
                    case WM_PAINT:
                    {
                        PAINTSTRUCT ps;
                        HDC hdc = BeginPaint(hWnd, &ps);
                        EndPaint(hWnd, &ps);
                    }
                    break;
                    case WM_DESTROY:
                        PostQuitMessage(0);
                        break;
                    default:
                        return DefWindowProc(hWnd, message, wParam, lParam);
                    }
                    return 0;
                }
                
                MainWidget::MainWidget(QWidget *parent)
                    : QWidget(parent)
                {
                    ui.setupUi(this);
                
                    HINSTANCE hInstance = GetModuleHandle(NULL);
                    WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) };
                    wcex.cbSize = sizeof(WNDCLASSEX);
                    wcex.style = CS_HREDRAW | CS_VREDRAW;
                    wcex.lpfnWndProc = WndProc;
                    wcex.cbClsExtra = 0;
                    wcex.cbWndExtra = 0;
                    wcex.hInstance = hInstance;
                    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
                    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
                    wcex.lpszMenuName = L"MyMenu";
                    wcex.lpszClassName = L"RenderWindow";
                
                    static ATOM wnd_class_ = RegisterClassExW(&wcex);
                    qDebug() << "RegisterClassExW failed with " << GetLastError();
                    HWND hwnd = ::CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, L"RenderWindow", L"render", WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
                
                    QWindow* render_wnd = QWindow::fromWinId((WId)hwnd);
                    QWidget* renderWidget = QWidget::createWindowContainer(render_wnd);
                
                    QHBoxLayout* mainLayout = new QHBoxLayout(this);
                    mainLayout->setMargin(0);
                    mainLayout->addWidget(renderWidget);
                    this->setLayout(mainLayout);
                
                    QPushButton* btn = new QPushButton(this);
                    QHBoxLayout* renderLayout = new QHBoxLayout(this);
                    renderLayout->setMargin(0);
                    renderLayout->addWidget(btn);
                    renderWidget->setLayout(renderLayout);
                
                }
                
                MainWidget::~MainWidget(){
                }
                
                
                1 Reply Last reply
                0
                • J JonB
                  5 May 2023, 07:51

                  @gaojinhsu said in Layout issue with QWidget converted form win32 window:

                  QWidget::createWindowContainer

                  https://doc.qt.io/qt-6/qwidget.html#createWindowContainer

                  The window container has a number of known limitations:

                  Stacking order; The embedded window will stack on top of the widget hierarchy as an opaque box. The stacking order of multiple overlapping window container instances is undefined.

                  BTW https://forum.qt.io/topic/89472/qwidget-createwindowcontainer-how-to-put-this-widget-createdby-this-api-to-stackunder-another-qwidget asked the same question, but was never answered.

                  You might try adding Qt::WindowStaysOnTopHint to the QPushButton, but I don't know whether it will make any difference.

                  G Offline
                  G Offline
                  gaojinhsu
                  wrote on 5 May 2023, 13:08 last edited by
                  #7

                  @JonB Sad to hear that. BTW. adding Qt::WindowStaysOnTopHint to the QPushButton doesn't work.

                  1 Reply Last reply
                  0
                  • G gaojinhsu has marked this topic as solved on 6 Jul 2023, 02:57

                  1/7

                  5 May 2023, 07:14

                  • Login

                  • Login or register to search.
                  1 out of 7
                  • First post
                    1/7
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved