QT/PyQT best practice for using QT Designer - qt

What is your development approach with QT/PYQT and QT Designer ?
Are you doing this:
Put all components on the panel (without any layout) and arrange them
Put components in layout (Align Vertically/Horizontally/Form/Grid)
Generate UI file and start coding
how do you manage when you have custom widget ? For example when you have to fine tune behaviour of a QButton or QLineEdit ? Is it possible to add this custom widget to designer?

Answer to part of your question:
Yes, it's possible to add custom widgets to the designer. You have two alternatives:
You can write a full-fledged designer plugin - see Creating Custom Widgets for Qt Designer for a full example.
You can promote widgets - i.e., you pick an already existing widget that's similar in looks and/or functionality to the widget you'll be creating at runtime, you place the "mock" widget on the form, you right click it, and you "promote" it to the actual type of the final widget you'll be using. At runtime, the form parser will create your actual widget instead of the placeholder. See Using Custom Widgets with Qt Designer > Promoting Widgets for more details.

Related

Qt GUI in designer or in code?

When it comes to designing a GUI in Qt, I am hesitating between using the designer in Qt Creator, or doing everything in source code. I'm using Qt widgets and not QML.
If I use the designer I can easily create a GUI using qt standard widgets. But as soon as I need to subclass a widget to extend its functionality I have to build a Designer plugin to support my new widget. Is that correct? Or is there another way to it?
You can build all the GUI in Designer including custom widgets, and you can also build your custom widgets in Designer.
Designer does not need to interpret your custom widgets. Just use the promote functionality. With promote, you start with a plain widget within Designer and then tell the "real" class of it (your custom one) and the header file where it is decleared. The only drawback is that within Designer, it will stay looking like an empty widget.
In my experience, it is much better to use Designer for the GUI than writing source code yourself. You can easily change all the properties afterwards etc., and it is helpful even if you rely on custom widgets. Source code is not a good declarative language for GUI objects, with all the properties etc. Also you cannot play around, you would need to compile all the time just to tell "Is it better to have this text label in bold font?".
Sometimes I edit the XML files that are created by Designer by hand. For example, if I want to put a widget somewhere else in the object tree. If you don't mess up the XML, Designer will still read it and not destroy your changes. The only reason I see for writing GUI in source code is when you have repetitive elements, or dynamic changes based on data input, e.g. a for()-loop that produces elements. In my project I have some Selector Boxes that are filled with options in the source code.
And btw: If you prefer to write your GUI in code instead of using Designer, maybe you are not the right person to craft the GUI. Most programmers don't understand that while they are technically able to design a GUI, they are not always also competent in doing it.
http://hallofshame.gp.co.at/index.php?file=shame.htm&mode=original
It is a bit of a shortcut, but I often use a simple QWidget as a container for my custom widget. This way, I can setup sizing policies, put the whole thing in the layout I want before my custom widget is even in. Then, in C++, I add the custom widget as a child of the container widget.
edit: As ypnos mentioned you can promote the placeholder directly. You can find guidelines here

make a new widget and use in a form

I'm new to the Qt Framework and Qt Creator, and I need to make a new widget with a UI contains some pushButtons, Labels, functions, and events and then use it in another form.ui.
or come to Qt desiner toolbox as custom widget.
in C# i use UserControl.
How can I do that?
i found promote to option but this is not my answer!
There is already documentation on how to create a Qt Designer custom widget. To be honest though, and this is a personal opinion, it is not really worth the effort unless this custom widget is extremely important to visualize in your UI layout.
You can just as easily create a placeholder widget or an empty layout for it in one UI design, and then add it to the layout in code later. The process is just simply:
Create custom widget as one UI
Create main UI with a an empty layout in the spot meant for custom widget
In code, addWidget to layout
Again, if you really need to visualize this custom widget in Designer, check out the docs. My perspective comes from a PyQt background where we use the Designer very very minimally just for complex layout design and do most of everything back in code anyways.

How to programmatically add a UI to a widget based on a Qtvtkwidget?

I am trying to write a program based of of this example i.e. a widget based on QVTKWidget, so that I can use the PCL Visualizer inside the widget, with no UI and the first step for me would be to add a simple UI a menubar with some simple options: close,save etc.
Unfortunately my experience with QT interfaces is with the Designer, I have though seen what ui files look like but I have seen no tutorials on how to add them to widgets, tutorials for adding them to main windows I've seen a few.
Do you know of a simple method to add a .ui file to a QVTKWidget or a widget in general?
The easiest way to do this is to create an application, or a widget in designer, then add a plain QWidget into the content, this QWidget you promote to type QVTKWidget, through the designer interface as described in the documentation. Then you can add all the other ui elements to the application and interact with the QVTKWidget

Adding user built widgets to a ui file in qt creator

The qt designer portion of qt creator has many built in widgets. But let's say I want to add custom widgets created in the same qt project to the ui file of the window. By taking these steps:
Create a new Qt GUI application with a main window, we'll call the window A.
Add a new widget to the project, the widget just uses standard UI components, say buttons. We'll call this widget B.
Add an instance of widget B to window A.
Now, I know one way to do that, and that is:
In window A, add a blank widget (or widget container, from the containers section of the list of possible widgets. We'll call this widget C.
Promote it (widget C) to widget B.
However, the problem with this is that Qt Creator's designer treats it like a generic QWidget. And as such, you can't do things like add it to a splitter, or connect signals/slots that are specific to the widget.
So is there any other ways to add widget B to window A in the ui file using qt creator? Thank you.
I'm not sure to understood your question well so I could ask the wrong question. Are you sure your "B" widget is a subclass of QDesignerCustomWidgetInterface? This should expose all stuff that your widget/plugin offers...
Last note: a friend of mine tried to add a custom widget like you. And at the end of the described procedure that Lol4t0 told you, he found you must compile plugin with the same compiler with wich qtcreator/designer was compiled. This happens because as we know c++ doesn't keep ABI compability (instead of i.e. C language) stuff like: Name handling can change from compiler to compiler, how data is loaded into registers can change...and so on. My friend tried to compile plugin with mingw, but he found that qtcreator was compiled with visual studio compiler. Therefore if you want to deploy your plugin on Windows or you compile your plugin with visual studio, or you have to compile qtcreator/designer from scratch.
I know this is a very old question, and I'm not sure what capabilities Designer had in 2012, but I came across this in a Google search for something else and figured I'd add some missing info:
However, the problem with this is that Qt Creator's designer treats it like a generic QWidget. And as such, you can't do things like add it to a splitter, or connect signals/slots that are specific to the widget.
Generic QWidgets can be added to splitters with no issues these days.
As far as signals and slots go, you can use them like so:
After promoting a widget to your custom widget, right-click it and choose "Change signals/slots..." from the context menu.
Add the signatures for any custom signals and slots you want to be able to use in Designer / Creator here.
Now those signals and slots will be accessible in Designer like any other signals and slots.
The only thing you can't really get with promoted widgets is access to custom properties in the property panel; for that, yeah, you'll need to go through the custom widget creation process.

Adding custom widgets to QStackedWidget

I'm having difficulty adding a custom widget to a QStackedWidget. I want to include a widget with a different grid layout in this stacked widget.
If someone could show me how to set up the layout (buttons, etc.) inside the stacked widget, that would be even better. The main dialog containing the stacked widget is really simple, and I don't want to clutter it up with tonnes of grid just for one widget inside the stacked widget.
At the moment I'm trying to make the complex widget as a seperate class and insert an instance of this class into the stacked widget using .addWidget() (this doesn't work).
Maybe I'm thinking about it the wrong way?
I'm using PyQt, but C++ answers are acceptable — I can get an idea of how things work by looking at C++ (well, I've been fine so far =s).
If you're using QtCreator I suggest do it this way:
Design each page/widget as a seperate class (seperate *.cpp, *.h and *.ui files) - press ctrl+n and choose Qt / Qt designer form class.
Add X pages/widgets to the stackedWidget. X is the number of views You created
Promote each widget to be your custom designed page (right click in the objects tree on the right and choose promote widget). In field "promoted class name enter" enter the name of your cutom creacted page.
This should get you starded. I hope it solves Your problem. Ofcourse You have to design each view.

Resources