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. Accessing from one form to another

Accessing from one form to another

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 13.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
    pag-r
    wrote on last edited by
    #1

    Hi.
    I'm new in Qt and I've stuck with a problem. I have QDialog to edit sql record, and MainWindow with viewTable. And all i want to do is possibilty to "refresh" viewTable in MainWindow when I click save button on edit dialog. Is there any way to do it without inheritance? I've searched forum and google as well, I've tried signals and slots but I'm far too new in Qt to do it properly.
    Regards.

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

      Signals and slots are the right track.
      What you are after is communication between two objects. It doesn't really matter if the objects are forms or something else. What you might do, is equip your main window with a slot (I propose to call it refresh()), and give your dialog a signal called triggerRefresh(). Then, when you create your dialog from your main window, you connect the signal to the slot you just defined. Next, you need to make sure you actually emit the signal from the dialog at the appropriate time.

      However, I am wondering, if you need all of this. I assume the dialog will only need to be visible while editing a record, and after editing (on pressing Save) the dialog will be closed and only then the main window will need to update? In that case, you can just make the dialog modal and trigger the update based on the result of your dialog (accepted or rejected; you only refresh on accepted (Save) of course).

      Yet another way of dealing with this, may be to make use of the model/view framework. Since you display a table, you have a model that holds your data, I'd think. You currently pass the dialog you use to edit the data the actual data to edit, I assume. Am I right? How about instead of passing the data, you pass the relevant QModelIndex(-es)? That way, you can simply have the edit dialog call setData() on the model when you click the Save button, and the data will be stored in the underlying model directly and automatically updated in your main window. Note that this way also uses signals and slots, but those signals and slots are already part of the model/view framework.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pag-r
        wrote on last edited by
        #3

        Thank You for reply, Andre. Dialog is set to modal and I'm trying to do it exactly in way You've wrote in second paragraph. However I'm not sure how to do it properly, anyway I will try to implement it.

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

          Well, you should recognize that when the dialog's exec() call returns, you still have access to a variable that represents that dialog. That gives you the opportunity to call methods on the dialog: that it is closed (no longer visible to the user), does not mean the object is destroyed! So, just give your dialog some methods to access the values you need, and call on those methods from your main window after the dialog has returned an accepted value.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pag-r
            wrote on last edited by
            #5

            I assume that the connect should be in my QDialog constructor?
            @connect(ui->pushButtonSave, SIGNAL(clicked()),QObject MainWindow, SLOT(MainWindow::refresh()));@
            And I don't have any idea how to declare MainWindow connection. QObject MainWindow doesn't work...

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

              If you want a signal-slot connection (I thought that you understood that you don't need one), then you make such a connection at the place where you have references to both the involved objects. The most logical place in this case would be the place in your mainWindow code where you create the dialog. So, not in the constructor of the dialog. Something like this:

              @
              EditDialog* editDialog = new EditDialog(this);
              //set your values
              connect(editDialog, SIGNAL(triggerRefresh()), this, SLOT(refresh()));
              editDialog.exec();
              //...
              @

              To make your dialog actually emit the signal, you may do something like this in the constructor of your dialog:

              @
              connect(ui->pushButtonSave, SIGNAL(clicked()), this, SIGNAL(triggerRefresh()));
              @

              Note that it seems that you did not use QDialog as the base of your dialog. Perhaps you should re-evaluate that choice. QDialog gives you the option to use standard buttons, including a save button, that have the appropriate roles pre-defined. The nice thing is, is that they will order themselves in the order that is defined by the platform your application runs on, so the user is never suprised by what button will cancel and what button will accept.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pag-r
                wrote on last edited by
                #7

                I think I don't really understand the idea of slots/signals and triggers too :/, so I better learn more basics:). My last experience with GUI was C++ Builder, and Qt is something completly new for me. in Builde what i have to do was include header file of second form and it was just all. Thanks for reply.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pag-r
                  wrote on last edited by
                  #8

                  I can't do it. Could somebody show me an example of accessing one form from another?
                  My test code
                  from mainwindow.h
                  @public slots:
                  void test();
                  void trigger();

                  private slots:
                  void on_pushButtonMainWindow_clicked();@
                  mainwindow.cpp
                  @void MainWindow::on_pushButtonMainWindow_clicked()
                  {
                  Dialog dialog(this);
                  connect(this, SIGNAL(trigger()), &dialog, SLOT(test()));
                  dialog.exec();
                  }

                  void MainWindow::test()
                  {
                  ui->lineEdit->setText("efgh");
                  }
                  void MainWindow::trigger()
                  {
                  ui->lineEdit->setText("this is a test");
                  }@
                  from dialog.cpp
                  @ connect(ui->pushButtonDialog, SIGNAL(clicked()), this, SLOT(MainWindow::trigger()));@
                  And it gives me:
                  @Object::connect: No such slot Dialog::MainWindow::trigger() in ..\slots\dialog.cpp:9
                  Object::connect: (sender name: 'pushButtonDialog')
                  Object::connect: (receiver name: 'Dialog')
                  Object::connect: No such signal MainWindow::trigger() in ..\slots\mainwindow.cpp:20
                  Object::connect: (sender name: 'MainWindow')
                  Object::connect: (receiver name: 'Dialog')@
                  Sorry for double post

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    Signals are sent from the objects that define them. Your main window has no signal trigger - that's what the debug console tells you literally :-)

                    In your case, you probably want to connect a signal of a button or an action to the slot.

                    If you define your own signal triggered, it does not need an implementation in the cpp file (actually you must not provide any!). And if you have that your signal must be emit somewhere to have the connection be called in the end.

                    Did you read the "Signals & Slots":http://doc.qt.nokia.com/4.7/signalsandslots.html docs already?

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pag-r
                      wrote on last edited by
                      #10

                      I've briefly read this doc and C++ GUI Programming with Qt 4 By Jasmin Blanchette, Mark Summerfield too (about signals and slots).
                      More, in project I'm actually working with, I have 2implementations of signals & slots, and they are working fine. But they both signals and slots in mainwindow area.
                      @ connect(ui->tableViewHouses, SIGNAL(clicked(const QModelIndex&)), this, SLOT(clickedRow(const QModelIndex&)));
                      connect(ui->tableViewInhabitants, SIGNAL(doubleClicked(const QModelIndex&)),
                      this, SLOT(doubleClickedRow(const QModelIndex&)));
                      @
                      I have a problem to connecting from main to dialog. I know that problem is my lack of knowledge, but i have no idea howto do it between different windows

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

                        Does your edit dialog have a signal defined called trigger (that is, does it have a line in the header file that looks like this)?
                        @
                        signals:
                        void trigger();
                        @

                        On top of that, do you have this line in the header of that dialog?
                        @
                        Q_OBJECT // this line is usually the first line inside the actual class definition
                        @

                        Your error message suggests that you don't.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          Define the signal in one form, emit when you have something to tell to the other form.

                          Connect in the other form. Or connect in some managing code - it depends.

                          Other possibility is to give the form a pointer to the other form and call a method directly.

                          Or do it like Andre suggested:
                          Call exec() on a "QDialog":http://doc.qt.nokia.com/4.7/qdialog.html subclass and get the data from a method of this dialog and put it into the widgets of the main form, if the exec returns with Accepted.

                          I personally would go with the last approach.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            pag-r
                            wrote on last edited by
                            #13

                            [quote author="Andre" date="1299516055"]Does your edit dialog have a signal defined called trigger (that is, does it have a line in the header file that looks like this)?
                            @
                            signals:
                            void trigger();
                            @

                            On top of that, do you have this line in the header of that dialog?
                            @
                            Q_OBJECT // this line is usually the first line inside the actual class definition
                            @

                            Your error message suggests that you don't.[/quote]
                            Yes, I haven't get signal, I've added trigger now, but still it doesn't work properly.
                            "pastebin":http://pastebin.com/nAA2X3d8

                            //edit
                            Its to much for me. Im quiting with this. Thanks for Your replies and Your time. Maybe some other time I'll try to understand slots and signals between forms.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              What you do is: Have the main window emit a signal that the dialog reacts on. You most probably want it the other way round!

                              Good luck with your projects so far. If you only read the docs briefly you'll be stuck in similar situations here and then in the future again... be warned!

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                pag-r
                                wrote on last edited by
                                #15

                                It works:) Thank You both for help. I've do it nor in signals&slots but in the way which Andre suggested with dialog set on Accepted
                                @void MainWindow::on_pushButton_clicked()
                                {
                                Dialog *dlg = new Dialog(this);
                                if(dlg->exec() == QDialog::Accepted) ui->lineEdit->setText("Accepted");
                                }@
                                This works perfectly for me, altough i know that i will have to learn slots&signals in future.
                                Thanks again for Your suggestions.
                                Best regards.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  sandy.pareek
                                  wrote on last edited by
                                  #16

                                  nicely explained, here...

                                  "my code, my life"

                                  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