Unable to get height/width of a widget - gtkmm - c++

I'm writing an application using gtkmm.
I wrote a simple widget class, that I want to display in the application's main window only in some cases. Otherwise, I would like a Label "disabled" to be visible.
To achieve that I packed both the widget and the label into one VBox, and I show() and hide() them in order to swap them.
However, the custom widget is far larger than the label, so I need to resize the label a bit.
I know I can use label.set_size_request(x,y), and it works, when I type the dimensions manually. But I am aware, that the widget may differ in size a bit, depending on the Gtk theme one uses, etc.
I'd love to set label exactly the same size *as the widget has. However, using widget.get_height()* does not work - it always returns 1.
Maybe a clue will be that I do this from within the main window's constructor.

I haven't done much in gtkmm, more in pygtk. But I think to get meaningful values from get_width/get_height, a widget must be realized, which is an X-window speak for visible. And I guess your widgets are not yet visible in window constructor.
I'd propose you to use a gtk.Notebook instead, with its tabs hidden. Then gtk.Notebook will control size of both your label and your custom widget.

Related

Qt correct way to set z or z-index (Programmatically)

I want to ensure that objects are layer correctly, I've read online that the way to achieve this is by setting the z-index style.
However so far I haven't been able to do this, I want to set the style programmatically.
I've tried:
pobjWidget->setStyleSheet("z-index: 5");
and
pobjWidget->setStyleSheet("z: 5");
Both of these result in "Unknown property z-index" or "Unknown property z" being displayed in the console.
pobjWidget is an a pointer to a QWidget.
The reason I need this is that I'm rendering widgets with a transparent background onto live video widgets and I need to ensure that the widgets on top do not flicker, this is an attempt to illuminate the flicker.
I assume you have a handle to every widget you want to order. If raise and lower are too crude for you, you can use
void QWidget::stackUnder(QWidget *w)
Quoting from the docs: "Places the widget under w in the parent widget's stack.
To make this work, the widget itself and w must be siblings."
The important bit is that they must be siblings, i.e. need to have the same QObject as parent.

How to let user vertically resize window

I want the user to be able to vertically resize the Window but I'm unsure how I would do this. I have selected my root object (View of type QWidget) in Qt Creator but I do not see an option to allow users to vertically resize the object. Does this have to be done through code?
By default, if a QWidget is the top-level window you are able to resize it given that the minimumSize and maximumSize are different since they indicate the range of resizing.
If you want to let the user to resize vertically only, then you just have to set both minimumWidth and maximumWidth to the same value (probably to the current width of your QWidget). Qt will take care of indicating the underlying windows manager the rest.
You can do it in the Designer or programmatically using the setMinimumWidth and setMaximumWidth methods. Edit: As mentioned in a comment, there is a setFixedWidth method that simplifies this operation (and make it more explicit in your code).
Of course, you can play with the combinations such as setting a minimum width (or height, or both -minimum size-) to avoid your top-level window to collapse and become unusable, setting a maximum size... One common setting is making minimumSize = maximumSize, so you make the window fixed size (you can use the convenience method setFixedSize).
PS: see that this has nothing to do with the sizePolicy, which simply indicates parent layout which actions can be taken -regarding size- when placing the widget. As a top-level window has no parent layout this policy is simply not used.

QWidget Transformation

I am writing an application using Qt and would like to have a "Metro Style" interface. Everything is finished except I don't know how to make the widget appear and disappear. For instance, in WPF you can animate (UIElement.RenderTransform).(TranslateTransform.Y) so that Y becomes zero or negative. How can I move the QWidget in a QGridLayout so that it can hide?
Example:
After doing some research I figured out a way to do this. Instead of letting Qt do the layout I simply handled it myself via move and set width/height functions. Overriding resizeEvent made it so I could update the values in case the window was resized. Additionally I used setMask to ensure that the widget did not leak over to unwanted locations in the UI.

Qt - dynamically add QLineEdit in a panel

this is a question for programming with Qt/C++. I have a combo box with two items. If current index for selection is 0, then no QLineEdit should be displayed in layout below the combo box. If it is 1, a QLineEdit should appear. It should disappear again if index is 0 again.
Notably, other elements in the layout should not be affected by the change. Values already entered by user in other QineEdit should remain in place.
Is it possible to dynamically modify widget? How did you procede?
Kind regards.
All QWidget objects have a function called hide().
You can attach a signal to the currentIndexChanged signal of the combo box, and in that function you implement whatever logic you have in mind and invoke the method hide of your QLineEdit.
The only problem with this approach is that a Qt Widget, when hidden, doesn't occupy any space on the screen, and this can lead to layout changes (depending on how you've programmed your layout, some other widgets can move a bit, for example). To prevent that you can make another Widget appear where the QLineEdit were (perhaps invoking the show() function, and placing the 'placeholder' on the same container that the LineEdit was), only to occupy its space and keep it there, or you can use a QStackedWidget add the two Widgets there and change its index.
I would recommend that you read the following example, it has some useful insight on dynamically changing things: Qt Extension Example.
Also, when in doubt, take a look in the other examples, they are really well documented and cover a lot of important topics on Qt.
Good luck with your code :)

Enlarge a Qt widget so it might cover other widgets

I have a complex layout of widgets in widgets in widgets in a QMainWindow. In one of them I have an image, it sits in the corner. What I would like to achieve is following: if the image is activated (e.g. clicked upon), it should be enlarged, so it might overlap other widgets, or parts of other widgets. The problem is, I still would like it to remain in the layout, but in a way that everything else remains in its original size and position.
I was thinking about having an empty but similar size widget as a "placeholder", and have the actual resizable widget float on top of it. My problem is, that it does not guarantee that it stays in its position if the main window is resized, maximized, etc. Is there a better or more efficient way to do it?
One way to do it, if the widgets to be overlapped are in the same layout than the one you want to enlarge, and the policies for that widget allow it, is just .setVisible(false) in the other widgets. The widget that remains visible should resize to cover all the available area!
If I can't find a better solution, I think I'll do the following:
The MainWindow will have no layout, just two QWidgets on top of each other. The bottom one will contain all the layouts and everything else, while the upper one will have a transparent background and the resizable widget, maybe supported with a number of spacers.