Solved Signals/Slots: GUI + Plugin Interface (API)
-
Hi, I would like to connect my UI Form Button to a Plugin API class slot function...but I got some Qt IDE erros:
My plugin class is called "Analysis X":class ANALYSIS_XSHARED_EXPORT Analysis_x: public QObject, public Plugin_API { Q_OBJECT Q_PLUGIN_METADATA(IID "com.lamar.plugin") Q_INTERFACES(Plugin_API) public: explicit Analysis_x(QObject* parent = nullptr); ~Analysis_x() override; void run(void) override; private: Form* _form; public slots: void onMessage(QString msg); };
My plugin GUI is a Form Class:
class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); signals: void mySignal(QString msg); private slots: void on_save_btn_clicked(); private: Ui::Form *ui; };
So, I tried to connect both using:
void Analysis_x::run() { qInfo() << "RUNNING: analysis X"; _form = new Form(); _form->show(); _form->connect(&_form, &_form->mySignal, &this, &this->onMessage); }
But I got this errors:
analysis_x.cpp:62:28: error: cannot create a non-constant pointer to member function analysis_x.cpp:62:46: error: cannot take the address of an rvalue of type 'Analysis_x *' analysis_x.cpp:62:53: error: cannot create a non-constant pointer to member function
What I have to do to connect these QObjects?
-
@mrjj perfect! Thank you! You are great!
-
@fem_dev said in Signals/Slots: GUI + Plugin Interface (API):
_form->connect(&_form, &_form->mySignal, &this, &this->onMessage);
The syntax is more like
_form->connect(_form, &Form::mySignal, this, this->onMessage);
Too many & and you must use the class name and not the instance for the function pointer. -
@mrjj said in Signals/Slots: GUI + Plugin Interface (API):
_form->connect(_form, &Form::mySignal, this, this->onMessage);
I tried it now, but I got:
analysis_x.cpp:63:56: error: reference to non-static member function must be called
-
Hi
did you use the keyword static in any place ?
als do you have the body defined for
void onMessage(QString msg);else check the syntax again
https://wiki.qt.io/New_Signal_Slot_SyntaxUpdate:
sorry, too hot.
The last part is also incorrect_form->connect(_form, &Form::mySignal, this, &Analysis_x::onMessage);
-
@mrjj
No, I don't use the keywordstatic
in any place.I defined
void onMessage(QString msg);
because I'm sending a message from a clicked button to my Plugin API:My Signal implemetation:
void Form::on_save_btn_clicked() { emit mySignal("Save"); }
My Slot implementation:
void Analysis_x::onMessage(QString msg) { qInfo() << "My msg: " << msg; }
What I'm doing wrong?
-
@fem_dev
hi you are mixing old and new syntax :)
try
form->connect(_form, &Form::mySignal, this, &Analysis_x::onMessage); -
@mrjj perfect! Thank you! You are great!