插槽似乎无法在Qt应用程序中识别 [英] Slot seemingly not recognized in Qt app

查看:112
本文介绍了插槽似乎无法在Qt应用程序中识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在学习C ++和Qt4,但我碰到了一个绊脚石。



我有以下类和实现:

  class Window:public QWidget 
{
public:
Window();

public slots:
void run();

private:
// ...
};

 <$ (runBtn,SIGNAL(clicked()),this,SLOT(run() )); 

// ...
}
Window :: run()
{
// ...
} $ b b

但是,当我尝试构建和运行它时,虽然它构建正常,它立即退出与消息

  Object :: connect:没有这样的插槽QWidget :: run()

除非我做错了,Qt似乎没有识别槽 run()





p> 更新:



代码现在是:

 code> class Window:public QWidget 
{
Q_OBJECT
public:
Window(QWidget * parent = 0);

public slots:
void run();

private:
// ...
};

  Window :: Window(QWidget * parent):QWidget(parent)
{
// ...

connect(runBtn,SIGNAL ,this,SLOT(run()));

// ...
}
Window :: run()
{
// ...
} $ b b

程序仍然意外完成,但不再告诉我没有 QWidget :: run()

解决方案

可能你忘记了一个Q_OBJECT宏class?

  class Window:public QWidget 
{
Q_OBJECT
public:
Window()
...


I have been working on learning C++ and Qt4 recently, but I have hit a stumbling block.

I have the following class and implementation:

class Window : public QWidget
{
public:
    Window();

public slots:
    void run();

private:
    //...
};

and

Window::Window()
{
    //...

    connect(runBtn,SIGNAL(clicked()),this,SLOT(run()));

    //...
}
Window::run()
{
    //...
}

However, when I attempt to build and run it, although it builds just fine, it immediately exits out with the message

Object::connect: No such slot QWidget::run()

Unless I did something wrong, Qt does not seem to be recognizing the slot run()

Could anyone please help?


Update:

The code is now:

class Window : public QWidget
{
    Q_OBJECT
public:
    Window(QWidget *parent = 0);

public slots:
    void run();

private:
    //...
};

and

Window::Window(QWidget *parent) : QWidget(parent)
{
    //...

    connect(runBtn,SIGNAL(clicked()),this,SLOT(run()));

    //...
}
Window::run()
{
    //...
}

The program still "unexpectedly finished", but no longer tell me that there is no such thing as QWidget::run()

解决方案

Possibly you have forgotten a Q_OBJECT macro in your Window class?

class Window : public QWidget
{
Q_OBJECT
public:
    Window()
...

这篇关于插槽似乎无法在Qt应用程序中识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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