How to create Qt custom base class - c++

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

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.

Qt how to embedded image resource in custom widget plugin dll and make it available in qt designer resource chooser dialog

I have implemented a qt custom widget (MyWidget) plugin which can be used in qt designer. There is a property IconImg of MyWidget with which user can provide an image file to change the icon of MyWidget. Now, I want to embedded several default images for it. Then, user can easily choose the preferred one.
My question is, in qt, how to embedded image resource in custom widget plugin dll and make it available in qt designer resource chooser dialog?

how to add a custom widget to Qt 4.8.6

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 !!!

Custom class in qt creator

I'm new to Qt and fairly new to C++ but I need help with this issue.
I have a custom class called HybridStack and I want it to extend a QStackedWidget and a QMainWindow.
I want it to extend a QStackedWidget so that I can use as many pages as I need and I want it to extend a QMainWindow so that I could be able to make each page have it's own MenuBar with different content of menu for different page.
I want to add this custom class HybridStack to Qt Designer by
promoting it from a QStackedWidget.
Is this possible? If it is, can you brief me on how to do this? If it's not possible then what's an alternative? Most importantly, I need to use it in the Designer because I need to use qmake
You can't derive from both QStackedWidget and QMainWindow, because both of those are in turn derived from QWidget. If you did so, you'd end up with the Dreaded Diamond. You'll have to use composition instead.
Even then, I'm not sure if it would work correctly to put a QMainWindow within a QStackedWidget, since it is designed to be a top-level item (i.e. its shown directly as a window, not embedded within another QWidget). Another way of accomplishing what you want (menu bar changing when the you change tabs) would be the following:
Make your top-level item a QMainWindow
Make the central widget a custom widget derived from QStackedWidget
When the item showing in the stack widget changes, you can call QMainWindow::setMenuBar to change the menu bar. Each widget within the QStackWidget could have its own QMenuBar instance that it uses for this purpose.

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.