Creating tabbed document interfaces in Qt Designer? - c++

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.

Related

Qt QWidget multiple instances

We are multiple people working on one Qt app. I for one am implementing a library, which will be instanciated in multiple other parts of the app. This library has a display class+form along with it.
Until now I had simply created one single instance of the library, running on a dummy, and passed debug data to one instance of the display+form, and worked like that.
But now that core debugging is finished, goal is to have everything instantiable - not just the core library code, but also the form itself, and embed that form into other displays. Each caller/user would be reponsible for passing output data of the core library instance they are using to an instance of the form. Each instance of the form would separately display the information generated by a specific library instance, possibly with different display options - they are all independent.
Similarly, it is possible to enter values in my display. Goal is to be able to enter different values in different displays instantiated accross the app, and sending those to specific instances (caller's responsiblity).
Question is of course : how to do that ? Internet talks about promoting, but I still don't see anywhere in Qt Designer where to include so-called promoted objects in other objects.
TL;DR : : I want some existing form to appear in the menu on the left in Qt Designer to be able to instantiate it multiple times in other forms. How to do this ?
Thanks in advance for the help !
Charles
You can promote any QWidget to your control from within Qt Designer. Add a QWidget, right-click and promote.
Ideally, you should create a designer plugin for your control, make the relevant properties designable, and build the plugin as well as the library. That way you'll be able to drag your control from the palette, and it will behave like the real thing.
Qt QWidget multiple instances
You answered yourself. Qt Creator: "File->New->Qt->Qt Designer Form Class" with QWidget as a base class will suit you. Then you can promote a simple QWidget in the UI into this custom widget, to create an instance. Each instance will manage its own UI.

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!

Common ui base for dialogs in Qt

In Qt widget application, I'd like to have a common base view for all my dialogs, so that I could inherit other classes from it.
This "base/common" view would contain initially a set of buttons at the bottom and a custom frame with data at the top. A place in the middle would be used by derived classes for placing view-specific contents. If the common dialog style changes in the future, changes will be applied in one class only.
Is there any way to use such approach in Qt, since ui files are processed with 'uic' to create classes? Ideally would be to not to lose the ability of using the gui designer, at least for the derived classes.
Any hints much appreciated.
You can make your "base view" as its own ui file with a big QFrame in the middle with nothing in it and name it contentsFrame. Then create your other widgets you want to place inside the empty contentsFrame in designer.
Now you have a couple options. You can have both open side by side in designer, click on your contents widget, select all, and drag everything to your contentsFrame. Then just hit save as and save it as a different widget. If you aren't afraid to leave designer for a tiny bit, you can add your contents widget to the base widget in code. Either way, make sure you are setting the layout for your contentsFrame or everything will look like garbage.

How to create a Multi widgets Qt application

Is it possible to develop an application with a multitude of interfaces (widgets), where each widget has its own .cpp and .h file? If so, can you suggest a way to do that?
I am aware of the stacked widget way but this method doesn't allow me to separate the code of each page from the others.
I need to have the general structure of my application to be similar to an android application developed in Eclipse.
You can create widgets separately, then, dynamically construct QStackedWidget and add widgets to it using addWidget method
PS. you can also use widget promotion, to have QStackedWidget be created by QtDesigner and filled with your widgets
http://doc.qt.digia.com/stable/designer-using-custom-widgets.html

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.