因此,我有一个Qt类MyQtClass
和一个QOject继承的类Sender
。我想从Sender
类访问Ui(顺便说一下,它只包含静态成员),所以我设置了static Sender& instance()
,static void emitSignal()
函数和QSignal Q_SIGNAL void mySignal()
在Sender类中(请参见下面的代码)。在Qt-class-header MyQtClass.h
中,我设置了QSlot Q_SLOT void mySlot()
。我将这两个插槽连接到main.cpp
(const bool connected = QObject::connect(&Sender::instance(), &Sender::mySignal, &w, &MyQtClass::mySlot);
)
通过qDebug();
,我发现连接成功,并且一旦调用方法emitSignal()
,就会发出信号。插槽永远不会被调用。
我猜我的错误主要出在连接上。
代码:
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();
}
非常感谢您的宝贵时间!
答案 0 :(得分:0)
我会检查您的构建系统。在没有重定义错误的情况下,您应该不能使用函数体编译Q_SIGNAL。如果可以构建它,则可能不涉及要使用的任何Qt机械。我对您的Visual Studio / mingw环境没有任何经验,但能够在Linux平台上构建以下内容。
main.cpp
#include <QDebug>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include "Sender.h"
#include "MyQtClass.h"
//#include "ui_MyQtClass.h"
MyQtClass::MyQtClass(QWidget *parent)
: QMainWindow(parent)
{
qDebug() << "Ui has been setup!";
}
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();
}
Sender.h
#pragma once
#include <QtWidgets/QMainWindow>
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();
};
MyQtClass.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QDebug>
class MyQtClass : public QMainWindow
{
Q_OBJECT
public:
MyQtClass(QWidget *parent = Q_NULLPTR);
Q_SLOT void mySlot() {
qDebug() << "Slot invoked";
}
private:
//Ui::MyQtClassClass ui;
};
然后我通过手动调用Meta-Object Compiler
来构建它moc Sender.h > moc_Sender.cpp
moc MyQtClass.h > moc_MyQtClass.cpp
g++ main.cpp moc_Sender.cpp moc_MyQtClass.cpp -I /usr/include/x86_64-linux-gnu/qt5/QtCore/ -I /usr/include/x86_64-linux-gnu/qt5 -fPIC -std=c++11 -I /usr/include/x86_64-linux-gnu/qt5/QtWidgets/ -lQt5Core -lQt5Widgets
产生了预期的输出
user@mintvm ~ $ ./a.out
Ui has been setup!
Connection established: true
Slot invoked
您是否尝试过使用qmake构建系统,以便您不必担心与元对象系统的编译有关的所有细节?