QT运行出错:QObject::connect: Parentheses expected以及QObject::connect: No such slot ***

本文详细介绍了在QGraphicsScene的子类中为item添加弹出菜单的方法,并解决了在连接菜单Action到槽函数时遇到的Nosuchslot错误。文章强调了使用信号/槽时必须包含Q_OBJECT宏,以及正确使用SLOT宏指明槽函数的重要性。

我在QGraphicsScene子类中添加了item的弹出菜单,并连接Action到槽函数,结果槽函数不起作用,输出:QObject::connect: No such slot ***

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
//选中item后弹出右键菜单
if (event->button() == Qt::RightButton)
{
    m_pItemSelected = nullptr;
    foreach (QGraphicsItem *item, items(event->scenePos()))
    {
        
if (item->type() == QGraphicsPixmapItem::Type)
        {
            m_pItemSelected = item;
            QMenu menu;
            QAction *removeAction = menu.addAction(
"Remove");
            QAction *toTopLayerAction = menu.addAction(
"To Top Layer");
            QAction *toBottomLayerAction = menu.addAction(
"To Bottom Layer");
            QAction *toUpperLayerAction = menu.addAction(
"To Upper Layer");
            QAction *toLowerLayerAction = menu.addAction(
"To Lower Layer");
            connect(removeAction, SIGNAL(triggered()), 
this, SLOT(slotRemoveItem()));
            connect(toTopLayerAction, SIGNAL(triggered()), 
this, SLOT(slotLayerTop()));
            connect(toBottomLayerAction, SIGNAL(triggered()), 
this, SLOT(slotLayerBottom()));
            connect(toUpperLayerAction, SIGNAL(triggered()), 
this, SLOT(slotLayerUpper()));
            connect(toLowerLayerAction, SIGNAL(triggered()), 
this, SLOT(slotLayerLower()));
            menu.exec(event->screenPos());
            
break;
        }
    }
}

 

在类中使用信号/槽时一定要加Q_OBJECT宏,signal和slots的参数要一样

槽函数加(): connect(toTopLayerAction, SIGNAL(triggered()), this, SLOT(slotLayerTop()));   // 正确

切记忘了():connect(toTopLayerAction, SIGNAL(triggered()), this, SLOT(slotLayerTop));      // 错误

转载于:https://www.cnblogs.com/MakeView660/p/10369462.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值