Qt - What should I use to implement multiple windows? - c++

I've started learning Qt (C++) and I'm overwhelmed by the whole lot of component it has. I'm writing my first application and here's what it should look like:
When I run it, I have one main widget which has 8 QPushButtons.
When I click any of the buttons a 'window' should open and it should
contain about 25-40 various labels, buttons, checkboxes, radio
buttons and so on. So, I need 8 'windows'.
My question is: what should I use to implement these 'windows' - Widgets, Dialogs, MainWindows? What is like a conventional QT way of solving this problem?

It depends on how you want your application to look like.
If you want to have multible 'Windows' like different Tabs in your application then use QStackedWidget Class or QTabWidget Class.
If you need something like a Pop-Up you could use a QDialog.
Please make sure you understand how QWidget works and always take a look in the Qt Documentation.

Related

Hide Page/Tab from QTWidget - QT 5.5

i need to hide tabs from a existing project in QT, i don't want to delete the code because i have to set parameters on that code, the Application relay on that too. Seems like QT hasn't built-in hide(); function, i tried to edit stylesheet to make it smaller, but doesn't work too, i've looked on the internet and seems like this is a known issue. Does somebody have some tricks to avoid this?
Only thing i was able to come up with is:
ui->TabObject->setEnabled(false);
basically i disable objects in the tab to make them not usable by the user, but this is not a good thing for the whole UI.
Maybe by calling QTabWidget::removeTab(index) - this removes the tab from the QTabWidget, but does not delete the tab's QWidget.

Qt: How can I use my widgets created with code in the *.ui file

I have a project in Qt made with the QWizard and QWizardPage classes. There are two ways to create a widget i.e: a Label:
One is going to the *.ui file and search the element and put it where you want (visual way). Then you can access it on your code with ui->nameOfLabel.
The other one is going to your code and creating it like QLabel codedLabel;
Actually I'm using the second way (it's easier for me to create, show and use) but my question is: Is there any way that I can see my label codedLabel on the *.ui file?
I would like to move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way (but having the label created in the code instead of the ui).
Thank you so much.
Widgets created at runtime from your source code and being added to a widget as child CANNOT be seen in Qt Designer when you edit the .ui file of the widget they will be added to.
However, there could be an alternative (reading what you are trying to achieve: having some child widgets being present or not based on the context):
Create the widget from the .ui within Qt Designer and hide it (QWidget::hide()) or even remove it (QLayout::removeWidget()) programmatically if not needed at runtime.
If the real reason why you want to see it is because you want to "move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way". Then I recommend that you simply create an empty QWidget (or QLayout) in Qt Designer (graphically: easy to place where you want to) and later (programmatically) add your QLabel to it (rather than adding it to the main top-level widget): then, it goes in the place you determined from Qt Designer tool.
You should not need any complex code to programmatically display your QLabel in a specific place, just choose the right parent to have it be displayed in the right place!

Creating tabbed document interfaces in Qt Designer?

I am trying to write a program that will use a tabbed document interface (TDI) like seen in Notepad++ or most web browsers. I know how to build GUIs using Qt Designer, and code in Qt C++ (after a few days of playing around).
I have created an example of what each page widget will look like using Designer, and now I want to add the ability to create and testroy tabs at runtime, each containing a unique instance of the page widget. However, I have no idea how to do this without adding a class that extends QWidget, and building the page widget with code. I could go down this route, but I'm sure there must be a better way of creating a TDI; but I can't find any tutorials or examples of how to do this.
Does anyone have any suggestions?
For creating tab interfaces you should look into QTabWidget.
It is a container widget included in Qt Designer which automatically handles operations on tabs. It has several build in methods for manipulating its tabs and theirs contents.
Each page of QTabWidget is handled separately and can have different layouts and functionality.
If you want to include several different objects to one page assign a layout to it and then assign the objects to the layout.

How to display list of opened tabs in Qt 4.8 on Ctrl+Tab

Is there any straightforward (without listening to key events) way to display the list of opened tabs in Qt, like following (in Visual Studio). I just want to make sure Qt does not offer such a feature, before implementing such a widget by myself.
At least in a primitive way like in NotePad++ (meant no offense). Now, when I am using QTabBar and press Ctrl+Tab it shows something like this,
And I don't know actually what is that !
The default behaviour of ctrl-tab is to cycle through the tabs. You will need to customize that to display your own dialog. You don't need to connect to any signals to do this, simply pass a reference to the tab widget/bar into the dialog. You can then use that reference to retrieve the names and to change the tab.

Making a game in Qt regarding GUI windows

I've been wanting to program a simple game with a simple GUI using Qt (Its will be a VERY simple game, nothing fancy). What I've been wondering is, how can I create multiple windows and display them when needed? For an example, a battle screen and an inventory screen. The user should only see one of them, but should be able to access the other one when needed. I was using stacked widget but I'm not sure if that's the proper way. Also, is it better to design the windows in the designer or to code them?
A StackWidget certainly would accomplish what you want to do. The reason why it is not always used for this kind of thing, is that it all the screens are pre-created at the beginning and always exist. This means it takes a little longer to initialize, and you are using more resources than you need at any one time
But as you are saying, if this is a simple game, then I don't see a big problem with it. Just as easily, you could also, create an empty layout and swap the inventory and game panels as needed.
Add my vote to everyone else suggesting to use the designer. It is so much easier to manipulate layouts, actions, and such using the designer then through code.
You can see the Designer manual here
So this is what I would suggest:
Create your "battleScreen.ui" - which is the designer file for your battle screen and everything in it, and then create your "inventory.ui". Both of these could be QWidgets, or QFrames, or whatever makes sense.
Then create your "Game.ui" which will be your QMainWindow.
In your Game main window, you can then add your QStackWidget, and place your inventory, and battle screens in the stack widget.
If you don't know how to do that...
1) drag a QWidget into your form (into the stack widget)
2) select the new QWidget and right-click.
3) Select "Promote to..."
4) Fill out the information to promote the QWidget to your inventory class
Promoted Class Name: The name of your inventory class
Header File: The header file of your inventory class
5) Click add
6) Click Promote.
Hope that helps.
Since I'm not sure what your goals are I can't advise whether or not the stacked widget is appropriate but I think you can accomplish quite a lot using the designer and style sheets. If you need to code some parts of the GUI, you can always drop in a place holder widget and either replace it with coded items or make them children of the place holders.
A general answer for a general question:
Use the Designer to create your windows; hide and show the auxiliary windows as needed.
Use a flow manager class to manage the visibility of a related set of windows.
The stacked widget is useful for managing a button/icon whose appearance changes based on state; the different representations live in the stack.