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

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



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • 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


  • Lifetime Qt Champion

    What passSave method ?



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



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


Log in to reply