EVARATE
EVARATE

Reputation: 135

Qt: How to give MainWindow access to data of its child

In my MainWindow there is a button that opens the MbulbWindow. There one can change some parameters and click a Generate button which calls the generateMBulb() function which does some calculation and in the end creates an object named entity of the class internalEntity.

Now I want to "send" this entity to the MainWindow where it will be saved for later use. But how do I do that? My idea was to create the transferEntity(internalEntity& entity) signal and connect it to the addEntity(internalEntity& entity) function of the MainWindow but Qt always gives an error message similar to the following:

QObject::connect: No such signal MbulbWindow::MBulbWindow::transferEntity() in ../MandelbulbUI_rewrite/mainwindow.cpp:15
QObject::connect:  (sender name:   'MbulbWindow')
QObject::connect:  (receiver name: 'MainWindow')

Here are my files:

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWindow>
#include <QAction>
#include "mbulbwindow.h"
#include "internalentityhandler.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    internalEntityHandler entityHandler; //Saves and manages entities
    MbulbWindow *mBulbWindow = new MbulbWindow;
private slots:

    //...

    //Generate
    void showMBGenerator();

    //internal entity management
    void addEntity(internalEntity& entity);

};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

    //...

    //Connections
    internalEntity entity;
    connect(mBulbWindow, SIGNAL(MBulbWindow::transferEntity()), this, SLOT(addEntity()));
    //This is where I would make the connection
}

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

//...

//Generate
void MainWindow::showMBGenerator(){
    mBulbWindow->show();
}

//internal entity management
void MainWindow::addEntity(internalEntity& entity){
    //Entity in memory:
    entityHandler.addEntity(entity);

    //...
}

mbulbwindow.h:

#ifndef MBULBWINDOW_H
#define MBULBWINDOW_H

#include <QDialog>
#include <vector>
#include "boolcloud.h"
#include "internalentityhandler.h"

namespace Ui {
class MbulbWindow;
}

class MbulbWindow : public QDialog
{
    Q_OBJECT

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

private:
    Ui::MbulbWindow *ui;

private slots:
    void generateMBulb();
signals:
    void transferEntity(internalEntity& entity);
};

#endif // MBULBWINDOW_H

mbulbwindow.cpp:

#include "mbulbwindow.h"
#include "ui_mbulbwindow.h"

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

    //Connections
    connect(ui->ButtonGenerate, SIGNAL(clicked()), this, SLOT(generateMBulb()));

}

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

void MbulbWindow::generateMBulb(){

    //Calculation of "mBulb" object...

    //Create entity
    internalEntity entity(mBulb, "Mandelbulb");

    emit transferEntity(entity);
    //This entity needs to be transferred to the MainWindow
    this->close();
}

Upvotes: 2

Views: 232

Answers (1)

Object object
Object object

Reputation: 2054

Your problem is that you are not using connect properly. Firstly the way you have used connect with SIGNAL and SLOT is invalid. Secondly you have not supplied the correct signiture of your signal and slot methods. You must provide the paramaters for each (when using SIGNAL and SLOT, there is another way I have outlined below)

Here:

connect(mBulbWindow, SIGNAL(MBulbWindow::transferEntity()), this, SLOT(addEntity()));

Ignoring the fact this is not a valid way to use SIGNAL and SLOT with connect for a moment, you are saying that MBulbWindow has a signal called transferEntity with no parameters, when in actaul fact transferEntity has a parameter of type internalEntity &.

You therefore need to connect it as such:

connect(mBulbWindow, SIGNAL(transferEntity(internalEntity&)), this, SLOT(addEntity(internalEntity&)));

Qt will now be able to find the right signal to bind too.

An alternative and IMO better way of doing it would be to use the newer syntax of connect. Like this:

connect(mBulbWindow, &MBulbWindow::transferEntity, this, &MainWindow::addEntity)

This saves you some typing, allows you to refactor both the signal and slot easier and IMO looks cleaner. It also has the advantage that type checks are performed to ensure that the correct type is passed to the slot (implicit conversion may still occur).

Upvotes: 3

Related Questions