EVARATE
EVARATE

Reputation: 135

Qt: Accessing a public object from another window

This program is supposed to create the object mBulbPrimary of the class pointCloudBool in the main window and perform calculations which change the content of that object. When clicking the actionSave_Mandelbulb widget a new window DialogSaveMB appears and the user can enter a filepath and click the pushButton_save button. This is supposed to call the mBulbPrimary.savePrimary(filePath) function of the original object.

However in dialogsavemb.cpp the mBulbPrimary object is not recogized and I don't understand why. The object is public and mainwindow.h is included in dialogsavemb.cpp so why can't it access the object or its methods/functions?

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "dialogsavemb.h"
#include "pointcloudbool.h"


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class DialogSaveMB;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    DialogSaveMB dialogSaveMB;  //Dialog window

    pointCloudBool mBulbPrimary;  //public mBulbPrimary object
private:
    Ui::MainWindow *ui;


private slots:
    //unrelated functions...

    void openSaveMBulbWindow();
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //unrelated connections...

    connect(ui->actionSave_Mandelbulb, SIGNAL(triggered(bool)),
            this, SLOT(openSaveMBulbWindow()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

//Implementation:

//unrelated functions...

void MainWindow::openSaveMBulbWindow(){
    dialogSaveMB.show();

}

dialogsavemb.h:

#ifndef DIALOGSAVEMB_H
#define DIALOGSAVEMB_H

#include <QDialog>

namespace Ui {
class DialogSaveMB;
}

class DialogSaveMB : public QDialog
{
    Q_OBJECT

public:
    explicit DialogSaveMB(QWidget *parent = nullptr);
    ~DialogSaveMB();

private:
    Ui::DialogSaveMB *ui;

private slots:
    void saveMBulb(std::string filePath);
};

#endif // DIALOGSAVEMB_H

dialogsavemb.cpp:

#include "dialogsavemb.h"
#include "ui_dialogsavemb.h"
#include "mainwindow.h"

DialogSaveMB::DialogSaveMB(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSaveMB)
{
    ui->setupUi(this);
    ui->radioButton_txt->setChecked(true);

    //Get filepath from a lineEdit widget of the dialog:
    std::string filePath = ui->lineEdit_filePath->text().toUtf8().constData();

    //unrelated connections...

    connect(ui->pushButton_save, SIGNAL(clicked(bool)),
            this, SLOT(saveMBulb(filePath)));

}

DialogSaveMB::~DialogSaveMB()
{
    delete ui;
}

void DialogSaveMB::saveMBulb(std::string filePath){
    mBulbPrimary.savePrimary(filePath);
    //The above line needs to access the 'mBulbPrimary' object in the MainWindow class
    //but it can't.
}

Upvotes: 0

Views: 742

Answers (1)

Maxim Paperno
Maxim Paperno

Reputation: 4859

This is probably what @hyde is describing in the comments:

In mainwindow.h add:

private slots:
  void saveMBulb(const QString &filePath);

In mainwindow.cpp MainWindow constructor add:

  connect(&dialogSaveMB, &DialogSaveMB::mBulbPathChanged, this, &MainWindow::saveMBulb);

In mainwindow.cpp add:

void MainWindow::saveMBulb(const QString &filePath)
{
  // you could add some checks here, eg. !filePath.isEmpty() or QFileInfo(filePath).exists()
  mBulbPrimary.savePrimary(filePath.toStdString());
}

In dialogsavemb.h add:

signals:
  void mBulbPathChanged(const QString &filePath) const;

In dialogsavemb.cpp DialogSaveMB constructor, change:

  //connect(ui->pushButton_save, SIGNAL(clicked(bool)), this, SLOT(saveMBulb(filePath)));

  connect(ui->pushButton_save, &QPushButton::clicked, this, [this]() {
    emit mBulbPathChanged(ui->lineEdit_filePath->text());
  });

You do not need to include minwindow.h in the DialogSaveMB code. This is poor practice anyway since it creates a circular dependency (MainWindow need the dialog header, and the dialog needs MainWindow header -- not ideal).

And mBulbPrimary does not need to be public.

Upvotes: 1

Related Questions