0

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.
}
4
  • Qt way to solve this is, have the dialog just emit a signal with the file name in saveMBulb. Then in MainWindow, connect this signal to a slot which handles the file name. Commented Feb 6, 2020 at 21:01
  • I thought that too but couldn't figure out how to point from the dialog to the window. Commented Feb 6, 2020 at 21:03
  • When you create the dialog object (happens in MainWindows constructor here, automatically), just connect whatever signals you need. Commented Feb 6, 2020 at 21:11
  • Do you mean to make the connection inside of mainwindow.cpp? I guess that makes sense but I think that I don't understand pointers (or whatever is needed here) well enough. I wrote this into mainwindow.cpp which doesn't work: connect(dialogSaveMB.ui->pushButton_save, SIGNAL(clicked(bool)), this, SLOT(saveMBulb(filePath))); Commented Feb 6, 2020 at 21:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.