Nominate our 2022 Qt Champions!

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.cpp

    Will that be possible??

    Simply including .h files will not help ....How to use?

    Thanks in advance,

  • Moderators

    You can pass your .ui file to a QUiLoader:

  • I was not aware about this kind of approach. Thank you, I am not sure I will get from it what I need. But fist I will read about it and understand properly.

    Anyways, thanks

  • 'Use' in what way? Set a value on it? Or put it on a completely different form?

    Note that QUiLoader is only very, very seldom needed. Chances are >99% that you don't need it, I'd wager.

  • Use in sense....Suppose I have a lineedit in date screen's ui. I want to acess this lineedit in main screen and want that when a button is clicked on a main screen the colour of lineedit in date screen shall change.

    Will that be possible?

  • 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.

  • Thanks Andre,

    Some basics were not clear that's why was facing such kind of confusions.

    Thank you so much for making me understand.


  • Ui::MainWindow::statusBar->showMessage("hello");
    error: invalid use of non-static data member 'statusBar‘

  • What's this?

    error: invalid use of non-static data member 'statusBar‘

    Use signal and slot instead. You can not call such method without creating an object of that class before.

Log in to reply