How to add QWidget to a QGLWidget - qt

What is currently the best way to add a QWidget to a QGLWidget as a child? In this case I want to add a QSlider to QGLWidget, however it seems like half the links on Google point to dead information now so it's tricky working out what the current way to achieve this is.
I did try creating a QSlider as a child and setting the geometry, but that didn't seem to do a lot.

Child widgets don't work with a QGLWidget, that's documented.
Use QOpenGLWidget and the child widgets will work fine. There's nothing special to do in this respect, simply add child widgets, layouts, etc., and it "just works".

Related

QWidget's minimumSize vs minimumSizeHint

Qt's widgets have 2 properties determining how small they can be:
minimumSizeHint
minimumSize
Now their basic difference seems clear enough: minimumSize restricts what size the widget can actually have, and Qt won't allow setting the size smaller. It also overrides minimumSizeHint, if set. And then minimumSizeHint is only used by layouts, which won't resize the widget smaller than that when resizing its contents.
Now I was implementing a custom widget, no problems there, and I started wondering about this.
Question: why would you ever want to use mininumSizeHint at all? What is the scenario, where you have to, or want to use it instead of just always using minimumSize? Why does minimumSizeHint exist?
(And if answer is "yeah, sizeHint is enough for everything", that's fine.)
I think the main difference is that minimumSize can be changed by any other object at any time, whether you like it or not, as all QWidgets have a public setMinimumSize() function; whereas your widget alone controls what is returned by minimumSizeHint().
As an implementer of a custom widget, you always want to override minimumSizeHint() if your widget has a minimum size that you think should usually be respected. This is what the built-in Qt widgets do.

Why do custom QWidgets, but not the built-in ones, have nonzero margins?

This is a screenshot of a QFormLayout holding a series of widgets. Note that the top and bottom input widgets are not horizontally aligned to the middle two rows. Note also that the labels on the left are not vertically aligned to the middle two rows.
The difference is that the top and bottom widgets are plain QLineEdit and QTextEdit widgets, the second row is a QWidget with a QHBoxLayout holding a QSpinBox and QDateTimeEdit, and the third is a QStackedLayout containing QWidgets each with a QHBoxLayout and API-supplied widgets inside those.
It looks like these middle two have extra margins. I can (partially) improve the situation by calling QLayout::setContentsMargins on the plain QWidgets' layouts. In fact, in the image above I already have, without that it's worse.
I haven't interfered with styles in the application, it's all system-default. This seems to affect Qt5 on Ubuntu 15.04, I don't think I saw this back when it was a Qt4 application.
I tried setStyleSheet ("QWidget {margin:0;}") in the top-level widget, that introduced all sorts of problems. I also tried the variation setStyleSheet (".QWidget {margin:0;}") but that had no effect. QLayout::setSpacing also had no effect, and setting padding:0 in the stylesheet doesn't fix it either.
Nothing I've tried seems to bring the spacing around these custom-layout QWidgets in line with the API-supplied widgets.
What have I missed? Thanks.
What you observe is both the struggle of multi-platform ui and the simple fact that some widgets are horrible. Given a limited amount of time and a big task, dev teams will not treat fairly the set of features. Powerful widgets and layouts like standard widgets, QGridLayout, will be maintained day and night to be defect free while legacy widgets like QStackedWidget will be pushed at the bottom of the maintenance queue.
I tried to reproduce your issue using designer alone and here is what I got (Qt 4.8) :
What you can see:
The second line with a widget container has great marging
The third line doesn't even show the combo box (I swear it's there!!).
My two cents :
There is a difference in behavior in the second row. Because I used Qt4 and you Qt5, and we both set the QHBoxLayout margin to zero then it means there is a potential regression at play, either in Qt5 or the linux implementation. So this confirms what you suspected.
QStackedWidget is horrible. It is the usual widget provided to give quick and basic functionality rather implementing the real deal. Here it would be a widget with a QStackedLayout. Like I said I suspect those kind of widgets to receive less love when big development efforts are required.
In these situations, use QGridLayout instead of a QStackedLayout of widgets with layout of widget. It will solve the issue of labels not being aligned. Difficult to set up at first but the result is usually neat.
By the way, I switched to a widget with a stackedlayout and here is the result :

Layout problems when switch from QWidget to QGraphicsItem

I'm switching a timeline viewer from QWidget to QGraphicsItem, because QWidget is too heavy, I got performance issues when there are thousand of QWidgets.
I need Layout Management. But I found QGraphicsItem was so light, that it did't support layout. I should use QGraphicsWidget, but there are many useful QGraphicsItem's subclasses, QGraphicsWidget doesn't have them. I think QGraphicsProxyWidget isn't the answer because it is already too slow when I using QWidgets...
So, is there any way to have a layout with any QGraphicsItem? Or is there a better solution?
QGraphicsLayout and QGraphicsLayoutItem are what you are looking for.
http://doc.qt.io/qt-4.8/qgraphicslayout.html
http://doc.qt.io/qt-4.8/qgraphicslayoutitem.html
Hope that helps.

draw qt widget bigger as the mouse hovers over it (overlapping of widgets)

I would like to create a simple effect with my qt gui, but i have no idea how to achieve this.
I have several widgets, that i implemented as subclasses of qwidget. These are part of another widget and live in a layout. When the mouse hovers over these widgets, i want them to appear bigger to highlight the selected one.
This is what i already tried:
Override the paint event, and simply paint it bigger. But then, the other widgets that also live in the same layout overpaint the oversized areas.
I also tried to call the paint function "by hand" from the parent window, to get control over the painting order. But that didnt help either.
I think there has to be a possibility achieving this effect this qt, but i simply dont know how.
Any ideas?
You could either:
create your GUI inside a QGraphicsView, with QGraphicsWidgets and use setScale when the mouse enters or leave the widget, or
use QML.

Scalable painting of a Qt application

I'm writing a simulation of an embedded device's screen (which contains custom widgets on top of a main QWidget), and while the native size of the screen is 800x600, I want to be able to scale it up and down by dragging the window's corner. Without diddling with grid layouts and stretchers (which won't scale the fonts up/down), how do I accomplish this sort-of zoom? I think part of the solution might be to create a QTransform and somehow inject that into the QWidget for the entire application, or its QPaintDevice or QPaintEngine. I'd like to do this without putting QTransform in each custom widget, just the "main window" QWidget.
This is possible if you are using QGraphicsView as your main display widget. QGraphicsScene now supports widgets as content, so you can literally just scale them.
I believe the alternative is to reimplement the paint() for each widget, and manually set the transform/scale before the painting of child widgets.
Bit of a guess here as I've not tried it... but you could try putting the top-level widget into a QGraphicsView then get the QGraphicsView to do the scaling.
You could then enable OpenGL on the QGraphicsView and have it scaled in hardware so it's nice and fast.

Resources