在“图形视图”中设置场景,在“图形场景”(QGraphicscene的子类)中添加了“删除项目”插槽。在场景类中,通过删除键可以删除项目,但是当我从主窗口调用时,它不会删除项目。我在Delete item插槽中接到电话,但是selectedItems =0。什么可能引起问题?
在“图形”场景课程中
void GraphicScene::DeleteItems()//Delete Item slot in scene class
{
qDebug()<<"delete items"<< selectedItems().count();
foreach(QGraphicsItem* item, selectedItems())
{
removeItem(item);
delete item;
}
}
void GraphicScene::keyReleaseEvent(QKeyEvent * keyEvent)// Delete key works fine
{
if (selectedItems().isEmpty())
return;
if(keyEvent->key() == Qt::Key_Delete)
{
DeleteItems();
}
}
在MainWindow类中
MainWindow::MainWindow(QWidget *parent)
{
addToolBar(Qt::TopToolBarArea, mpEditToolbar = new
QToolBar());
DeleteAction = new QAction(QIcon(":/images/delete.png"),tr("Object
&Delete"), this);
DeleteAction->setStatusTip(tr("Delete item"));
connect(DeleteAction,SIGNAL(triggered()),mpGraphView ,
SIGNAL(DeleteObject())); // grpah view connecting to delete slot
mpEditToolbar->addAction(DeleteAction);
}
当我从删除键执行正常时,它不能与工具箱删除操作一起使用。有什么问题吗?
答案 0 :(得分:2)
在Main Window类中具有GraphicsView和GraphicScene类(子类)的私有成员 以便轻松调用广告位。
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
GraphFrame *mpGraphFrame;
GraphicScene *mpScene;
}
MainWindow.cpp
Connect应该位于创建GraphicScene对象的位置
MainWindow::MainWindow(QWidget *parent)
{
addToolBar(Qt::TopToolBarArea, mpEditToolbar = new
QToolBar());
DeleteAction = new QAction(QIcon(":/images/delete.png"),tr("Object
&Delete"), this);
DeleteAction->setStatusTip(tr("Delete item"));
connect(DeleteAction,SIGNAL(triggered()),mpGraphScene ,
DeleteItems();
mpEditToolbar->addAction(DeleteAction);
}