Pass value to Dialog/set Values
-
Hello.
So, my MainWindow opens a Dialog window whenever a button is clicked.
I need to pass an integer value to the Dialog window from the MainWindow, then, the Dialog window will make a database query with that int value to fill its QLineEdit Widgets.
I've been trying to pass such value, but I had no success so far.
Also, there's no problem if the QLineEdit texts are previously set in the MainWindow before the execution. -
Hi and welcome to devnet,
You should show the code you are currently using.
-
Hello @SGaist
Ok, there is:mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QtSql> #include <QPushButton> #include "secdialog.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); SecDialog secdialog(); private slots: void on_editClientBtn_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include <secdialog.h> #include "dbConnection.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); dbConnection db; db.startdb(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_clientsTable_doubleClicked(const QModelIndex &tableIndex) { QString selectedClientId = tableIndex.sibling(tableIndex.row(), 0).data().toString(); selectedClientId.toInt(); } void MainWindow::on_editClientBtn_clicked() { SecDialog secdialog; secdialog.setModal(true); secdialog.exec(); }
secdialog.h
#ifndef SECDIALOG_H #define SECDIALOG_H #include <QDialog> #include "dbConnection.h" namespace Ui { class SecDialog; } class SecDialog : public QDialog { Q_OBJECT public: QSqlDatabase db=QSqlDatabase::database(); explicit SecDialog(QWidget *parent = nullptr); ~SecDialog(); private: Ui::SecDialog *ui; }; #endif
secdialog.cpp
#include "secdialog.h" #include <QSqlDatabase> #include <QSqlQuery> #include <QString> #include "mainwindow.h" SecDialog::SecDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SecDialog) { ui->setupUi(this); } SecDialog::~SecDialog() { delete ui; }
I know how to bind values and make queries , but i did not paste that part in this topic because that would make the code blocks too long...
-
Make your
QString selectedClientId
a member variable inMainWindow
, so it is available outsideon_clientsTable_doubleClicked
.In
on_editClientBtn_clicked
pass theselectedClientId
as a parameter tosecdialog
creation.Make
SecDialog
constructor accept thisselectedClientId
parameter, and use it for your database query.Or you can have
SecDialog
define a getter/setter for theclientId
instead of doing it via constructor parameter if you prefer. -
Thanks @JonB. Could you please guide me a little bit further on how to do it? I'm new to Qt and C++.
So, I will putQString selectedClientId
in the mainwindow.h file, right? And how can I makeSecDialog
accept that parameter? I remember trying something like that, but couldn't get it done.mainwindow.h
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); SecDialog secdialog(); QString selectedClientId; //like this?
-
public: QString selectedClientId; //like this?
Yes, like you have it. Though better in
private:
section, no need for it to bepublic
. That makes it a member variable, so when you set it inon_clientsTable_doubleClicked()
it is available outside.You can leave the
SecDialog secdialog
where it was inon_editClientBtn_clicked
, that does not need to be a member variable. But add a parameter toSecDialog
like:SecDialog::SecDialog(QString clientId, QWidget *parent) : QDialog(parent),
(Best
const QString &clientId
, but I didn't want to lay too much on you.) Do whatever you need to in that constructor with the passed-inclientId
, like using it in a query or saving it as a member variable there if you need it later than in the constructor.In
on_editClientBtn_clicked
create it likeSecDialog secdialog(selectedClientId, this); secdialog.exec();