Connect: No such Slot QTreeView

时间秒杀一切 提交于 2019-12-11 13:33:56

问题


I have inherited a class MainTree from QTreeview

maintree.cpp file

void  MainTree::LaunchTree()
{
//Tree launching
 connect(this, SIGNAL(customContextMenuRequested(const QPoint& )),this,SLOT(showCustomContextMenu(const QPoint&)));
}

void MainTree::showCustomContextMenu(const QPoint &pos)  
{
  //Add actions

}

But i get the following error

QObject::connect: No such slot QTreeView::showCustomContextMenu(const QPoint&)

I could not understand why, am i missing something ??

Definition of the class MainTree

class MainTree : public QTreeView
{

public:
    MainTree();
    MainTree(QWidget *parent = 0);

public slots:

private slots:
    void showCustomContextMenu(const QPoint& pos);

private:
     void launchTree();

 };

回答1:


You are missing the Q_OBJECT macro out, so try this:

class MainTree : public QTreeView
{
Q_OBJECT
// ^^^^^
public:
    MainTree();
    MainTree(QWidget *parent = 0);

public slots:

private slots:
    void showCustomContextMenu(const QPoint& pos);

private:
     void launchTree();

 };

Do not forget to re-run qmake after this to regenerate the moc files properly. Make sure you have the moc include at the end of your source code, or you handle the moc generation without that.

Also, note that if you used Qt 5.2 or later with C++11 support, you would get a static assertion about the missing Q_OBJECT macro, so you would not get runtime issues anymore. I suggest to follow that if you can.




回答2:


When referring to slot and signals you have to omnit all decoration: const & and so on (only star can remain).

connect(this, SIGNAL(customContextMenuRequested(QPoint)), 
        this, SLOT(showCustomContextMenu(QPoint)))

Also you forgot about Q_OBJECT macro.



来源:https://stackoverflow.com/questions/22221798/connect-no-such-slot-qtreeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!