Important: Please read the Qt Code of Conduct -

Passing Data between forms

  • 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!

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

  • @Fuel Could you demonstrate this in an example?

  • Example

    QString ProfileSearch::getData()
        return data; //return some Data. In this Case a QString
    MainView view;;
    QString data = view.getData(); //get the Data from the other Widget

  • Lifetime Qt Champion

    @Fuel said in Passing Data between forms:

    Make a privat Method

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

  • Lifetime Qt Champion

    @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

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

  • @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.

  • All,

    I was able to get it working by using mrjj's instruction found on this topic:

  • 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:
    public slots:
        void updateUI2TextSlot( const QString text ) {ui->MWtextEdit->setText(text);} // set new text in mainwindow form
        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");

  • You might get error is the following statement, since ui is not included

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