【问题标题】:Draw an item in a static location relative to the QGraphicsView在相对于 QGraphicsView 的静态位置绘制项目
【发布时间】:2013-08-26 22:19:55
【问题描述】:

我想绘制一个始终显示在我的QGraphicsView 右上角的通知。但是,QGraphicsItems 位置是在场景坐标中指定的,因此如果用户平移/缩放以查看场景的不同部分,此通知将移出屏幕。

我发现我可以通过在当前视图更改时移动和缩放通知来模拟这种行为。但这似乎非常无效,而且一点也不优雅。

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

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

【问题讨论】:

标签: c++ qt qgraphicsview qgraphicsitem qgraphicsscene


【解决方案1】:

让通知成为原始场景的一部分的天真解决方案是不好的 - 它破坏了模型视图分离。您可以有多个视图,所有视图都显示一个场景,但通常只有其中一个可以根据需要显示通知。

另一种简单的方法是在视图顶部覆盖 QWidget 通知。问题在于,在某些架构上,将常规 QWidgets 覆盖在加速 QGLWidgets 之上会使前者消失。请注意,QGraphicsView 的视口可能是 QGLWidget!

因此,唯一可移植的解决方案是在 QGraphicsSceneView 的 viewport() 中明确地在其他所有内容之上进行绘画。

下面是一个完整的例子。

// 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"

【讨论】:

  • 注意:使用drawForeground 会更直接。 *待办事项
  • 我尝试运行这个示例,但是当我开始左右滚动时,文本项被弄脏了。知道为什么吗?我在 Ubuntu 16.04 上运行 Qt 5.5.1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多