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. How do I use an instance of a class that is in mainwindow from another window
QtWS25 Last Chance

How do I use an instance of a class that is in mainwindow from another window

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 267 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.
  • A Offline
    A Offline
    AI_Messiah
    wrote on last edited by
    #1

    So what I want is to use a value that I have in my dialog and call a method of a class that is being used in them mainwindow

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

      Hi,

      Can you explain how you use that dialog ?
      What is the relation between the mainwindow and the dialog ?

      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
      0
      • A Offline
        A Offline
        AI_Messiah
        wrote on last edited by
        #3

        This is my mainwindow class
        header

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include "validate.h"
        #include "dialog.h"
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
            Validate valid;
        private slots:
            void on_pushButton_2_clicked();
        
            void on_lineEdit_returnPressed();
        
        private:
            bool isvalid;
            Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_H
        
        

        source

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            isvalid = false;
            if (!valid.haspass){
                Dialog mydia;
                mydia.setModal(true);
                mydia.exec();
            }
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        
        void MainWindow::on_pushButton_2_clicked()
        {
            if (isvalid){
                Dialog mydia;
                mydia.setModal(true);
                mydia.exec();
            }
        }
        
        void MainWindow::on_lineEdit_returnPressed()
        {
            QString myen;
            myen = ui->lineEdit->text();
        
            isvalid = (valid.passInput(myen.toUtf8().constData()));
        }
        
        

        this is my dialog class
        header

        #ifndef DIALOG_H
        #define DIALOG_H
        #include <QtWidgets>
        #include <QDialog>
        #include <string>
        using namespace std;
        namespace Ui {
        class Dialog;
        }
        
        class Dialog : public QDialog
        {
            Q_OBJECT
        
        public:
            explicit Dialog(QWidget *parent = nullptr);
            ~Dialog();
            string entry;
        private slots:
            void on_buttonBox_clicked(QAbstractButton *button);
        
            void on_buttonBox_accepted();
        
            void on_buttonBox_rejected();
        
        private:
            Ui::Dialog *ui;
        };
        
        #endif // DIALOG_H
        
        

        source:

        #include "dialog.h"
        #include "ui_dialog.h"
        
        Dialog::Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
        {
            ui->setupUi(this);
        }
        
        Dialog::~Dialog()
        {
            delete ui;
        }
        
        
        
        void Dialog::on_buttonBox_accepted()
        {
            QString myen;
            myen = ui->lineEdit->text();
            entry = myen.toUtf8().constData();
        
        }
        
        void Dialog::on_buttonBox_rejected()
        {
        
        }
        
        

        I want to access the passSave method within valid from the dialog

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

          What passSave method ?

          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
          • A Offline
            A Offline
            AI_Messiah
            wrote on last edited by
            #5

            I access a method in the instance of the Validate class valid

            1 Reply Last reply
            0
            • nageshN Offline
              nageshN Offline
              nagesh
              wrote on last edited by
              #6

              @AI_Messiah If I understood your requirements correctly then

              In MainWindow you are creating valid instance and Dialog instance..

              Hence make in Dialog class to take valid object as arguments during construction..

              In Dialog store the reference to valid instance..

              And call the required method of valid class..

              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