在QGraphicsView中相对于静态位置绘制一个项目

12

我希望在我的QGraphicsView的右上角绘制一个通知。然而,QGraphicsItems的位置是用场景坐标指定的,因此如果用户平移/缩放以查看场景的不同部分,则该通知将移动到屏幕外。

我已经想出了一种方法,即每当当前视图改变时移动和缩放通知来模拟这种行为。但这似乎非常低效且不优雅。

看起来QGraphicsView应该支持此类行为。文档中提到了一个具有希望性的标志ItemIsPanel,但没有提及在视图中的静态放置。 ItemIgnoresTransformations也将有助于缩放/缩放,但不能平移。

是否有任何内置的Qt功能支持此行为?


你尝试过在视口上工作吗?就像这样:graphicsView.mapToScene(graphicsView.view.viewport()->rect().topRight()) - dare
可能是Forcing QGraphicsItem To Stay Put的重复问题。 - alexisdm
请参考以下链接:https://dev59.com/4HPYa4cB1Zd3GeqPoOH3 - Trilarion
1个回答

13

将通知作为原始场景的一部分是一个天真的解决方案,这会破坏模型-视图分离。您可以有多个视图,所有视图都显示一个场景,但通常只有一个视图可以显示所需的通知。

另一种简单的方法是在视图之上覆盖一个QWidget通知。问题是,在某些体系结构上,在加速的QGLWidgets之上叠加常规的QWidgets会使前者消失。请注意,QGraphicsView的viewport可能是一个QGLWidget!

因此,唯一可移植的解决方案是显式地在QGraphicsSceneView的viewport()之上绘制其他所有内容。

下面是一个完整的示例。

enter image description here

// main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>

qreal rnd() { return qrand() / (float)RAND_MAX; }

class OverlaidGraphicsView : public QGraphicsView
{
    Q_OBJECT
    QGraphicsScene * m_overlayScene;
public:
    explicit OverlaidGraphicsView(QWidget* parent = 0) :
        QGraphicsView(parent), m_overlayScene(NULL) {}
    explicit OverlaidGraphicsView(QGraphicsScene * scene = 0, QWidget * parent = 0) :
        QGraphicsView(scene, parent), m_overlayScene(NULL) {}
    void setOverlayScene(QGraphicsScene * scene) {
        if (scene == m_overlayScene) return;
        m_overlayScene = scene;
        connect(scene, SIGNAL(changed(QList<QRectF>)), SLOT(overlayChanged()));
        update();
    }
    QGraphicsScene * overlayScene() const { return m_overlayScene; }
    void paintEvent(QPaintEvent *ev) {
        QGraphicsView::paintEvent(ev);
        if (m_overlayScene) paintOverlay();
    }
    virtual void paintOverlay() {
        QPainter p(viewport());
        p.setRenderHints(renderHints());
        m_overlayScene->render(&p, viewport()->rect());
    }
    Q_SLOT void overlayChanged() { update(); }
};

class Window : public QWidget
{
    QGraphicsScene scene, notification;
    OverlaidGraphicsView * view;
    QGraphicsSimpleTextItem * item;
    int timerId;
    int time;
public:
    Window() :
        view(new OverlaidGraphicsView(&scene, this)),
        timerId(-1), time(0)
    {
        for (int i = 0; i < 20; ++ i) {
            qreal w = rnd()*0.3, h = rnd()*0.3;
            scene.addEllipse(rnd()*(1-w), rnd()*(1-h), w, h, QPen(Qt::red), QBrush(Qt::lightGray));
        }
        view->fitInView(0, 0, 1, 1);
        view->setResizeAnchor(QGraphicsView::AnchorViewCenter);
        view->setRenderHint(QPainter::Antialiasing);
        view->setOverlayScene(&notification);
        item = new QGraphicsSimpleTextItem();
        item->setPen(QPen(Qt::blue));
        item->setBrush(Qt::NoBrush);
        item->setPos(95, 0);
        notification.addItem(item);
        notification.addRect(0, 0, 100, 0, Qt::NoPen, Qt::NoBrush); // strut
        timerId = startTimer(1000);
        QTimerEvent ev(timerId);
        timerEvent(&ev);
    }
    void resizeEvent(QResizeEvent * ev) {
        view->resize(size());
        view->fitInView(0, 0, 1, 1, Qt::KeepAspectRatio);
        QWidget::resizeEvent(ev);
    }
    void timerEvent(QTimerEvent * ev) {
        if (ev->timerId() != timerId) return;
        item->setText(QString::number(time++));
    }
};

int main(int argc, char ** argv)
{
    QApplication a(argc, argv);

    Window window;
    window.show();
    a.exec();
}

#include "main.moc"

1
请注意:使用drawForeground会更直接。待办事项 - Kuba hasn't forgotten Monica
我尝试运行这个例子,但是当我开始左右滚动时,文本项目变得模糊了。有什么想法吗?我正在Ubuntu 16.04上运行Qt 5.5.1。 - Guilty Spark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接