简体   繁体   中英

Connect: No such Slot QTreeView

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

 };

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM