Qt 5.x Custom Widget Creation and Use - qt

I'm returning to Qt programming after an absence of a couple of years, and I'm starting with Qt 5.1. In the past, I've used the designer, and a good deal of hand-crafted code to put Qt projects together. Now, we'd like to make heavy use of the Creator.
But I'm not seeing how to accomplish some seemingly basic tasks. For example, I'd like to design a custom widget, then pull it into my main application, but although the Creator allows me to make multiple files within a project, they seem to have no knowledge of one another as far as the Creator itself is concerned. Once my widget is built, there's no way to pull it into the main application, and also no way to test it independently, at least that I'm able to find.
It seems as though documentation for Qt has taken a major blow somewhere along the line. It is cursory and thin, compared to the extremely detailed docs that used to be available in the past.
If someone can point me to a decent collection of documentation and tutorials, I would be grateful.

Unit Testing
Use the Qt Test module: https://doc.qt.io/qt-5/qttest-index.html
Using Custom Widgets
I'm not 100% sure what you meant by "pull [the widget] into the main application".
If you want to combine your custom widgets in Qt Designer, add a placeholder (blank) QWidget in the parent and Promote it to your custom widget:
https://doc.qt.io/qt-5/designer-using-custom-widgets.html#promoting-widgets
If you want to combine your custom widgets in C++, instantiate your custom child widget and add it into the parent widget's layout using QLayout::addWidget():
https://doc.qt.io/qt-5/qlayout.html#addWidget
If you want to make your application display a custom widget, simply #include the widget's header, instantiate the widget, and call QWidget::show():
https://doc.qt.io/qt-5/qwidget.html#show
If you want to develop your custom widget in a separate standalone project, include it in your main project as a Subproject:
https://doc.qt.io/qtcreator/creator-project-creating.html#adding-subprojects-to-projects
How do I make a subproject with Qt?
Other Notes
Qt Designer has been integrated into Qt Creator for many years. Qt Designer and its documentation have changed very little between Qt 4.8 and Qt 5.
The extremely detailed docs for Qt 5 are at https://doc.qt.io/qt-5/. There are links to useful doc collections in the nav bar on the right.
I recommend exploring QML/Qt Quick. It's much easier to create QML-based GUIs compared to widget-based GUIs. It's still a young technology though, so it might not suit your needs yet: http://doc.qt.io/qt-5/qmlapplications.html

Related

Is there any plan to add Qxt features to Qt?

Qxt is an extension library for Qt providing a suite of cross-platform utility classes to add functionality not readily available in Qt. Qxt consists of several modules, each of which lives in a separate library. It has many nice features like:
QSlider with two buttons
Rotating & richtext buttons and labels
LineEdit with sample text
managing many screen settings
String SpinBox
Check ComboBox
Global Shortcut (hot keys)
Schedule View
Tooltip with arbitrary widget on it
RPC(connecting signals and slots through network)
XML-RPC
QxtSQLPackage (Sending SQL query result through network or to a file)
QxtSlotMapper (Map a signal to slots based on parameter value)
Qt comes with lots of new features in each release. I wonder why these useful and nice features are not included in new versions of Qt. Does anybody know if there is any plan to add these features to Qt in the future?
While I do totally understand not just sending Qxt code up stream, because it can be very kludgy, some of the things mentioned are a bit silly as to why they're not included already. The global shortcuts is the number one thing that comes to mind. Having sample text in a line edit would be another cool feature.
For a while now, in my spare time, I've been working on a note taking app, and I've specifically went with QT because of the ease of having it work cross platform compared to GTK or other GUI frameworks. One of the major concepts was having a global hotkey to make the application appear and disappear, but because QT doesn't do this on its own, I had to deal with Qxt for this one function, and it made it a giant mess to get going. I dealt with tons of bugs that prevented me from building versions for Windows. If I could, I'd gladly ditch Qxt in favor of something already baked in to QT.
Qxt grew out of a need for features that QT wouldn't include for one reason or another, and I frankly just don't understand why.

which Qt project type we should use?

New to Qt. In the official Qt tutorial, it says:
To set up a project, you first have to decide what kind of an application you want to develop: do you want a user interface based on Qt Quick or HTML5 or Qt widgets.
We plan to use Qt to do medical image display, and also use Qt to build a GUI application to control a medical device. They will be two different projects.
Which Qt project type we should use for them?
Docs say:
QWidgets are a better choice if your UI is comprised of a small number of complex and static elements, and QML is a better choice if your UI is comprised of a large number of simple and dynamic elements.
I say:
If you're going for desktop applications I'd suggest Qt Widgets, you don't have to ship the final app with libs for QML and the whole source will be written in C++ (faster, simplier so easier to debug).
QML would be a nice alternative if you want to create phone/tablet (touch experience in general).
I would argue that between Qt Quick and Qt Widgets there is no objectively right answer. In theory, one should be able to replicate any UI using either method - because at the end of the day, they are both using QtGui behind the scenes. So functionality is probably not an issue.
One thing to note is that Qt Quick (i.e., QML) is designed explicitly to make UI programming require much less code, and much less C++ knowledge. I would say it achieves this goal very well.
However, at the end of the day, I think it mostly comes down to what language your developers are already familiar with. If you have a team of C++ pro's, then I would go with Qt Widgets - if only because it's going to be very easy for them to pick up, and it's something they're already familiar with. (I'm guessing this is the case because you're already writing C++ code for your project).
If, on the other hand, your developers are already very good at QML (or, more generally, JavaScript - which QML is heavily influenced by), then I would go with Qt Quick for the same reasons.
I know of two types of Qt UI: Qt Quick and widgets.
Widgets behave like most other UI toolkits out there, you have a GUI editor and a tree of UI objects. They are pretty mature and look like most standard UIs.
Qt Quick is the newer Qt UI toolkit. It uses a domain specific declarative language (QML) to specify the user interface and JavaScript for interactions. There are also plans to offer Qt widgets inside the Qt Quick framework, but I'm not sure how far that project has gotten. Qt Quick is meant to deliver more dynamic / custom user interfaces.
As far as I know Qt will continue to support both approaches in the foreseeable future so which one you pick depends on your use-case.

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

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.

Is Qt Quick the way to go to build a modern GUI for a C++ application?

After 4 years of absence I'm finally coming back to Qt development again. I'm quite impressed by the development it has gone through, but also a bit confused by Qt Quick, which got just introduced in Qt 4.7
For me it seems like something to build a quick, appealing GUI with some logic in Java Script. However, if I'm going to build a C++ application, is Qt Quick also the modern way of building a GUI for that or should I follow the classic path by using the .ui files?
Qt Quick should fill a much needed gap but whether that means it will become the way to build applications will largely be determined on the type of applications you intend to build.
Qt has provided .ui files for a long time. These files are easy to work with when you need to create applications based on standard widgets. Designer makes it easy to layout the widgets and do other basic operations.
Qt 4.2 introduce style sheets (qss) that made it possible to style existing widgets. These styles are convenient ways of styling the standard widgets to get away from the standard widgets' look-and-feel, while retaining basic functionality.
But sometimes you need to create custom widgets, widgets that don't exist. Before Qt quick, the only way to do this was to write native code (be it C++ or, with language bindings, Python or Ruby). With Qt Quick it's now quite possible to write widgets and full blown UIs that would otherwise be somewhat painful to write in code. Because Qt quick's focus is on the UI, it's becomes a better development language for that goal. The Qt Quick paradigm also transcends the idea of strict standard widgets, adding support for transitions and boundary-less widgets.
The answer really depends on the type of application you're planning to build and I consider the description provided on the page you linked to very accurate:
"Qt Quick helps programmers and designers collaborate to build the fluid user interfaces that are becoming common in portable consumer devices, such as mobile phones, media players, set-top boxes and netbooks."
At the moment, Qt Quick is certainly not the most obvious choice if you're planning on building a classical desktop application, an area of development which the Qt library traditionally excels at, and in this case you're probably better off using what you call "the classic .ui approach"; at the same time, I think that's the exact reason why Qt Quick was introduced: to add a new tool to facilitate the development (or rapid prototyping) of applications not focused on the desktop.

Resources