Using Ui file in other classes
-
Hi All,
Is there any option to use a Ui file in different classes??
I mean suppose I have two different class name Date and Menu
I have created the screen for Date in designer and used that ui in Date.cpp by including "ui_Date.h"
Now I want to use any widget say a label of this Date class's Ui into my other class i.e. Menu.cppWill that be possible??
Simply including .h files will not help ....How to use?
Thanks in advance,
-
You can pass your .ui file to a QUiLoader: http://qt-project.org/doc/qt-4.8/quiloader.html
-
Yes, that is possible. But for that, you should recognize what the ui file is. It is an easy way to specify a GUI, for which then code will be generated with real object instances representing all the widgets you put on the form. So, you do not reuse the .ui file or something like that, you are simply accessing an object.
Normally, what happens is that your ui file will be used to define a form (or more general: a widget, but to avoid confusion I will use 'form'). This form is a class. All widgets on this form are available as pointers to their object instances. Every pointer is a member of the UI::FormName class, an instance of which you have inside your own class. This instance is usually called m_ui or ui.
There are multiple ways to expose these widgets to other code outside your form. The most naive way is to make the m_ui variable public. That means that every other piece of code that has a pointer or a reference to the form can directly access all widgets on the form, and do whatever it wants with them. I strongly recommend against this solution. It promotes spaghetti code, tight integration between components that should be disconnected, and code that quickly becomes very hard to change and maintain.
Instead, it is better to give your form some methods that allow code outside the form to get and set some values that make sense for that form. It is then up to the form to decide how to get or where to set/display such values. So, instead of exposing your line edit, you give your form a method that results in the line edit on it changing color.