我在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)); // 錯誤