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:

Qt Code:
  1. #include <QApplication>
  2. #include <QPushButton>
  3. #include <QTabWidget>
  4. #include <QStackedWidget>
  5. #include <QVBoxLayout>
  6.  
  7. int main(int argc, char *argv[]) {
  8. QApplication a(argc, argv);
  9.  
  10. auto button = new QPushButton("buton");
  11. button->resize(200, 200);
  12. button->show();
  13.  
  14. auto window = new QWidget{};
  15. auto main_layout = new QVBoxLayout(window);
  16. window->resize(500,500);
  17. window->show();
  18.  
  19. QObject::connect(button, &QPushButton::clicked, [&]() {
  20. auto widget = new QWidget();
  21. widget->setStyleSheet("QWidget { background-color: #119911; }");
  22. widget->winId(); // make it native
  23.  
  24. auto tab_widget = new QTabWidget(window);
  25. // tab_widget->winId(); // fixes the problem
  26. tab_widget->addTab(widget, "mytab");
  27.  
  28. main_layout->addWidget(tab_widget);
  29. });
  30.  
  31. return QApplication::exec();
  32. }
To copy to clipboard, switch view to plain text mode 

qt6 native bug.jpg

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