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. Passing Data between forms
QtWS25 Last Chance

Passing Data between forms

Scheduled Pinned Locked Moved General and Desktop
formsqtableviewsignal & slot
11 Posts 5 Posters 13.0k 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.
  • F Offline
    F Offline
    Fuel
    wrote on 27 Jul 2017, 18:52 last edited by
    #2

    Make a privat Method in the profilesearch Class that returns the Data. In the mainview you can access now the Method.

    C J 2 Replies Last reply 27 Jul 2017, 19:42
    0
    • F Fuel
      27 Jul 2017, 18:52

      Make a privat Method in the profilesearch Class that returns the Data. In the mainview you can access now the Method.

      C Offline
      C Offline
      Core2
      wrote on 27 Jul 2017, 19:42 last edited by
      #3

      @Fuel Could you demonstrate this in an example?

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fuel
        wrote on 27 Jul 2017, 19:50 last edited by
        #4

        Example

        QString ProfileSearch::getData()
        {
            return data; //return some Data. In this Case a QString
        }
        
        MainView view;
        view.show();
        QString data = view.getData(); //get the Data from the other Widget
        
        1 Reply Last reply
        0
        • F Fuel
          27 Jul 2017, 18:52

          Make a privat Method in the profilesearch Class that returns the Data. In the mainview you can access now the Method.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 28 Jul 2017, 05:53 last edited by
          #5

          @Fuel said in Passing Data between forms:

          Make a privat Method

          It must be public, else nobody outside the class can access it

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Core2
            27 Jul 2017, 18:40

            Hello,

            I am struggling to learn how to pass variables between forms. I have a mainview.ui and a profilesearch.ui. The mainview opens up first, and from this ui i open the profilesearch. In the profilesearch.ui I query a sql db and display the data in a Qtableview. I would like to pass a row of data from the Qtableview if one of two things happen. 1. Single click any cell in the row and then select a push button. 2. double click any cell in the row that they want to select.

            So I want to pass the data from the Profilesearch.ui to the mainview.ui. How may I do this?

            Thanks in advance!

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 28 Jul 2017, 05:54 last edited by
            #6

            @Core2 It depends on when you need to pass the data. For example if you want to pass data while creating a window you can pass that data as parameter to the constructor. Other way is what @Fuel said (except the method must be public). Or you can use signals/slots, see http://doc.qt.io/qt-5.9/signalsandslots.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • F Offline
              F Offline
              Fuel
              wrote on 28 Jul 2017, 06:16 last edited by
              #7

              Yes, sorry i mean a Public Method and a Private Membervariable. Or just a public Memebervariable

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Core2
                wrote on 28 Jul 2017, 14:09 last edited by
                #8

                @jsulm I would like to send data when I close the profilesearch form. So the sql query has already ran and the data is being displayed in the QTableView. The user can either:
                1. single click a cell in the row and then select a push button that passes the data and closes the
                profilesearch form.
                2. double click any cell and the row that cell belongs to gets passed to the mainview form and close the
                profile search form.
                Still, should I make this public Member (Method)?

                If anyone is able to get really specific for me that would be most good.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Core2
                  wrote on 31 Jul 2017, 20:02 last edited by
                  #9

                  All,

                  I was able to get it working by using mrjj's instruction found on this topic: https://forum.qt.io/topic/81884/signals-and-slots-connect/4

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Andrey Shmelew
                    wrote on 1 Aug 2017, 03:30 last edited by
                    #10

                    I may be wrong but you can use signal and slot (on changing textbox's text example):

                    //mainwindow.cpp constructor:
                    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
                    {
                    // connection for accessing to UI from another UI
                       ProfileSearch* profilesearch = new ProfileSearch;
                        connect( profilesearch, SIGNAL(updateUI1Signal(const QString)), this, SLOT(updateUI2TextSlot(const QString) ) ); 
                    // launch profilesearch form:
                    profilesearch.exec();
                    }
                    
                    //mainwindow.h
                    {
                    public slots:
                        void updateUI2TextSlot( const QString text ) {ui->MWtextEdit->setText(text);} // set new text in mainwindow form
                    }
                    
                    //profilesearch.h
                    signals:
                        void updateUI1Signal(const QString text);
                    

                    // row click event handler

                    void ProfileSearch::on_row_column_whatewer_clicked()
                    {
                    emit    updateUI1Signal("Hello from ProfileSearch form to MainWindow form");
                    }
                    
                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      red_dragoon
                      wrote on 30 Apr 2020, 17:09 last edited by
                      #11

                      You might get error is the following statement, since ui is not included
                      ui->MWtextEdit->setText(text)

                      which can be got ridden by adding following syntax in in mainwindow.h
                      #include "ui_mainwindow.h"

                      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