E
E
Egorithm2020-07-12 16:06:35
Qt
Egorithm, 2020-07-12 16:06:35

How to correctly receive and pass data between classes that implement parts of the window interface?

In general, there is something like this code (simplified):

Example:
class MainWindow : public QWidget
{
Q_OBJECT

private:
    ..............................
    QSplitter *splitter;
    PhotoWorkArea *work_area;
    ..............................
}

MainWindow::MainWindow(QWidget *parent)
    : QWidget{parent}
{
    ..............................
    splitter = new QSplitter{this};
    work_area = new PhotoWorkArea{splitter};
    ..............................
}

I want to PhotoWorkAreabe able to directly access other widgets that are included in MainWindow. In other words, I need to have direct access from one included MainWindowobject to another.

I tried to make it a PhotoWorkAreafriend MainWindowand include it main_window.hppin photo_work_area.hpp, but since the second one is already included in the first one, it turns out mutual inclusion, which is why everything breaks. Therefore, the only option that came to my mind is to include main_window.hppin photo_work_area.cpp, and write in each method in which I need access to the parent (in the hierarchy QObject) the MainWindowfollowing:
MainWindow *main_window = qobject_cast<MainWindow*>(this->parentWidget()->parentWidget());

All this is somehow crutch and crooked. Tell me, how best to implement the plan? I think that this problem is not new, and many have already encountered it. But I don't know how to google it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egorithm, 2020-07-12
@EgoRusMarch

07/13/20
I came to this conclusion. If you need to receive and transmit data from some parts of the window interface to other parts of the interface, then it is better to do this through the MainWindow (or equivalent) slots. And instead of duplicating the data itself, create pointer fields to this data inside the classes that implement part of the window interface, and pass them through the getters and setters of the corresponding classes in the MainWindow constructor.
07/14/20
It looks like I found an easier way =)

main.cpp
#include "parent.hpp"

int main()
{
    Parent object;
    return 0;
}
parent.hpp
#ifndef PARENT_HPP
#define PARENT_HPP

#include "child.hpp"

class Parent
{
    friend class Child;

private:
    Child child{this};

public:
    Parent() = default;
    ~Parent() = default;

private:
    void doNothing() const;
};

#endif // PARENT_HPP
child.hpp
#ifndef CHILD_HPP
#define CHILD_HPP

class Parent;

class Child
{
private:
    Parent *parent;

public:
    Child(Parent *parent);
    ~Child() = default;
};

#endif // CHILD_HPP
parent.cpp
#include "parent.hpp"
#include <iostream>

void Parent::doNothing() const
{
    std::clog << "Do nothing!\n" << std::endl;
}
child.cpp
#include "child.hpp"
#include "parent.hpp"

Child::Child(Parent *_parent) : parent(_parent)
{
    parent->doNothing();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question