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 Updated to NodeBB v4.3 + New Features

Layout issue with QWidget converted form win32 window

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 640 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 last edited by gaojinhsu
    #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

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • G gaojinhsu

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      0
      • G gaojinhsu

        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

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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
        0
        • Christian EhrlicherC Christian Ehrlicher

          @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 last edited by
          #3

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

          Christian EhrlicherC 1 Reply Last reply
          0
          • G gaojinhsu

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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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
            0
            • G gaojinhsu

              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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              0
              • Christian EhrlicherC Christian Ehrlicher

                @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 last edited by gaojinhsu
                #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
                • JonBJ JonB

                  @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 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

                  • Login

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