【问题标题】:Signals and Slots help QT信号和插槽帮助 QT
【发布时间】:2013-05-03 12:30:21
【问题描述】:

我有一个带有函数的 Ui 类,每次我从一个类发出信号时都想调用它,比如说测试。在 ui 函数中,我需要连接我的信号和插槽,但我尝试了 QT 文档中的代码并且没有运气

信号声明

signals:

void paint(int x, int y, int id);

信号发射

emit paint(x, y, id)

连接(m_test 是一个类对象)

connect(&m_test,SIGNAL(paint(int,int,int)), this, SLOT(uiFunction(int,int,int)));

收到此错误

错误:C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : 无法将参数 1 从 'uiFunction *' 转换为'const QObject *'

但我遵循了这个 QT 文档示例(计数器是类)

Counter a, b;
     QObject::connect(&a, SIGNAL(valueChanged(int)),
                      &b, SLOT(setValue(int)));

有什么想法吗?

【问题讨论】:

  • 这是说你的m_test 类型不是从QObject 派生的。
  • 好的,但 QT 文档中的示例中的那个也没有
  • class Counter : public QObject... 再试一次。
  • 别忘了 Q_OBJECT 宏

标签: c++ qt signals


【解决方案1】:

你需要你的 Ui 类继承自 QObject,然后在类的声明之后添加 QOBJECT 宏。例如

class Ui : public QObject
{
    QOBJECT

signals:
    void paint(int x, int y, int id);

private slots:
    void UiFunction(int x, int y, int id);
};

【讨论】:

    猜你喜欢
    • 2012-10-15
    • 2015-07-18
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多