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. Access a dialog's UI from main class.
Forum Update on Monday, May 27th 2025

Access a dialog's UI from main class.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.7k Views
  • 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.
  • L Offline
    L Offline
    leonidas_lolo
    wrote on last edited by
    #1

    Hello guys,
    Let's say that i have a Dialog and a MainWindow classes. MainWindow has (mainwindow.cpp, mainwindow.h, mainwindow.ui, main.cpp). Dialog has (dialog.cpp, dialog.h, dialog.ui). I want to access all the items that dialog.ui has (pushbutton, textbrowser, etc) from mainwindow class.
    I hope that you understand my topic.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @leonidas_lolo the best way would be to use Signals & Slots mechanism.

      157

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leonidas_lolo
        wrote on last edited by
        #3

        Can anyone post a link or an example (the simplest one).

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mojtekosa
          wrote on last edited by p3c0
          #4

          Just create public getter methods in your Dialog class...

          QString Dialog::name() const
          {
              return ui->nameLineEdit->text();
          }
          
          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            A simple example would be :

            MainWindow.cpp

            Dialog *dialog = new Dialog(this);
            dialog->show();
            

            Dialog.cpp
            Consider it contains a QPushButton named pushButton, then

            connect(pushButton,SIGNAL(clicked()),parentWidget(),SLOT(onDialogButtonClicked()));
            

            where onDialogButtonClicked() is declared as a SLOT in MainWindow.cpp

            157

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leonidas_lolo
              wrote on last edited by
              #6

              Where is the best point to declare a connection ( at main class constructor ? )

              p3c0P 1 Reply Last reply
              0
              • L leonidas_lolo

                Where is the best point to declare a connection ( at main class constructor ? )

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @leonidas_lolo but then you will need to know that object viz. in other class in main class. Considering the example above, you will need to know pushButton in MainWindow.cpp for connections.
                Ofcourse you can also connect to a custom signal in Dialog.cpp which can be emitted on pushButton's click.

                157

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  leonidas_lolo
                  wrote on last edited by
                  #8

                  Yes I think that in my application custom signals and slots are more useful. But is better to make the connection of the signal and slot only when what I want happens or should I declare the connection in the main class constructor ?

                  p3c0P 1 Reply Last reply
                  0
                  • L leonidas_lolo

                    Yes I think that in my application custom signals and slots are more useful. But is better to make the connection of the signal and slot only when what I want happens or should I declare the connection in the main class constructor ?

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    @leonidas_lolo

                    But is better to make the connection of the signal and slot only when what I want happens.

                    Yes if you want it then connect each-other objects.

                    should I declare the connection in the main class constructor ?

                    You can, if the objects are accessible in constructor.

                    157

                    1 Reply Last reply
                    0

                    • Login

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