Qt QMainWindow's Draggable Area Issue with QToolBar

I'm facing a strange issue when trying to add a QMainWindow to a QGraphicsScene. For all intents and purposes, please do not recommend me to use the QMdiArea as the alternative, because that distracts away from the actual issue I am having and it does not fit my needs.

Here is the minimal example that exemplifies 2 key issues, both of which I believe to possibly be Qt bugs.



#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QToolBar>
#include <QVBoxLayout>

class MainWindow : public QMainWindow{

    MainWindow(QWidget *parent = 0);


#endif // MAINWINDOW_H


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
    resize(1000, 750);

    QGraphicsView* view = new QGraphicsView;
    QGraphicsScene* scene = new QGraphicsScene;
    view->setFixedSize(1000, 750);
    view->scene()->setSceneRect(-150, -150, view->size().width(), view->size().height());


    QWidget* widget = new QWidget;
    widget->resize(300, 300);
    QVBoxLayout* vLay = new QVBoxLayout;


    QMainWindow* testWindow = new QMainWindow;
    testWindow->resize(300, 300);

    QToolBar* toolbar = new QToolBar;
    toolbar->setStyleSheet("border: 1px solid red"); //For better seeing the issue



Now, you may be scratching your head and wondering why the heck I'm even doing this...

tl;dr: I'm creating a custom mdi area within the QGraphicsView with a custom Dock and layering manager. I need each subwindow to have a dockable QToolBar, much like a QMainWindow, but only the QMainWindow supports this feature out of the box. Each subwindow is the overarching QWidget in my example above.

Before converting the "inside" contents of my subwindow to a QMainWindow, everything was working fine, and even as a QMainWindow, everything is still fine. However, once I add the QToolBar to the QMainWindow everything begins to go wrong.

You will notice upon start up that the program looks correct and that the QToolBar is correctly placed and offset. Now if you try to drag the QToolBar anywhere at all, even outside of the QMainWindow and perform a mouse key release, one of two things will happen:

  1. The program will crash and have a very strange stack trace while running on my current system: Qt 5.10.1 with gcc 5.2.0 on REHL

  2. The QToolBar will incorrectly snap to the wrong location within the QMainWindow. Look closely at the predefined rectangular area when dragging and hovering over a valid dockable location and use the red border as a guide.

The crashes are inconsistent and sometimes happen every second drag event after the first. Sometimes it happens the moment you grab the QToolBar and other times it happens after the drop event. I'd add the stack trace, but every single time the debugger has a different output. I have seen some messages regarding nVidia.so libraries such as these:

7fd6d96c1000-7fd6daf36000 r-xp 00000000 fd:00 34442271                   /usr/lib64/libnvidia-glcore.so.390.67
7fd6daf36000-7fd6db135000 ---p 01875000 fd:00 34442271                   /usr/lib64/libnvidia-glcore.so.390.67
7fd6db135000-7fd6db4a9000 rw-p 01874000 fd:00 34442271                   /usr/lib64/libnvidia-glcore.so.390.67
7fd6db4a9000-7fd6db4c2000 rw-p 00000000 00:00 0
7fd6db4c2000-7fd6db5d3000 r-xp 00000000 fd:00 35871194                   /usr/lib64/libGLX_nvidia.so.390.67
7fd6db5d3000-7fd6db7d3000 ---p 00111000 fd:00 35871194                   /usr/lib64/libGLX_nvidia.so.390.67
7fd6db7d3000-7fd6db7f8000 rw-p 00111000 fd:00 35871194                   /usr/lib64/libGLX_nvidia.so.390.67
7fd6db7f8000-7fd6db7ff000 rw-p 00000000 00:00 0
7fd6db7ff000-7fd6db800000 ---p 00000000 00:00 0
7fd6db800000-7fd6dc000000 rw-p 00000000 00:00 0                          [stack:30187]
7fd6dc000000-7fd6dc021000 rw-p 00000000 00:00 0
7fd6dc021000-7fd6e0000000 ---p 00000000 00:00 0
7fd6e00aa000-7fd6e00ad000 r-xp 00000000 fd:00 3060689                    /usr/lib64/tls/libnvidia-tls.so.390.67
7fd6e00ad000-7fd6e02ad000 ---p 00003000 fd:00 3060689                    /usr/lib64/tls/libnvidia-tls.so.390.67
7fd6e02ad000-7fd6e02ae000 rw-p 00003000 fd:00 3060689                    /usr/lib64/tls/libnvidia-tls.so.390.67

The debugger may have over 50 greyed-out function callbacks, sometimes the same, sometimes different. Notably I've seen the QPropertyAnimation mixed in those callbacks, but not every time, so I can't pinpoint it and I feel like I'm going mad.

Now, you might think that this has to do with the QMainWindow being embedded as a QGraphicsProxyWidget within the QGraphicsScene and just having a side effect from that... but no. If you instead add just the QMainWindow to the scene directly and bypass using the QWidget that contained it, then everything works fine. Snapping works with no offset issues and there are no more crashes. Due to this interaction I'm almost convinced it is a Qt bug and would like to submit a bug report using this post as a base. If I'm missing something or need to update my graphics drivers, I will. However, it doesn't explain why the snapping does not match the QMainWindow's rubber band rectangle placement when you're dragging.