Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.


  • Lifetime Qt Champion

    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 in MainWindow, so it is available outside on_clientsTable_doubleClicked.

    In on_editClientBtn_clicked pass the selectedClientId as a parameter to secdialog creation.

    Make SecDialog constructor accept this selectedClientId parameter, and use it for your database query.

    Or you can have SecDialog define a getter/setter for the clientId 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 put QString selectedClientId in the mainwindow.h file, right? And how can I make SecDialog 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?
    


  • @nicmorais

    public:
    QString selectedClientId; //like this?
    

    Yes, like you have it. Though better in private: section, no need for it to be public. That makes it a member variable, so when you set it in on_clientsTable_doubleClicked() it is available outside.

    You can leave the SecDialog secdialog where it was in on_editClientBtn_clicked, that does not need to be a member variable. But add a parameter to SecDialog 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-in clientId, 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 like

        SecDialog secdialog(selectedClientId, this);
        secdialog.exec();
    


  • Wow! Thank you @JonB, this solved my issue. One more solved topic to the forum.
    Have a nice a nice day!