Answer the question
In order to leave comments, you need to log in
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):
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};
..............................
}
PhotoWorkArea
be able to directly access other widgets that are included in MainWindow
. In other words, I need to have direct access from one included MainWindow
object to another. PhotoWorkArea
friend MainWindow
and include it main_window.hpp
in 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.hpp
in photo_work_area.cpp
, and write in each method in which I need access to the parent (in the hierarchy QObject
) the MainWindow
following:MainWindow *main_window = qobject_cast<MainWindow*>(this->parentWidget()->parentWidget());
Answer the question
In order to leave comments, you need to log in
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 =)
#include "parent.hpp"
int main()
{
Parent object;
return 0;
}
#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
#ifndef CHILD_HPP
#define CHILD_HPP
class Parent;
class Child
{
private:
Parent *parent;
public:
Child(Parent *parent);
~Child() = default;
};
#endif // CHILD_HPP
#include "parent.hpp"
#include <iostream>
void Parent::doNothing() const
{
std::clog << "Do nothing!\n" << std::endl;
}
#include "child.hpp"
#include "parent.hpp"
Child::Child(Parent *_parent) : parent(_parent)
{
parent->doNothing();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question