English 中文(简体)
如何在不紧迫的情况下选择多个项目 QGraphicsScene中的Ctrl钥匙?
原标题:How to select multiple items without pressing Ctrl key within QGraphicsScene?

在Qt s QGraphicsScene中,如果I wanna一项目仅点击,另一个可点击的项目将使选定的项目无法当选。 如果我要选择多个项目,我就使用Ctrl-key。 但是,这在某些情况下可能并不方便,因此,如何在<代码>QGraphicsScene内选择不使用硬基的多个项目?

最佳回答

你想改变<代码>QGraphicsScene的缺省行为,因此,你必须创建自己的场面,继承<代码>QGraphicsScene。

在您的班子中,你至少必须重新实施<条码>压力调控,并处理项目选择。

这里是你可以做的(传承的场面名称为GraphicsSelectionScene):

void GraphicsSelectionScene::mousePressEvent(QGraphicsSceneMouseEvent* pMouseEvent) {
    QGraphicsItem* pItemUnderMouse = itemAt(pMouseEvent->scenePos().x(), pMouseEvent->scenePos().y());

    if (!pItemUnderMouse)
        return;
    if (pItemUnderMouse->isEnabled() &&
        pItemUnderMouse->flags() & QGraphicsItem::ItemIsSelectable)
        pItemUnderMouse->setSelected(!pItemUnderMouse->isSelected());
}

以这种方式实施,如果某个项目尚未选定,则点击,或以其他方式加以忽略。

但是,执行<条码>压力/代码”显然是不够的:你必须处理<条码>。 如果你不想要发生违约行为,那么“双重风险”栏目。

页: 1 View。 这方面的一个例子是,一种看法创造了自己的景象(MainFrm)。 课程:

#include "mainfrm.h"
#include "ui_mainfrm.h"
#include "graphicsselectionscene.h"
#include <QGraphicsItem>

MainFrm::MainFrm(QWidget *parent) : QGraphicsView(parent), ui(new Ui::MainFrm) {
    ui->setupUi(this);

    // Create a scene with our own selection behavior
    QGraphicsScene* pScene = new GraphicsSelectionScene(this);
    this->setScene(pScene);

    // Create a few items for testing
    QGraphicsItem* pRect1 = pScene->addRect(10,10,50,50, QColor(Qt::red), QBrush(Qt::blue));
    QGraphicsItem* pRect2 = pScene->addRect(100,-10,50,50);
    QGraphicsItem* pRect3 = pScene->addRect(-200,-30,50,50);

    // Make sure the items are selectable
    pRect1->setFlag(QGraphicsItem::ItemIsSelectable, true);
    pRect2->setFlag(QGraphicsItem::ItemIsSelectable, true);
    pRect3->setFlag(QGraphicsItem::ItemIsSelectable, true);
}
问题回答

也许这只是一个黑板,而是为我工作。 举例来说,你可以选择使用转移钥匙的多个项目

void MySceneView::mousePressEvent(QMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier ) //any other condition
        event->setModifiers(Qt::ControlModifier);

    QGraphicsView::mousePressEvent(event);
}


void MySceneView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier ) //any other condition
        event->setModifiers(Qt::ControlModifier);

    QGraphicsView::mouseReleaseEvent(event);
}




相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签