Qt在MainWindow的对象成员中连接SIGNAL和SLOT [英] Qt Connecting SIGNAL and SLOT in object member of MainWindow

查看:906
本文介绍了Qt在MainWindow的对象成员中连接SIGNAL和SLOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类MyClass与:

   -  private:
pushButton * button;
void connectSignalAndSlot();
- private slot:
void buttonAction();

我想使用connectSignalAndSlot()在MyClass中连接这些文件,如下所示:

  void MyClass :: connectSignalAndSlot()
{
QObject :: connect(button,SIGNAL(clicked SLOT(buttonAction()));
}

这给我一个错误

 没有匹配的函数调用'QObject :: connect(QPushButton *& const char *,MyClass * const,const char *)' 

如果我使用MyClass继承QObject,程序会编译并启动,但是显示以下问题在我的应用程序输出窗格中:

  QObject :: connect:没有这样的插槽QObject :: buttonAction \myclass.cpp:48 

我必须将按钮和插槽公开并使用它们MainWindow类只?



感谢您的帮助!

解决方案 c> 继承自 QObject 并添加 Q_OBJECT 定义(头文件)中的$ / code>宏来使插槽/信号有效。

  class MyClass:public QObject 
{
Q_OBJECT

public:
....
}


I have a class MyClass with:

  - private: 
      pushButton *button;
      void connectSignalAndSlot();
  - private slot: 
      void buttonAction();

I want to connect these in MyClass using connectSignalAndSlot(), like so:

  void MyClass::connectSignalAndSlot()
  {
    QObject::connect(button,SIGNAL(clicked()),this,SLOT(buttonAction()));
  }

This gives me an error of

no matching function for call to 'QObject::connect(QPushButton*&, const char*, MyClass* const, const char*)';

If I inherit QObject with MyClass, the program compiles and starts, but then I get the following issues displayed in my Application Output pane:

QObject::connect: No such slot QObject::buttonAction() in ..\MyProject\myclass.cpp:48

Do I have to make the button and slot public and use them in the MainWindow class only? Is there no way to keep this at the MyClass level?

Thanks for your help!

解决方案

You must have MyClass inherit from QObject AND add Q_OBJECT macro in your MyClass definition (header file) to have slots/signals work.

class MyClass : public QObject
{
     Q_OBJECT

public:
     ....
};

这篇关于Qt在MainWindow的对象成员中连接SIGNAL和SLOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆