Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using Ui file in other classes
Forum Updated to NodeBB v4.3 + New Features

Using Ui file in other classes

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pragati
    wrote on last edited by
    #1

    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,

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      You can pass your .ui file to a QUiLoader: http://qt-project.org/doc/qt-4.8/quiloader.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pragati
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pragati
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pragati
                wrote on last edited by
                #7

                Thanks Andre,

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

                Thank you so much for making me understand.

                :)

                1 Reply Last reply
                0
                • sonichyS Offline
                  sonichyS Offline
                  sonichy
                  wrote on last edited by
                  #8

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

                  https://github.com/sonichy

                  1 Reply Last reply
                  0
                  • onurtunaO Offline
                    onurtunaO Offline
                    onurtuna
                    wrote on last edited by
                    #9

                    What's this?

                    Ui::MainWindow::statusBar->showMessage("hello");
                    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.

                    1 Reply Last reply
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved