I have a question again:
I have a class PBV t(inherits from Tab) hat has a class Geometry. Geometry is the parent of Geo_1. From Geo_1 I want to have access to methods of PBV ( e.g. printContent . How do I do that? I am able to create Signal-Slots but since I have to use methods of PBV often that would make lots of Signal-Slots.
Here is my code:
PBV.h:
#include "../Geometry/Geo_1.h"
class PBV : public Tab
{
Q_OBJECT
public:
explicit PBV (QWidget *parent = 0);
~ PBV ();
virtual void printContent( QStringList *const qsl);
private:
Geo_1 *GEO_1;
Geometry *GEO;
}
PBV.cpp:
…
Geo_1 *GEO_1;
GEO_1 = new Geo_1(this);
GEO_1->set_LNE_default();
…
.
Geo_1.h:
#ifndef GEO_1_H
#define GEO_1_H
#include "Geometry.h"
#include "../Tabs/PBV.h"
class Geo_1: public Geometry
{
Q_OBJECT
public:
Geo_1 (QObject *_parent = 0);
virtual void set_LNE_default();
};
#endif // GEO_1_H
.
Geo_1.cpp:
#include "Geometry.h"
#include <QDebug>
#include "Geo_1.h"
#include "../Tabs/PBV.h"
Geo_1::Geo_1(QObject*_parent)
: Geometry(_parent) {}
void Geo_1::set_LNE_default()
{
// here I want to use printContent
}
.
Geometry.h:
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QObject>
class Geometry : public QObject
{
Q_OBJECT
public:
Geometry(QObject *_parent=0);
~Geometry();
virtual void set_LNE_default();
};
#endif // GEOMETRY_H
.
Geometry.cpp:
#include "Geometry.h"
#include <QDebug>
Geometry::Geometry(QObject *_parent)
: QObject(_parent) {}
Geometry::~Geometry(){}
void Geometry::set_LNE_default() { }
One approach, as referred to by comments, is to keep track of the parent class in the constructor of the child:
In Geometry.h, add a private member variable:
private:
PBV* m_pParentPBV;
Then, in the constructor in Geometry.cpp, set this to the parent you are already passing:
Geometry::Geometry(QObject *_parent)
: QObject(_parent)
{
m_pParentPBV = dynamic_cast<PBV*>(_parent);
}
Now you can call methods on the parent using m_pParentPBV->printContent() etc.
Related
I am new in C++ Qt and struggling with the correct use of forward declarations and #include.
What I want to do:
I have a Qt Gui (Class Ui::Gui) where we can set values.
I want to save these values in Gui Class variables.
As soon as a button (Generate Xml) is clicked, I want to pass the object
'ui' to the XmlGeneratorClass, So i can use the values to generate a Xml.
gui.h
#ifndef GUI_H
#define GUI_H
#include <QMainWindow>
#include <QDebug>
#include "xmlgeneratorqobject.h"
namespace Ui {
class Gui;
}
class Gui : public QMainWindow
{
Q_OBJECT
public:
explicit Gui(QWidget *parent = nullptr);
~Gui();
qint8 testvalue = 1;
signals:
void transmitToXmlGen(Ui::Gui*);
private slots:
void on_pushButtonGenerateXml_clicked();
private:
Ui::Gui *ui;
XmlGeneratorQObject *xmlgenerator = new XmlGeneratorQObject();
};
#endif // GUI_H
gui.cpp
#include "gui.h"
#include "ui_gui.h"
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui)
{
ui->setupUi(this);
connect(this,SIGNAL(transmitToXmlGen(Ui::Gui*)),xmlgenerator,SLOT(receiveFromGui(Ui::Gui*)));
}
Gui::~Gui()
{
delete ui;
}
void Gui::on_pushButtonGenerateXml_clicked()
{
emit transmitToXmlGen(ui);
}
xmlgeneratorqobject.h
#ifndef XMLGENERATORQOBJECT_H
#define XMLGENERATORQOBJECT_H
#include <QObject>
#include <QDebug>
namespace Ui {
class XmlGeneratorQObject;
class Gui;
}
class XmlGeneratorQObject : public QObject {
Q_OBJECT
public:
explicit XmlGeneratorQObject(QObject * parent = nullptr);
private slots:
void receiveFromGui(Ui::Gui*);
};
#endif // XMLGENERATORQOBJECT_H
xmlgeneratorqobject.cpp
#include "xmlgeneratorqobject.h"
XmlGeneratorQObject::XmlGeneratorQObject(QObject *parent){}
void XmlGeneratorQObject::receiveFromGui(Ui::Gui* objectFromGui)
{
qDebug() << objectFromGui->testvalue; // ERROR member access into incomplete type 'Ui::Gui'
}
Expected result:
Access to public variables from passed gui-object should be possible
Actual result:
member access into incomplete type 'Ui::Gui'
Can you please help me learn forward declaration / include?
Is my approach in general okay?
Your xmlgeneratorqobject.cpp needs the line
#include "ui_gui.h"
This gives it the details of the ui widgets. This file is generated by the Qt build system.
In one of my project, I am trying to use the Qt signals and slots mechanism with a std::vector<bool> as parameter. My project is equivalent to the following minimal code :
class App
// app.h
#ifndef APP_H
#define APP_H
#include <QObject>
#include <QSharedPointer>
#include "emitter.h"
#include "receiver.h"
class App : public QObject
{
Q_OBJECT
public:
App(QObject *parent = 0);
};
#endif // APP_H
// app.cpp
#include "app.h"
App::App(QObject* parent): QObject(parent)
{
Emitter emitter;
Receiver receiver;
receiver.attachEmitter(emitter);
emitter.run();
}
class Emitter
//emitter.h
#ifndef EMITTER_H
#define EMITTER_H
#include <QObject>
#include <vector>
#include "helper.h"
class Helper;
class Emitter : public QObject
{
Q_OBJECT
public:
explicit Emitter(QObject *parent = 0);
void run();
signals:
void triggered(std::vector<bool> value);
};
#endif // EMITTER_H
// emitter.cpp
#include "emitter.h"
Emitter::Emitter(QObject *parent) : QObject(parent)
{
}
void Emitter::run()
{
emit triggered(Helper::value());
}
class Receiver
//receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QDebug>
#include <QSharedPointer>
#include "emitter.h"
class Emitter;
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject *parent = 0);
void attachEmitter(QSharedPointer<Emitter> emitter);
signals:
public slots:
};
//receiver.cpp
#include "receiver.h"
Receiver::Receiver(QObject *parent) : QObject(parent)
{
}
void Receiver::attachEmitter(QSharedPointer<Emitter> emitter)
{
connect(emitter.data(), &Emitter::triggered, [this](std::vector<bool> value) {
qDebug() << "Received value";
});
}
For some reason, the compiler doesn't like it at all and prints me this stack :
What do I have to do ? Thank you
Signal/slot values passend by value have to be registered with the Qt meta system and I don't think the std::vector is registered by default. Is there a reason you're not using QVectorinstead?
For futher information look at Q_DECLARE_METATYPE and qRegisterMetaType().
I`m still stuck.
I have a GUI-class that creates an Object. From a method of this new Object I want to use methods of the creating class
I have a Gui(PBV) with line_Edits and a Combobox. In this Combobox I can choose between different Geometries. Geo_1, Geo_2,… that inherit from Geometry. According to the entries in the combobox I want to create different Objects (Geo_1,Geo_2,…) that then set the lineEdits of the creating class according to the needs of the Geo_n-Object.
I don`want to do that with signal-slot
I asked different questions on that but I can`t get further.
I somehow feel this is kind of recursive... Is there a solution fort hat?
Here is my code:
PBV.h:
#include "../Geometry/Geo_1.h"
class PBV : public Tab
{
Q_OBJECT
public:
explicit PBV (QWidget *parent = 0);
~ PBV ();
virtual void printContent( QStringList *const qsl);
private:
Geo_1 *GEO_1;
Geometry *GEO;
}
PBV.cpp:
…
Geo_1 *GEO_1;
GEO_1 = new Geo_1(this);
GEO_1->set_LNE_default();
…
.
Geo_1.h:
#ifndef GEO_1_H
#define GEO_1_H
#include "Geometry.h"
#include "../Tabs/PBV.h"
class Geo_1: public Geometry
{
Q_OBJECT
public:
Geo_1 (QObject *_parent = 0);
virtual void set_LNE_default();
};
#endif // GEO_1_H
.
Geo_1.cpp:
#include "Geometry.h"
#include <QDebug>
#include "Geo_1.h"
#include "../Tabs/PBV.h"
Geo_1::Geo_1(QObject*_parent)
: Geometry(_parent) {}
void Geo_1::set_LNE_default()
{
// here I want to use printContent
}
.
Geometry.h:
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QObject>
class Geometry : public QObject
{
Q_OBJECT
public:
Geometry(QObject *_parent=0);
~Geometry();
virtual void set_LNE_default();
};
#endif // GEOMETRY_H
.
Geometry.cpp:
#include "Geometry.h"
#include <QDebug>
Geometry::Geometry(QObject *_parent)
: QObject(_parent) {}
Geometry::~Geometry(){}
void Geometry::set_LNE_default() { }
When PBV allocates a Geo_1 instance, it is already passing a pointer to itself in the constructor:
GEO_1 = new Geo_1(this); // "this" is your PBV instance
Why not save the pointer to use later?
Geo_1::Geo_1(PBV*_parent) : Geometry((QObject*)_parent)
{
this->pbv = _parent; // Save reference to parent
}
Then you can do:
void Geo_1::set_LNE_default()
{
// here I want to use printContent
this->pbv->printContent(...); // this->pbv is your saved ptr to the PBV that created this instance
}
EDIT
Additonally, you might run into the problem that you will have to cross-include the two headers (PBV and Geo_1), to fix this, remove the include for Geo_1 from PBV.h and forward-declare Geo_1. Just like this:
class Geo_1;
Before declaring your PBV class.
I want to write an application which consists of several plugins. My idea is that the plugin itself is always just a factory which then can create the required object.
So I created an Interface for the pluginfactory called AbstractFactoryPlugin with a pure virtual method called create. Because of the fact that I want to have several different plugins (factories) with the same interface I can’t put the Q_DECLARE_INTERFACE macro in the AbstractFactoryPlugin header file.
So for a sample plugin I created an Interface which inherits from AbstractFactoryPlugin called IMyObjectFactoryPlugin and declared this interface with the DECLARE macro. Further the MyObjectFactoryPlugin then inherits from QObject and IMyObjectFactoryPlugin.
But when I load the plugin and call the create function, the create function of MyObjectFactoryPlugin seems to be never called. What am I doing wrong?
Many thanx in advance.
Sourcecode:
#ifndef ABSTRACTPLUGINFACTORY_H
#define ABSTRACTPLUGINFACTORY_H
#include "IAnObject.h"
class AbstractFactoryPlugin
{
public:
virtual ~AbstractFactoryPlugin() {}
virtual IAnObject *create(QObject *parent) = 0;
};
#endif // ABSTRACTPLUGINFACTORY_H
#ifndef IMYOBJECTFACTORYPLUGIN_H
#define IMYOBJECTFACTORYPLUGIN_H
#include "AbstractFactoryPlugin.h"
#include <QtPlugin>
class IMyObjectFactoryPlugin : public AbstractFactoryPlugin
{
public:
virtual ~IMyObjectFactoryPlugin() {}
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(IMyObjectFactoryPlugin,
"org.MyObjectFactoryPlugin");
QT_END_NAMESPACE
#endif // IMYOBJECTFACTORYPLUGIN_H
#ifndef MYOBJECTPLUGIN_H
#define MYOBJECTPLUGIN_H
#include <QtPlugin>
#include "IMyObjectFactoryPlugin.h"
#include "AbstractFactoryPlugin.h"
class MyObjectFactoryPlugin : public QObject, IMyObjectFactoryPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.MyObjectFactoryPlugin" )
Q_INTERFACES(IMyObjectFactoryPlugin)
public:
MyObjectFactoryPlugin(QObject *parent = 0);
IAnObject *create(QObject *parent);
};
#endif // MYOBJECTPLUGIN_H
#include "MyObject.h"
#include "MyObjectFactoryPlugin.h"
#include <QDebug>
MyObjectFactoryPlugin::MyObjectFactoryPlugin(QObject *parent) :
QObject(parent)
{
}
IAnObject *MyObjectFactoryPlugin::create(QObject *parent)
{
qDebug() << Q_FUNC_INFO << "was called";
return new MyObject();
}
#ifndef IANOBJECT_H
#define IANOBJECT_H
class IAnObject
{
public:
IAnObject() {isInitialized = false;}
virtual ~IAnObject() {}
virtual bool initialize() = 0;
protected:
bool isInitialized;
};
#endif // IANOBJECT_H
#include "IAnObject.h"
#ifndef MYOBJECT_H
#define MYOBJECT_H
class MyObject : public IAnObject
{
public:
MyObject();
bool initialize();
};
#endif // MYOBJECT_H
#include "MyObject.h"
MyObject::MyObject()
{
}
bool MyObject::initialize()
{
return true;
}
#include <QCoreApplication>
#include <QDir>
#include <QPluginLoader>
#include <QDebug>
#include <E:/QtProjects/_test_PlugIn/MyPlugin/AbstractFactoryPlugin.h>
#include <E:/QtProjects/_test_PlugIn/MyPlugin/IAnObject.h>
#include <E:/QtProjects/_test_PlugIn/MyPlugin/IMyObjectFactoryPlugin.h>
IAnObject *loadPlugin()
{
QDir dir(QCoreApplication::applicationDirPath());
qDebug() << dir;
foreach(QString fileName, dir.entryList(QDir::Files))
{
QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
qDebug() << fileName;
if(plugin)
{
qDebug() << "##### Plugin load ok #####";
AbstractFactoryPlugin *abstractFactoryPlugin = reinterpret_cast<AbstractFactoryPlugin *>(plugin);
return abstractFactoryPlugin->create(NULL);
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "loadPlugin:" << loadPlugin();
return a.exec();
}
Any Qt5 plugin must inherit from QObject, if you want your base class to be independent from QObject you'll have to create an intermediary class that inherits both AbstractFactoryPlugin and QObject (let's call it AbstractMyObjectFactoryPlugin).
#ifndef ABSTRACTMYOBJECTFACTORYPLUGIN_H
#define ABSTRACTMYOBJECTFACTORYPLUGIN_H
#include "AbstractFactoryPlugin.h"
class AbstractMyObjectFactoryPlugin : public QObject, public AbstractFactoryPlugin
{
Q_OBJECT
public:
AbstractMyObjectFactoryPlugin(QObject *parent = 0) : QObject(parent) { }
virtual ~AbstractMyObjectFactoryPlugin() { }
};
Q_DECLARE_INTERFACE(AbstractMyObjectFactoryPlugin,
"org.AbstractFactoryPlugin");
#endif // ABSTRACTMYOBJECTFACTORYPLUGIN_H
To create a plugin, you'll have to inherit from this class and override the create() method:
class MyObjectFactoryPlugin : public AbstractMyObjectFactoryPlugin
{
Q_OBJECT
Q_INTERFACES(AbstractMyObjectFactoryPlugin)
Q_PLUGIN_METADATA(IID "org.MyObjectFactoryPlugin" FILE "MyObjectFactoryPlugin.json")
public:
MyObjectFactoryPlugin(QObject* parent = 0) : AbstractMyObjectFactoryPlugin(parent) { }
virtual IAnObject *create(QObject *parent);
virtual ~MyObjectFactoryPlugin() { }
};
To cast the loaded plugin (a QObject) to AbstractFactoryPlugin safetly, you'll have to cast it to the intermediary class first.
AbstractFactoryPlugin * objectFactoryPlugin = qobject_cast< AbstractMyObjectFactoryPlugin * >(plugin);
The implicit assignment will cast it to the right base class.
Note: You'll have to check if the cast is successful first if you have other inheritance trees.
Hello I keep getting this
error: expected class-name before '{' token
{
^ line 15
what does this error mean exactly? Im attempting to inherit the controller class that has a function that I need to call in form1's cpp.
#ifndef FORM1_H
#define FORM1_H
#include <QDialog>
#include "controller.h"
namespace Ui {
class form1 ;
}
class form1 : public QDialog, public controller
{
Q_OBJECT
public:
explicit form1(QWidget *parent = 0);
~form1();
private slots:
void on_pushButton_clicked();
private:
Ui::form1 *ui;
};
#endif // FORM1_H
the controller class
#include "controller.h"
#include "ui_controller.h"
controller::controller(QWidget *parent) :
QWidget(parent),
ui(new Ui::controller) {
ui->setupUi(this);
show(1); }
void controller::show(int x) {
if(x==1)
{
myform1 = new form1(this);
myform1->show();
}
if(x==2)
{
myform2 = new form2(this);
myform2->show();
}
if(x==3)
{
myform3 = new form3(this);
myform3->show();
} }
controller::~controller() {
delete ui; }
controller.h:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QWidget>
#include <form1.h>
#include <form2.h>
#include <form3.h>
namespace Ui {
class controller;
}
class controller : public QWidget
{
Q_OBJECT
public:
form1 * myform1;
form2 * myform2;
form3 * myform3;
void show(int x);
explicit controller(QWidget *parent = 0);
~controller();
private:
Ui::controller *ui;
};
#endif // CONTROLLER_H
My guess is you have a circular include issue, meaning controller.h includes, either directly or indirectly, form1.h.
EDIT: Change the include I was talking about to a forward declaration - you don't need the full definition of form1:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QWidget>
class form1;
class form2;
class form3;
namespace Ui {
class controller;
}
class controller : public QWidget
{
//.................
//.................