Is it possible to use QGraphicsLayout in an application with a QMainWindow?
-
What I was hoping to do was have a standard QMainWindow class with menus, toolbar, plus various widgets in a layout, one of which would be a graphics view showing a graphics widget that contained a graphics layout. But whenever I put the graphics view into a main window nothing gets displayed.
As a test I took the working code provided in the Qt Basic Graphics Layouts Example, created a QMainWindow class in main, and moved the QGraphicsScene, Window and QGraphicsView creation to the main window class.
I tested the main window class on its own, and widgets like a line edit show up fine. But the code below no longer works when in the main window class.
QGraphicsScene scene; Window *window = new Window; scene.addItem(window); QGraphicsView view(&scene); view.resize(600, 600); view.show();
I just get a blank area. If don't add the Window widget, but instead, for example, draw an ellipse that is visible. If I add a plain QGraphicsWidget with a background colour that is also visible. It is just when things are inside a layout on a graphics widget that I get nothing. I've been searching for answers, digging into the documentation and even looking through the Qt source to see if I can figure out if what I am trying to do is even possible, but without any luck.
-
What I was hoping to do was have a standard QMainWindow class with menus, toolbar, plus various widgets in a layout, one of which would be a graphics view showing a graphics widget that contained a graphics layout. But whenever I put the graphics view into a main window nothing gets displayed.
As a test I took the working code provided in the Qt Basic Graphics Layouts Example, created a QMainWindow class in main, and moved the QGraphicsScene, Window and QGraphicsView creation to the main window class.
I tested the main window class on its own, and widgets like a line edit show up fine. But the code below no longer works when in the main window class.
QGraphicsScene scene; Window *window = new Window; scene.addItem(window); QGraphicsView view(&scene); view.resize(600, 600); view.show();
I just get a blank area. If don't add the Window widget, but instead, for example, draw an ellipse that is visible. If I add a plain QGraphicsWidget with a background colour that is also visible. It is just when things are inside a layout on a graphics widget that I get nothing. I've been searching for answers, digging into the documentation and even looking through the Qt source to see if I can figure out if what I am trying to do is even possible, but without any luck.
@barrian Why are you adding that Window to the scene as an item?
Normally you add the QGraphicsView to the Window, the the scene to the view, then the graphics item to the scene.
Then the items get displayed by the scene in the view on the window. Which might be in a layout on the main window or something. That much is not clear from your code snippet though. -
Hi
Did you try with
http://doc.qt.io/qt-5/qgraphicsproxywidget.html#detailsI have not tried with complete mainwindow, though. but why not.
-
Hi
Did you try with
http://doc.qt.io/qt-5/qgraphicsproxywidget.html#detailsI have not tried with complete mainwindow, though. but why not.
@kenchan Window is a QGraphicsWidget, so it should go into the scene. I am modifying the code from Basic Graphics Layouts Example. So it is a view showing a scene containing a graphics widget containing a variety of layouts. Once the view is inside a main window nothing is displayed on the screen. If there is no graphics layout inside the graphics widget then everything displays, but I have to do the layout myself.
@mrjj I was trying to avoid using standard QWidgets inside the graphics scene due to the performance impact.
-
@kenchan Window is a QGraphicsWidget, so it should go into the scene. I am modifying the code from Basic Graphics Layouts Example. So it is a view showing a scene containing a graphics widget containing a variety of layouts. Once the view is inside a main window nothing is displayed on the screen. If there is no graphics layout inside the graphics widget then everything displays, but I have to do the layout myself.
@mrjj I was trying to avoid using standard QWidgets inside the graphics scene due to the performance impact.
-
@barrian
Hi
I read it reverse then :)Why do you want to use QGraphicsLayout when you can just use the normal layouts?
@mrjj I have a scene with items I want to lay out, and I want to have the view of that scene in a layout. Something like the Qt Diagram Scene Example, but with a layout inside the scene as well.
It has been pointed out to me that in my haste to modify the Basic Graphics Layouts Example I left everything allocated on the stack. Heap allocation solved my problem in modifying the example. From that working modified example I can figure out where I went wrong with my own application, where I was already using heap allocation for everything, but must have been doing something else wrong.