이 프로그램은 메인 창에서 mBulbPrimary
클래스 의 객체를 생성하고 그 객체 의 pointCloudBool
내용을 변경하는 계산을 수행합니다. actionSave_Mandelbulb
위젯을 클릭하면 새 창이 DialogSaveMB
나타나고 사용자는 파일 경로를 입력하고 pushButton_save
버튼을 클릭 할 수 있습니다 . 이것은 mBulbPrimary.savePrimary(filePath)
원래 객체 의 기능 을 호출하는 것으로되어 있습니다.
그러나 dialogsavemb.cpp 에서 mBulbPrimary 개체는 인식되지 않으며 이유를 이해할 수 없습니다. 객체는 public이고 mainwindow.h 는 dialogsavemb.cpp에 포함되어 있는데 왜 객체 나 메서드 / 함수에 액세스 할 수 없습니까?
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.
}
이것은 아마도 @hyde가 주석에서 설명하는 것입니다.
mainwindow.h에 다음을 추가하십시오.
private slots:
void saveMBulb(const QString &filePath);
mainwindow.cpp MainWindow
생성자에서 다음을 추가하십시오.
connect(&dialogSaveMB, &DialogSaveMB::mBulbPathChanged, this, &MainWindow::saveMBulb);
mainwindow.cpp에서 다음을 추가하십시오.
void MainWindow::saveMBulb(const QString &filePath)
{
// you could add some checks here, eg. !filePath.isEmpty() or QFileInfo(filePath).exists()
mBulbPrimary.savePrimary(filePath.toStdString());
}
dialogsavemb.h에서 다음을 추가하십시오.
signals:
void mBulbPathChanged(const QString &filePath) const;
dialogsavemb.cpp DialogSaveMB
생성자에서 다음을 변경하십시오.
//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());
});
당신은 할 수 없습니다 포함 할 필요가 minwindow.h
에 DialogSaveMB
코드입니다. 어쨌든 이것은 순환 종속성을 생성하기 때문에 좋지 않습니다 (MainWindow에는 대화 상자 헤더가 필요하고 대화 상자에는 MainWindow 헤더가 필요합니다-이상적이지 않음).
그리고 mBulbPrimary
공개 할 필요가 없습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다