Get access to parent variables from child class in qt - c++

I have two class in Qt. In one I declared some variables and child QFrame class with QPainter. Now, if it's possible, how i can get access to parent variables from child class?
I know that I can pass variables by signals and slots or catch child QPainter events, but i think it would be nice get access directly.

It boils down to the visibility of the data in the base class. If the data is public or protected then you have access to it. Otherwise, the data is private and you don't have direct access to it.

Not exactly. Simple example:
header parent
class gameWindow : public QWidget
{
Q_OBJECT
public:
gameWindow(QWidget *parent = 0);
int round;
};
class parent
#include "gamewindow.h"
gameWindow::gameWindow(QWidget *parent) :
QWidget(parent)
{
round = 0;
}
header child :
class plArea:public QWidget
{
Q_OBJECT
public:
plArea(QWidget *parent=0);
};
class child:
#include "plarea.h"
plArea::plArea(QWidget *parent):QWidget (parent)
{
parent->round = 1;
}
return
'class QWidget' has no member named
'round'

Related

Qml not receiving updated value of member variable of base class

I basically just want to use multiple derived classes to change a member variable of a base class and to forward that value to qml using qproperty, but for some reason it's not working
car.h
#include <QObject>
class Car : public QObject{
Q_OBJECT
Q_PROPERTY(int seats MEMBER m_seats NOTIFY updateSeats)
public:
explicit Car(QObject *parent = 0);
~Car();
int m_seats;
Q_INVOKABLE void test();
signals:
void updateSeats();
};
car.cpp
#include "car.h"
Car::Car(QObject *parent) :
QObject(parent),
m_seats(0)
{
}
Car::test(){
m_seats = 5;
emit updateSeats();
}
Car::~Car(){}
toyota.h
#include "car.h"
class Toyota : public Car{
Q_OBJECT
public:
explicit Toyota(QObject *parent = 0);
~Toyota();
void foundCar();
};
toyota.cpp
#include "toyota.h"
Toyota::Toyota(QObject *parent)
{
}
Toyota::foundCar(){
m_seats = 4;
emit updateSeats();
}
Toyota::~Toyota(){}
Now, after invoking the foundCar function in class Toyota, if I do
console.log(car.seats) in qml I get 0, but I expect it to be 4 because I am modifying it in the derived class.
However if I call car.test() from qml and then I print car.seats, the value is 5. I am confused why this is the case. In qml I want car.seats to be 4. What am I missing?
Toyota object is derived class object Toyota of base Car not an object of Car you are modifying derived class object member Toyota::m_seats and that won't have any effect on direct base Car object .. and because Q_PROPERTY is defined only in base class Car .. the only value QML would see is base class object .. and specifically the object you set in setContextProperty ... the code you omitted after the post edit.
From previous edits in your post, you seem to set the setContextProperty in your engine as Car object .. its in this object where you need to modify member that is a Q_PROPERTY.
I am referring to your code:
Car::startGui(){
QQmlApplicationEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("car", this)
// start engine, which works properly
}

How to access qabstractlistmodel derived class object as a property of another class from qml?

Iam designing a qml page which consists of 3 lists. I want the data to be displayed in these lists as model from cpp. Can i have all these 3 models as properties from a single class.
I have a class derived from qabstractlistmodel to use as model. I want this model as a property from another class which is bind to qml using qqmlcontextproperty.
ie. I could be able to access this model as a property.
class ToDoModel : public QAbstractListModel
{
Q_OBJECT
...
}
class HelperClass : public QObject
{
Q_OBJECT
Q_PROPERTY(ToDoModel todoModel READ todoModel CONSTANT)
public:
explicit HelperClass(QObject *parent = nullptr);
ToDoModel* todoModel() const;
signals:
public slots:
private:
ToDoModel *_todoModel;
};
int main(int argc, char *argv[])
{
HelperClass helperClass;
engine.rootContext()->setContextProperty(QStringLiteral("helperClass"), &helperClass);
...
}
It shows the error :
Unable to handle unregistered datatype 'ToDoModel' for property 'HelperClass::todoModel'
A QObject, like the QAbstractListModel, is not copied, so in that case you must return the pointer. So in general if T is a QObject then if you expose it as a property it must be Q_Property(T* name ...).
So in your case it changes to:
class HelperClass : public QObject
{
Q_OBJECT
Q_PROPERTY(ToDoModel* todoModel READ todoModel CONSTANT)
// ...
You can use friend class to allow access to private and protected members of other class

qt - send signal between objects in a class with different parents

I have a class ("controller" for example)
and in this class, I have created many objects of different other classes
with different parents.
How to send signal between that classes and "controller" to call a function in "controller" class?
#include "controller.h"
Controller::Controller(QObject *parent) : QObject (parent){
connect(sender(), SIGNAL(recivedCall(QString)), this, SLOT(alert(QString)));
}
void Controller::onCall(QJsonObject callinfo){
nodes[callinfo["username"].toString()]= new PanelManager();
nodes[callinfo["username"].toString()]->handleCallStateChanged(callinfo);
}
void Controller::alert(QString callinfo){
qDebug()<<callinfo;
}
For example, how to send signal from "recivedCall" in each "PanelManager" object to call "alert" function in "controller" class ?
The object which creates your two components has to set the connections between your signal and your slot. But, you shouldn't expose inner components (i.e. create getter to return a pointer on a attribute).
A way to tackle the last problem with Qt is to create a signal in your parent and let it broadcast the calls.
For example, if I need to connect a QCheckBox to a QLineEdit in two different widgets:
class Parent1: public QWidget
{
Q_OBJECT
public:
Parent1(): QWidget(), myCheckBox(new QCheckBox("Edit", this))
{
connect(myCheckBox, &QCheckBox::clicked, this, &Parent1::editForbidden);
}
private:
QCheckBox* myCheckBox;
signals:
void editForbidden(bool);
};
class Parent2: public QWidget
{
Q_OBJECT
public:
Parent2(): QWidget(), myLineEdit(new QLineEdit("Edit", this))
{
connect(this, &Parent2::forbidEdit, myLineEdit, &QLineEdit::setReadOnly);
}
private:
QLineEdit* myLineEdit;
signals:
void forbidEdit(bool);
};
// In the parent of Parent1 and Parent2 (or in the main if there is no parent)
QObject::connect(p1, &Parent1::editForbidden, p2, &Parent2::forbidEdit);
In this example, when I click on the checkbox in parent1, the lineEdit in parent2 is disabled. But, Parent1 doesn't know anything about Parent2.

How to use the creating class` methods from the created class?

I have a question:
I have a class userinterface that has a class MoveSeries. From MoveSeries I want to have access to the methods of my class userinterface. In this example I want to have access to the method get_MoveCurve_Delta() of userinterface. How do I get access to the creating class (userinterface) from the created class (MoveSeries ? I tried the Signal-Slot-Approach but since I have to use several methods of userinterface several times this makes lots of signal-slots...
here is my code:
Userinterface.h:
class UserInterface : public QMainWindow
{
Q_OBJECT
public:
UserInterface(QWidget *parent = 0, Qt::WFlags flags = 0);
~UserInterface();
...
private:
double MoveCurve_Delta;
MoveSeries *MOVE_SERIES ;
public:
void set_MoveCurve_Delta( double val) { MoveCurve_Delta = val;}
double get_MoveCurve_Delta() { return MoveCurve_Delta ;}
}
Userinterface.cpp:
UserInterface::UserInterface(QWidget *parent, Qt::WFlags flags) :
QMainWindow(parent, flags)
{
ui.setupUi(this);
...
MOVE_SERIES = new MoveSeries( this);
}
MoveSeries.h:
class MoveSeries : public QDialog
{
Q_OBJECT
public:
explicit MoveSeries(QWidget *parent = 0);
~MoveSeries();
...
MoveSeries.cpp:
MoveSeries::MoveSeries(QWidget *parent) :
QDialog(parent),ui(new Ui::MoveSeries)
{
ui->setupUi(this);
this->parent = parent;
parent->set-MoveSeries_Delta_Val();
}
Rather than assume that the parent QWidget in MoveSeries is UserInterface, you can also require that it is.
MoveSeries.h:
class UserInterface; // only need a forward declaration
class MoveSeries : public QDialog
{
Q_OBJECT
public:
explicit MoveSeries(UserInterface *parent = 0);
~MoveSeries();
...
UserInterface * uiparent;
}
MoveSeries.cpp:
#include "Userinterface.h" // include the header where it is required
MoveSeries::MoveSeries(UserInterface *parent) :
QDialog(parent), ui(new Ui::MoveSeries), uiparent(parent)
{
ui->setupUi(this);
uiparent->set-MoveSeries_Delta_Val();
}
It looks like you want to cast the parent to the class you want:
static_cast<UserInterface *>(parent)->get_MoveCurve_Delta();
Bear in mind that this could be dangerous as it makes an assumption about the type of the parent.
If you want only UserInterface be the parent of MoveSeries, say so:
explicit MoveSeries(UserInterface *parent = 0);
If you want any widget to be able to act as the parent, you cannot access UserInterface methods because the parent does not necessarily have them.

How to call to non-static parent function from within child function in Qt/C++?

I have difficulties in understanding how to pass parent object to child.
In Qt, I have a MainWindow class and a DoSomething() function. Then I created a Job object within MainWindow and tried to call DoSomething within Job's DoItNow() function. But I just don't know how to do it.
MainWindow.h
class Job;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int value;
void DoSomething();
private:
Job *job;
}
MainWindow.cpp
#include "mainwindow.h"
#include "job.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
job = new Job(this); // passing this pointer to child
}
void MainWindow::DoSomething() { // do something }
Job.h
class Job : public QObject
{
Q_OBJECT
private:
void DoItNow();
public:
explicit CDMcommand(QObject *parent = 0);
}
Job.cpp
#include "job.h"
#include "mainwindow.h"
Job::Job(QObject *parent) : QObject(parent)
{
// some setups
parent->value = 0; // this is not working
}
void Job::DoItNow()
{
parent->DoSomething(); // What is the pointer to MainWindow instance?
}
How to access non-static public register in *parent?
How to pass *parent to function in job instance?
Maybe I missunderstand the question, but I think you are a bit confused about inheritance. Your Job is a child class of QObject and MainWindow indirectly inherits also form QObject, but there is no direct relation between MainWindow and Job. I am not too familiar with Qts signal and slot mechanism, which is probably the way to go here, but maybe I can offer you a different solution:
Job::Job(QObject *parent) : QObject(parent)
{
// some setups
parent->value = 0; // this is not working
}
This is not working, because QObject has no member called value. If you can live with Jobs constructor not taking a QObject* as parameter, then just declare a
MainWindow* parentWindow;
as a private member in Job and change the constructor to
Job::Job(MainWindow *parentWindow) : QObject(parentWindow)
{
// some setups
parentWindow->value = 0; // this will work now
}
then also
void Job::DoItNow()
{
parentWindow->DoSomething();
}
will work without problems.
How can I call the Qt 'parent' object method from the 'child'
object method?
The safe and simple way to do it:
void Job::DoItNow()
{
// first evaluate the pointer: is that of type we expect?
MainWindow* pMainWindow = qobject_cast<MainWindow*>(parent());
if (pMainWindow)
pMainWindow->DoSomething(); // MainWindow::DoSomething must be exposed to class Job
}
But of course making two classes dependent on each other too much is a violation of OOP principles: these two objects become tightly coupled now. And there is already a good suggestion in comments: use an explicit signal-slot mechanism for that or providing the interface to interact between decoupled objects.