How to create relation between main ui and different cpp/h files?
-
I am newby in QT. I want to create a desktop project using QT widget app c++. In my mainwindow ui, there are multiple layouts and frames for each layout. For instance, for one of frame i use a listwidget. I add items here in mainwindow.cpp. Then, click one of item it sholud create a chart. I created a qchart in a different frame. I use mainwindow.cpp either for creating qchart. But i have more frames. If i use mainwindow.cpp for everything it can be a problem. I don't know how to create a relation between new cpp/h files and mainwindow ui. I use just one page for this desktop app. So, if i need to create new cpp/h files how can i add something (like qlistwidget items, qchart ...) to one of mainwindow frame (or layout)?
-
@Rozerin-YILDIZ Lots of options, but I am not sure I understand the question. Can you write a list of steps the user takes and what they should see, or draw a picture of what you want to happen?
Something like:
- User launches app.
- See main window with bunch of buttons and a QTabWidget in a layout.
- User clicks "Foobar" button.
- See new tab in QTabWidget
- Tab displays a BarBazWidget defined in
barbaz.{h,cpp}
- Or user clicks "Foobar" button.
- Sees new window. Original window stays visible
- New window displays a BarBazWidget defined in
barbaz.{h,cpp}
- Or user clicks "Foobar" button.
- Original window stays visible
- Space previously occupied by a table is replaced with a BarBazWidget defined in
barbaz.{h,cpp}
- Original table is discarded/kept
- User launches app.
-
@Rozerin-YILDIZ said in How to create relation between main ui and different cpp/h files?:
So, if i need to create new cpp/h files how can i add something (like qlistwidget items, qchart ...) to one of mainwindow frame (or layout)?
See https://doc.qt.io/qt-6/designer-using-custom-widgets.html
You create a custom widget in other ui/h/cpp files and then in main window you add a normal QWdget and then use the "promote" feature to replace this QWidget with your own custom widget. -
@ChrisW67 Let's say this is the main window:
As you can see, there are multiple frames (layouts) in main window. For instance, there is a qlistwidget in frame 1. If click one of list item, then there will be a qchart in frame 2. That's the story. I wrote this code in mainwindow.cpp and it works, no problem. But for other frames, i will need create something like this. That's why there will be many issues in a one cpp file. I want to split this issues (if it makes sense). There will be just one .ui file (mainwindow.ui). I hope i can be clear enough. Thank you.
-
@Rozerin-YILDIZ said in How to create relation between main ui and different cpp/h files?:
There will be just one .ui file (mainwindow.ui)
The UI file is not directly connected to your class structure... As you might know, you don't need any
*.ui
files at all. Everything can be done by code.I want to split this issues (if it makes sense)
Make no sense to me to split the
MainWindow
content into multiple classes / files. If it's shown (used only) in yourMainWindow
widget, what's wrong with having the code there?
QMainWindow
after all is just another widget, that you can fill with content.As I've said in your other topic, you can make widget "factories" to create and return the new widget to your main window.
But this is also not needed.And if you subsclass some widget, you have your own separate class (files) anyway, which you include in your MainWindow header or code file.