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!
-
@Fuel said in Passing Data between forms:
Make a privat Method
It must be public, else nobody outside the class can access it
-
@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
-
@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: https://forum.qt.io/topic/81884/signals-and-slots-connect/4
-
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"); }
-
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"