How to access Child UI files - c++

How can i access ui files of children of a class. Lets say MainWindow class has twoa child dialog. I want to access LINEEDIT of dialog so that i can take text from there. Similarly how can i access ui files of parent inside child class in QT.
Note:
I havn't inherited any thing from Parent class.

I suggest you read the beginner tutorials of Qt. The ui files just carry XML that the uic can read to turn into 'real' C++ for the compiler. If you need to send data between decoupled UI components, use Signals/Slots - a fundamental building block in the Qt framework.

Related

Connecting multiple .ui files in Qt

I have a window(.ui file) and at certain point i want to erase everything inside of it and repaint a new interface by connecting a new .ui file to that class without creating seperate class. Is there a way to do it?
The task sounds like a good job for QStackedWidget. With its help you can design different widgets (aka "pages") conveniently in the designer, and switch between them inside your code using setCurrentIndex() and/or setCurrentPage().

How to share data with a TreeView such as done by gtk_list_store

I do managing an app with a TreeView in Qt/C++
In order to not mix File system access and window/UI management, I do have a create a C++ code which manage to interact with the FileSystem and a C++ class which is in charge of displaying the Tree
I have seen that Gtk had a
gtk_list_store_set(..)
is there any equivalent in C++ / Qt ?
What I would do is to be able to file the TreeWidgetItem using an API or method from the filesystem side and the display tree part just have to read this value.
Any idea on how to do it properly ?
I was thinking instantiate the class of TreeWidget inside the code of the FileSystem but I would like to make it smarter.
FileSystem file a treeview model and the ui read this model.
Thanks for your help
If I understood you correctly, there is a ready made solution in Qt for your problem. QFileSystemModel you can use it to access the file system, and than pass it to Widget.
If you are using MTP stack, you may need to implement your own model by subclassing QAbstrackItemModel http://doc.qt.io/qt-5/qabstractitemmodel.html

Splitting Qt forms between multiple ui files

I have a main window UI file created in Qt Designer. To avoid clutter and make the code more modular I'd like to create some parts of this window in separate ui files.
For example I may have a tab widget on the main window and then I'd like to have separate page1.ui and page2.ui which I can then "embed" inside the main window.
Is it possible with just UI files or do I need to create a class for each additional widget?
I was thinking about creating separate classes Page1UI and Page2UI which publicly inherit from ui_page1.h and ui_page2.h generated by qmake (so that the widgets inside them can be easily accessed from the main window) and then promoting main window's placeholder widgets to those classes. So in code I'd have something like this for example:
connect (page1->ui->someButtonFromPage1uiFile, ..., this->ui->someMainWindowWidget, ...);
However Qt documentation doesn't seem to mention public inheritance so I wonder if this is the right approach or if there may be a simpler way.
You don't need inheritance for this. These UI files are just widgets. So of course you can do this. You should have a main class which will have a tab widget and then just add your page1, page2, etc in main tab widget. What you need is composition here in my opinion.
What you need to do is create a separate class derived from QWidget which will contain an object of your generated UI form. You need to call setupUI() of that generated form in your new class with this as parameter.

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.

How do you programatically update the UI in Qt?

I'm fairly new to Qt, and am trying to wrap my head around signals and slots. Though I've figured out how to create custom slots, I've yet to figure out how do update the GUI from my C++ code. I realized that the entire UI, which I created in the designer, is only written in what appears to be XML based UI code. Do I have to handwrite my own Qt C++ UI in order to update the interface, or can I somehow use C++ to update the XML based UI? I'm just looking to add a widget to the main form on a button click. Any help is appreciated. Thanks!
To add a widget to a form you can simply do
ui->layout()->addWidget(new Widget);
XML is used by QtDesigner as a mean to create, update, and persist your GUI, enabling a visual approach to development, but in the end you can build your application entirely without it.
you dont havfe to update the xml of UI . you can inherit the ui into your own class using setupUi
http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qwidget.html#setupUi
now in your C++ class you can update the widgets like changing label text, lineEdit text, setting up spinbox value using Signals & slot.
That xml is used by Qt designer and outside of designer it's only used by uic as a source to generate C++ code, so everything that you do in the designer end-up as C++ code, so it can be updated (or done completely) in C++ code (you can look into the code and see that you most likely have a member called ui that is most likely a pointer, but it can also be an instance of a C++ class that is generated for you by uic, and trough that ui member you can access the widgets and layouts that you created in the designer).
A generic resource that i recommend you to read can be found here and then (if you still can't figure it out) ask a specific question about what exactly are you trying to achieve.
LE: that link contains 3 methods to use the generated code into C++ code, don't try all of them into your working project, you may choose one method or use the default one (ui member pointer)