Window Templates in Qt (Or basically object oriented Windows) - c++

So, I have a bunch of windows I want to create, each different, but has similar properties. For simplicity, lets just say I want a row of buttons along the bottom (the actual buttons differ for each window, but they should be in the same configuration). Then in the top half of the window, I want it to very based on what window is open.
In short, I would like to have window A which has the template of a window with buttons at the bottom. And then button B which instantiates the buttons, and has it's own thing at the top, and window C which has it's own set of buttons, and thing on the top, which is completely different than B.
Is there any way I can do this in Qt? Also would it be possible to just have one window A, and have it change back and forth between the configuration in B and C when appropriate?

Yes, it can be done with Qt. You can create the widgets dynamically. Design each window as a separate QWidget which will contain other widgets (the buttons, for example). The mainwindow will then contain a simple boxlayout and one of these window widgets created dynamically. When the required window type changes, delete the current window widget and create another one.
Another option would be to put all widgets needed by windows A, B and C to same the window. When the window A is needed, show widgets belonging to it and hide the others.

Actually, if you know anything about QTab, then it works exactly the way you're describing, except it adds a set of tabs at the top which is probably not wanted here.
But anyway... create a QMainWindow inside which you create 3 widgets (A, B, and C). Hide B and C when using A. Then hide A and C when using B, etc.
This is similar to what Roku was proposing, but he had a somewhat different tree organization as he would only show one window and change the widgets in that larger window. Having 3 widgets that cover the entire window and switching between them is probably easier to manage as you in effect have to change only 2 of them when switching from one to the other.

Related

Do I need a top-level sizer on my wxWidgets frame-based app?

I am undergoing a number of tutorials on wxWidgets and found that I must add a wxPanel to my main frame in order to get a Windows native-like background, instead of the ugly dark-grey that it defaults to.
Now, the WxSmith tutorial: Hello world states that a wxBoxSizer is the first control that should be added to the main frame. Then goes the wxPanel, and then another sizer where I'll put my controls (text, buttons, etc).
I'm failing to understand the need for the first sizer. For me it would make more sense to have the following hierarchy: Frame -> Panel -> Sizer -> Controls.
So, do I really need to add a top-level sizer to my frame, before the panel?
Note: The only purpose of the wxPanel is to give my window a light-grey background.
Edit In fact, I only get what I want (light background color for the entire window) if the panel is the frame's first and only child control.
No, you don't need a sizer for the panel because wxFrame automatically resizes its only child to fill its entire client area, just because it's such a common case.
The purpose of wxPanel is not only to give you the "correct" background, but also to provide correct keyboard navigation among your controls. And the reason it is decoupled from wxFrame is that some frames don't contain controls but are used as e.g. canvases, in which case the panel would be unnecessary.
Finally, don't be confused: sizers are not controls, they are associated with a window and, in turn, contain (some of) the window children, but are not controls themselves.

MFC Tab control without tabs?

I'm wanting to make something like a tab control, but without visible tabs at the top.
I would prefer to have the tabs selected from a list or tree at the left hand side of the page, something like this...
Selecting the list/tree item at the left changes everything on the right-hand side of the dialog.
I know I could do this by individually showing/hiding all the fields on the RHS, depending on the selected view, but this is unmanageable to design, when there are at least 10 different designs. C++ doesn't let me design groups and make them visible/invisible in one go. I would prefer to design them as totally separate dialog resources, and then bring them in, like a tab control.
I believe Windows Forms has a ContentControl, which is like a tab control without the tabs, which sounds perfect, but MFC doesn't seem to have this.
Is there a way to do this nicely? Or maybe even a 3rd party control to handle it?
In MFC you would do this by making a child modeless dialog for each group. For each dialog turn off the titlebar style and border style and it will blend in to the parent window instead of looking like a dialog. Create all the dialogs, then use ShowWindow to show/hide one at a time.
Minor detail: Put an invisible control (like a group box) on the parent window to serve as a landmark. When you create each dialog use MoveWindow to position and size it on the landmark.
Use window style WS_EX_CONTROLPARENT in the parent window to help with tab key navigation from parent to child.
Yes you can using DIALOG resources. Set the DIALOG Style to Child, Border to None, Title Bar to False. You can then add implementation classes/files for each DIALOG. The dialogs are then inserted/removed to and fro the parent as a child components by setting the containing window as the dialog's parent (i.e., SetParent)

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.

Qt window resize problem

I am having a problem redrawing a QWidget window after its size has been adjusted. I have tried update(), repaint(), adjustSize(), but all seem to suffer from the same thing: only part of the window is redrawn, resulting in the window frame on the bottom and right sides to not show. The window is also not resized entirely.
Just in case it makes a difference, the window is in a QMdiArea.
Thanks.
// ... some subwidget resizing and moving.
calibrationWindowUIs[activeWindow].layoutWidget2->move(QPoint(oldXLeft, 30 + height + 21));
calibrationWindowUIs[activeWindow].layoutWidget1->move(QPoint(oldXRight, 30 + height + 21));
// Set window size.
calibrationWindows[activeWindow]->setMinimumSize(calibrationWindowUIs[activeWindow].tabWidget->geometry().width() + 40, calibrationWindowUIs[activeWindow].tabWidget->geometry().height() + 40);
calibrationWindows[activeWindow]->update();
Note: I'm new to Qt; perhaps I'm doing something wrong with layouts?
Edit: I may have not given enough information. Alright, to be quite honest, I still have to delve deeper into layouts and related material. What I had tried to do here was to use Qt Designer in order to design the window. I've done what perhaps amounts to a stupid mistake: I didn't use an overall parent layout for the entire window, but hacked it with a couple of smaller layouts that I therefore have to move about and resize individually. See the Qt Designer screen (the red rectangles are the sole layouts): .
What is happening is that in the frame to the right, I am playing a video clip that can be of various resolutions. I want the frame to resize depending on this resolution, which also means that the buttons and window have to move/resize accordingly. That is where the window resize comes in. I'm sure there is a more elegant solution than what I am doing here, but I am trying to handle several other scenarios here and hence the lack of quality of code.
The result is that when I load a clip, the window attempts to resize, but does so badly; the following is the result:
If the window is dragged, it 'pops' into its correct size; in the meantime, however, it just looks ugly.
A couple further questions: do you use the Qt Designer to design your UIs? I found that programmatically you can achieve much better control of your interfaces. One thing which I could not do in the designer was to have a layout parented by the main widget, i.e. the equivalent of having the following bit of code:
QVBoxLayout* layout = new QVBoxLayout;
this->setLayout(layout);
A layout placed in the designer always seems to create this 'layoutWidget' subwidget, which the layout you placed is then parented to. Any way around that?
We use a mix of designer and code to create layouts, the Qt layout system can be very unintuitive at times. But I would probably not layout a full series of tabs in one designer ui file, i would make each tab each own widget and then assemble them either through code or in the designer by promoting to custom classes. This gives you better separation of responsibilities, by putting all the functionality of all the tabs into one file you almost guarantee a large unwieldy class.
When a widget has child widgets in designer you can assign a layout to it by adding it from the context menu. Make sure nothing is selected and click on the background of the widget in which you want to create a layout, select the layout and all of the widgets children will be assigned the layout.
What does help is creating hierarchies of layouts. Looking at your first screenshot, i would probably use a vertical layout with spacers on top and bottom for the items on the right, an horizontal layout with spacers left and right for the button bar and a grid layout for all the items together. Without the spacers your items will extend when the window grows. The spacers will let you control the behavior under resizing better.
you are calling setMinimumSize(). That's fine, but you should also call resize()

Unable to get height/width of a widget - gtkmm

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.