how to add a custom widget to Qt 4.8.6 - c++

I'm trying to figure out how to add a custom widget to Qt Designer. So far I've been unsuccessful. Very new to Qt. I'm running Qt 4.8.6. I know there is a newer version but due to project constraints I have to stick with this one. Essentially I'm need to add a hex spin box, i.e. a spinbox that counts in hex rather than in decimals. I even found code that implements it. It does explain how to integrate it into Qt Designer, however it is explained for version 3. Can someone offer assistance?
Any help is greatly appreciated

Read about creating designer widgets and using them, but briefly:
Subclass QDesignerCustomWidgetInterface to create your custom designer plugin for your custom widget
Build and install your designer plugin
Tell designer about your plugin location, or just make sure it's installed to $QTDIR/plugins/designer
There's also an example to follow.

Here is a documentation. At short, you just add any widget (e.g. QSpinBox to draw it nice in the Designer) to your form and convert it to the needed one.

To add a custom widget to Qt designer simply follow the steps:
In Qt designer create a new widget File->New->Widget.
Add your UI etc for this widget and save it->->MyWidget.ui
Create a class "MyWidgetHandler" to handle this widget in a MyWidgetHandler.h & cpp
In your MainWindow/Dialog wherever you want to display this widget add a "Widget" from the "Containers" section of the designer.
Now right-click the "Widget" container you just added and click "Promote to..."
This should open a dialog. You can select here the base class to inherit properties in your case QComboBox or just select the QWidget class.
Next enter the full class name i.e with namespaces eg: "blah::MyWidgetHandler"
In the Header file section simply add the path to the header file for this class. This should be a resolvable path. Eg: "UI/MyWidgetHandler.h" or "C:/UI/MyWidgetHandler.h"
Now click "Promote" and save your MainWindow.ui
The Property Editor should now show your custom class type eg: blah::MyWidgetHandler. and the properties from the base class you selected.
Don't forget to setup the UI in your handler class and include header in the Mainwindow handler.
Cheers !!!

Related

Qt 5.10 - How to add a chart example to a layout

It's my very first experience with Qt so i hope its a basic question with a basic answer I couldn't find. I need to add a "custom" chart widget
(like in the following example: https://doc.qt.io/qt-5.10/qtcharts-zoomlinechart-example.html)
to some Qt container like "layout" or "form". Its very straightforward when you have the widget in the toolbox and you use the designer but since it is a custom widget i cannot do that.
How do you do this in Qt creator?
If your Chart class inherits from QWidget, you have two ways to do this:
In the Design mode, put a QWidget on your Window, or into a Layout. Then, right click on it and select "Promote to ...". There, write your class name in "Promoted class name".
In your window cpp file, write:
YourChartClass* ycc = new YourChartClass(this);
ycc.show();
or
YourChartClass* ycc = new YourChartClass();
this->layout()->addWidget(ycc)
If your chart inherits from another class, and that class inherits from QWidget, the code lines are the same.

Set CSS both in designer and in code for my custom widget

I have a custom widget subclassed from QPushButton, MyButton. It's implemented in a single .cpp file and I'm using it in Qt Creator in my application's form (I've added a QPushButton then promoted it to MyButton).
As discussed here: Should I really use a single qss file for my application instead of having one for each (UI) class? I wish to generally set its CSS inside its constructor (the general look of the button, the background image, hover behavior etc), but I would also like to be able to set it in Qt Creator (maybe customize the font size/color; generally specific to a particular button).
The issue is, as the setupUi call is issued, my buttons are created, they set their styles in their constructors, but then Qt applies the styles set in Qt Creator, immediately overriding mine.
What can I do to achieve this effect?

Custom menu actions in Qt Designer

I'm currently trying to use Qt Designer to build a GUI and I would like to customize slots of my menu actions.
E-g: I'd like the user to press a menu action and it'd show a widget if it is hidden or hide it if it is already visible.
Basically, what I want to do is execute some code of mine and not the default actions such as show() or hide().
So I'm wondering if I should create a subclass of QMenuBar, add custom slots to it, then create a plugin to use it inside Qt Designer or if I should create a subclass for QMenu or QAction ? Or maybe it isn't the right way to do that ?
I'm working under Visual Studio and I'm only using Qt Designer, not Qt creator.
I'm new to GUI and Qt programming and I'm a bit lost here.
Thanks in advance :)
You have basically 2 options:
Implement the custom logic in the Mainwindow sublcass.
For this, you simply add the slots required for your handling in the class, and make them available in Qt Designer. You can do this:
either in the Signal/Slot editor and click "Modify" and then click on the + Symbol. By this you make new slots available in QtDesigner;
or when your slot is called on_(senderName)_(signalName), Qt autowiring will automatically connect the signals, and you don't have to do this in code or desinger.
Create a QMenuBar subclass and implement the custom logic there.
Your case tell Qt Designer to select your specific subclass as replacement for the default QMenuBar by right-clicking on it, and select "Promote to...". In the new dialog, you can specify your custom class that will be used as replacement in actual code, but in design time a QMenuBar is used. With this mehtod, you don't have to write a separate plugin to make your class available in Qt Designer.
Note that with the second option, your custom logic will only be called when the actions are triggered through the menu bar, not by shortcuts or tool buttons
Create a slot in your class:
onMenuActionTriggered()
Use the connect() to react on action's signal:
connect(ui.myAction, SIGNAL(triggered()), this, SLOT(onMenuActionTriggered()));
In your slot you can do whatever you want.
Another solution (not my favourite one, but possible) is to use the auto-connect functionality, which means, by declaring a slot 'on_myAction_triggered()' (where myAction is the name of your QAction) you don't need to use the connect() since it is automatically connected by Qt
The menu bar is automatically added to any new form derived from QMainWindow (the default when creating a gui application, but you can create new main windows using file->new file or project... and selecting Qt->Qt Designer Form Class ).
To add options to it you simply click in the area labeled "Type here" and write your option text. When you do so an action will appear in a list in the lower part of Qt Designer. Right click on that action and select "go to slot". It will pop up a dialog with "triggered()" already selected for you. Simply click "Ok" and Qt Creator will take care of all the details and transport you to the body of the slot function.

How to create Qt custom base class

I would like to create a custom main window for my application and I would like to integrate it with Qt Creator. When you create a new Qt Widgets Application you get the option of inheriting from QWidget, QMainWindow or QDialog. I would like to add my own widget there:
I would like my custom .ui form to be generated when I select my custom widget from the drop down list. Is this possible? If yes, how?
Or maybe it is possible to add a custom template widget to the Qt Designer Form dialog when creating a new form? I'm talking about this:
Regards!
To integrate a custom widget with Qt Designer, you require a suitable description for the widget and an appropriate .pro file.
It is documented here:
http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html

Refactoring / partitioning of Qt GUI widget source file

I have created a traditional Qt (widget based) GUI, something like this: MainWindow::MainWindow(parent) : QMainWindow(parent)
This is designed by Qt Creator as forms (mainwindow.ui), aka Design Mode. Everything works fine. But the GUI code with all widgets, initializing the corresponding models, and functionality gets quit long. I'd like to refactor to small units. Things I came up with:
I tried using specialized (derived) widgets. Example: A created MyTableView::QTableView contains the specialized model, as well the signal/slot handling between model and widget. This reduces the amount of code in MainWindow. However, I do loose the capability to design the GUI via Qt Creator's Design mode.
The best thing I came up with so far, was to spilt the source code (multiple cpp files). It still represents one class, but less code in one file.
So, how could I better partition my GUI class?
If you still want to uncouple the initialization of widgets by derived widgets, you can use "promote to ..." option in Qt designer. Steps:
class MyTableView : public QTableView {}, and so initialization of table view is moved to the constructor of MyTableView.
In Qt Designer, open the ui form (MainWidow.ui), and drag and drop a QTableView on it;
Right mouse click the QTableView, in prompt menu, there's a "promote to" option, open it
In the dialog of "promoting widget", specify your custom QTableView's class name and header file, say MyTableView, MyTableView.h. This step requires existing custom class and header file.
Borrowed a picture:
You could create your own Qt widgets and register them with QtDesigner. Then will you be able to use them on forms as mere QLabels and friends. See this link
In a recent project, we had pretty restrictive uncoupling requirements (especially not to be too strongly linked to Qt). What we used to do based on MVC-like pattern is:
Implement a controller that controls the application workflow
Add a GUI "adapter" class per screen that communicates with the controller. Let's say HomeScreen class, SecondScreen class
Each adapter class contains a given number of widgets: TimelineWidget, FormWidget
Each widget is composed of a ui member (Ui::TimelineWidget ui) that is generated from a .ui file designd with Qt designer
Note that this structure might not be suitable for small projects.