Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass value to Dialog/set Values
QtWS25 Last Chance

Pass value to Dialog/set Values

Scheduled Pinned Locked Moved Solved General and Desktop
qdialogpass valueqlineedit
7 Posts 3 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nicmoraisN Offline
    nicmoraisN Offline
    nicmorais
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should show the code you are currently using.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • nicmoraisN Offline
        nicmoraisN Offline
        nicmorais
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          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.

          1 Reply Last reply
          2
          • nicmoraisN Offline
            nicmoraisN Offline
            nicmorais
            wrote on last edited by
            #5

            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?
            
            JonBJ 1 Reply Last reply
            0
            • nicmoraisN nicmorais

              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?
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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();
              
              1 Reply Last reply
              3
              • nicmoraisN Offline
                nicmoraisN Offline
                nicmorais
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved