So I have a Qt class MyQtClass
, and a QOject inherited class Sender
. I want to access the Ui from the Sender
class (Which by the way only consists static members), so I set up the static Sender& instance()
, static void emitSignal()
functions and the QSignal Q_SIGNAL void mySignal()
in the Sender class (See the code below). In the Qt-class-header MyQtClass.h
I've set up the QSlot Q_SLOT void mySlot()
. I connect those two slots in the main.cpp
(const bool connected = QObject::connect(&Sender::instance(), &Sender::mySignal, &w, &MyQtClass::mySlot);
)
Via qDebug();
I found out that the connect is successful, and once I invoke the method emitSignal()
the Signal is being emitted. The Slot though is never being called.
I gues that my mistake is in the main at the connection.
Code:
Sender.h:
#pragma once
#include <QtWidgets/QMainWindow>
#include <qdebug.h>
class Sender : public QObject
{
Q_OBJECT
using QObject::QObject;
public:
/*----------------------------*/
/*---Lots of static Members---*/
/*----------------------------*/
static Sender& instance(){
static Sender m_instance;
return m_instance;
}
static void emitSignal() {
emit instance().mySignal();
}
Q_SIGNAL void mySignal() {
qDebug() << "Signal emitted!";
}
};
MyQtClass.h
#pragma once
#include <qdebug.h>
#include <QtWidgets/QMainWindow>
#include "ui_MyQtClass.h"
class MyQtClass : public QMainWindow
{
Q_OBJECT
public:
MyQtClass(QWidget *parent = Q_NULLPTR);
Q_SLOT void mySlot() {
qDebug() << "Slot invoked";
}
private:
Ui::MyQtClassClass ui;
};
MyQtClass.cpp
#include "MyQtClass.h"
#include "Sender.h";
#include <qdebug.h>
MyQtClass::MyQtClass(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
qDebug() << "Ui has been setup!";
}
main.cpp
#include "MyQtClass.h"
#include "Sender.h"
#include <qdebug.h>
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyQtClass w;
w.show();
const bool connected = QObject::connect(&Sender::instance(), &Sender::mySignal, &w, &MyQtClass::mySlot);
qDebug() << "Connection established: " << connected;
Sender::emitSignal();
return a.exec();
}
Thank you very much for your time!