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 Updated to NodeBB v4.3 + New Features

Access a dialog's UI from main class.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.8k Views 2 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.
  • L Offline
    L Offline
    leonidas_lolo
    wrote on 18 Mar 2015, 10:05 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
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 18 Mar 2015, 10:33 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 18 Mar 2015, 11:06 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 18 Mar 2015, 11:21 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
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 18 Mar 2015, 11:42 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 19 Mar 2015, 09:11 last edited by
              #6

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

              P 1 Reply Last reply 19 Mar 2015, 09:26
              0
              • L leonidas_lolo
                19 Mar 2015, 09:11

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

                P Offline
                P Offline
                p3c0
                Moderators
                wrote on 19 Mar 2015, 09:26 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 19 Mar 2015, 09:33 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 ?

                  P 1 Reply Last reply 19 Mar 2015, 09:37
                  0
                  • L leonidas_lolo
                    19 Mar 2015, 09:33

                    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 ?

                    P Offline
                    P Offline
                    p3c0
                    Moderators
                    wrote on 19 Mar 2015, 09:37 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

                    1/9

                    18 Mar 2015, 10:05

                    • Login

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