Passing Data between forms
-
wrote on 27 Jul 2017, 18:40 last edited by
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!
-
wrote on 27 Jul 2017, 18:52 last edited by
Make a privat Method in the profilesearch Class that returns the Data. In the mainview you can access now the Method.
-
Make a privat Method in the profilesearch Class that returns the Data. In the mainview you can access now the Method.
-
wrote on 27 Jul 2017, 19:50 last edited by
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
-
Make a privat Method in the profilesearch Class that returns the Data. In the mainview you can access now the Method.
@Fuel said in Passing Data between forms:
Make a privat Method
It must be public, else nobody outside the class can access it
-
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!
@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
-
wrote on 28 Jul 2017, 06:16 last edited by
Yes, sorry i mean a Public Method and a Private Membervariable. Or just a public Memebervariable
-
wrote on 28 Jul 2017, 14:09 last edited by
@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.
-
wrote on 31 Jul 2017, 20:02 last edited by
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
-
wrote on 1 Aug 2017, 03:30 last edited by
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"); }
-
wrote on 30 Apr 2020, 17:09 last edited by
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"