Connect: No such Slot QTreeView

2019-08-14 06:29发布

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();

 };

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-14 06:33

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.

查看更多
放我归山
3楼-- · 2019-08-14 06:34

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.

查看更多
登录 后发表回答