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. Qt6. Embedding native widgets in QTabWidget/QStackedWidget breaks their geometry
Forum Updated to NodeBB v4.3 + New Features

Qt6. Embedding native widgets in QTabWidget/QStackedWidget breaks their geometry

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 141 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Nikita Grigorev
    wrote last edited by
    #1

    Qt 6.9.2, Windows.

    I put a native widget (made native with winId()) into a QTabWidget, which is a child of some root QWidget.
    It works OK, but if I add more widgets, their geometry breaks.
    The next widgets have regions on the right and bottom sides that are rendered with random garbage.
    I have this problem on Qt 6.9.2, but the same code works fine on Qt 5.12.3.

    Here's a minimal reproduction:

    #include <QApplication>
    #include <QPushButton>
    #include <QTabWidget>
    #include <QStackedWidget>
    #include <QVBoxLayout>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
    
        auto button = new QPushButton("buton");
        button->resize(200, 200);
        button->show();
    
        auto window = new QWidget{};
        auto main_layout = new QVBoxLayout(window);
        window->resize(500,500);
        window->show();
    
        QObject::connect(button, &QPushButton::clicked, [&]() {
            auto widget = new QWidget();
            widget->setStyleSheet("QWidget { background-color: #119911; }");
            widget->winId(); // make it native
    
            auto tab_widget = new QTabWidget(window);
            // tab_widget->winId(); // fixes the problem
            tab_widget->addTab(widget, "mytab");
    
            main_layout->addWidget(tab_widget);
        });
    
        return QApplication::exec();
    }
    

    qt6 native bug.png

    I fixed the problem by making QTabWidget native first.
    I could also use Qt::WA_DontCreateNativeAncestors, but I ran into other problems with scroll areas, so I can't use this attribute.

    It looks like it has something to do with the window frame. The size of the garbage area suspiciously matches the height of the window frame.
    If I use the Qt::FramelessWindowHint window flag for the widget, it fixes the problem in the code above. But it doesn’t help in my actual code, where the widget hierarchy is more complicated.

    I inspected the native windows of the widget and its parent with Spy++: the rectangles don't match, even though the widgets have the same geometry in GammaRay. So the native windows and Qt widgets got out of sync for some reason.

    qt6 native bug 2.png

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IgKh
      wrote last edited by
      #2

      Welcome to the forum.

      What you describe is clearly a bug - whether a Qt widget has a backing HWND or not shouldn't matter. FWIW, you example shows the same problem for me on Linux/Wayland with Qt 6.9.2 but not with the latest dev branch. So it was a bug of some sort - affecting more than just Windows - but already fixed at some point.

      Can you try with 6.9.3 or with the latest 6.10.0 beta?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        IgKh
        wrote last edited by
        #3

        Addition: you are probably seeing a variation of https://bugreports.qt.io/browse/QTBUG-139132. There is no fix attached to that ticket, possibly something else fixed it "by mistake".

        1 Reply Last reply
        0
        • N Offline
          N Offline
          Nikita Grigorev
          wrote last edited by
          #4

          Tested with Qt 6.10.0-0-202510021201, the same problem

          1 Reply Last reply
          0

          • Login

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