connect 函数失败,因为信号找不到对象

问题描述 投票:0回答:1

当调用

QObject::connect(*object,signal,*object,slot)
时,由于信号函数,我收到错误“cannot call a member function without object”。

我正在尝试将自定义按钮发出的信号连接到按钮容器类中定义的插槽。

class customButton : public QPushButton
{
    Q_OBJECT
    public:
        explicit customButton(QWidget *parent = 0);

    protected:
        virtual void hoverLeave(QEnterEvent* e);
        virtual void enterEvent(QEnterEvent* e);

    signals:
        void hovered();
};

在容器头中,我定义了一个

customButton
类的对象:

class board
{
    public:
        board();
        customButton* casilla = new customButton();

    public slots:
        void mostrarCasilla();
};

现在,在容器构造函数中,我尝试用这条线连接信号和槽

QObject::connect(casilla,&customButton::hovered(),this,board::mostrarCasilla());

但我收到此错误:

board.cpp:20: error: cannot call member function ‘void customButton::hovered()’ without object
board.cpp: In constructor ‘board::board()’:
board.cpp:20:52: error: cannot call member function ‘void customButton::hovered()’ without object
      |     QObject::connect(casilla,&customButton::hovered(),this,board::mostrarCasilla());
      |                               ~~~~~~~~~~~~~~~~~~~~~^~

物体是

casilla
,有信号
hovered()
,但是信号说没有物体。

c++ qt signals-slots qt-signals
1个回答
0
投票

&A::func
是一个成员函数指针,
&
的作用是获取func的地址。

对于

func()
来说,
()
是一个函数调用,也许你可以在
func()
之后得到一个值,但是这个值是右值,
&
需要一个左值,所以
&A::func()
在任何情况下都是错误的情况。

© www.soinside.com 2019 - 2024. All rights reserved.