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

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.

Related

Qt5 Designer and Encapsulation

I've created a main window in Qt Desginer which has the following structure
Ui_MainWindow
VerticalLayout
QTabWidget
Widget (*A)
QChart
QChart
The uic generates a header file "ui_mainwindow.h". This header files contains references on all the sub (subsub, subsubsub, ...) widgets. In my point of view this isn't good encapsulation.
I found a way to set custom classes for the different widgets in the Qt Designer.
Since I have to write these classes manually I have no access/references to the child widgets. I only can control the widget itself. Still all widgets are referenced in the MainWindow UI.
Is there a way to generate separate classes for each (or some selected) widget? I'd like to intercept *A and access the child QCharts.
Thank you,
Even though you can already "intercept A" in the current form. You can certainly separate A in a different section then add a QWidget in your main window, right click on it and promote it to being A

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.

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.