0

I have a weird scenario. I create a QMainWindow that is embedded in a QGraphicsScene. I want to have multiple QMainWindows each with a toolbar inside the scene. I'm simulating an MDI Area, without using the QMdiArea class.

Here is the MainWindow.cpp

#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
    resize(1000, 750);
    qApp->setStyle(QStyleFactory::create("Fusion"));

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

    setCentralWidget(view);

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

    widget->setLayout(vLay);

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

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

    testWindow->addToolBar(toolbar);
    vLay->addWidget(testWindow);

    scene->addWidget(widget);
}

What happens is the QToolBar will spawn in the correct location on the embedded QMainWindow, but when it's moved and docked anywhere, it will snap too far up and too far to the left. I've added outliner code to outline the toolbar so you can see the toolbar box.

Here is the MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

class MainWindow : public QMainWindow{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);

};

#endif // MAINWINDOW_H

Why does the toolbar do this weird snapping effect? I added the QMainWindow into a QWidget on purpose, as it's needed for something I'm doing. I realize that embedding just the QMainWindow has the desired interaction, but I need it embedded in the QWidget. I also realize that not having parents are bad for memory management, but I handle that as well.

I'm on Qt Version 5.10.1 and I'm using Redhat as my OS.

  • Might be an issue with the view port offset being calculated incorrectly due to this somewhat strange use of QMainWindow. Is the "too far up and too far to the left." approximately the same as the size of the title bar? – dom0 Nov 08 '18 at 22:22
  • Is there a specific reason to do this, or why not just go for QMdiArea? – dom0 Nov 08 '18 at 22:22
  • @dom0 There is a specific reason to do this, essentially I'm adding my own titlebar to the window to allow for customizability, added buttons and what not. Also, I need to have multiple window selection for the user, the actual program is much much bigger, it's essentially a better QMdiArea. Currently, I see no way to get multi-window selection for QMdiArea and no way to customize the titlebar. Whenever you try to customize the titlebar, it simply ignores the customize options. – JComputerScience Nov 09 '18 at 13:13
  • @dom0 also the offset is the contents margin of the widget qmainwindow is embedded in. I tried setting the content margins to 0,0,0,0 and spacing in layout to 0 and those didn't fix the issue – JComputerScience Nov 09 '18 at 14:13

0 Answers0