在相对于QGraphicsView的静态位置绘制项目 [英] Draw an item in a static location relative to the QGraphicsView

查看:395
本文介绍了在相对于QGraphicsView的静态位置绘制项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 QGraphicsView 的右上角画一个通知。然而,在场景坐标中指定 QGraphicsItems 位置,因此如果用户平移/缩放以查看场景的不同部分,则该通知将移离屏幕。

我发现我可以通过在当前视图更改时移动和缩放通知来模拟此行为。但这似乎是无效的,并不是所有优雅。



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



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

解决方案

将通知作为原始场景的一部分的天真解决方案是坏的 - - 视图分离。您可以有多个视图,都显示一个场景,但通常只有其中一个可以根据需要显示通知。



另一个简单的方法是覆盖在视图顶部的QWidget通知。问题是在一些架构上,在加速的QGLWidgets之上覆盖常规QWidget会使前者消失。注意,一个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)
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(); }
};

类Window:public QWidget
{
QGraphicsScene scene,notification;
OverlaidGraphicsView * view;
QGraphicsSimpleTextItem * item;
int timerId;
int time;
public:
Window():
view(new OverlaidGraphicsView(& scene,this)),
timerId
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.show();
a.exec();
}

#includemain.moc


I would like to draw a notification that always appears in the upper right corner of my QGraphicsView. However, QGraphicsItems positions are specified in scene coordinates, so if the user panned/zoomed to view a different part of the scene, this notification would move off screen.

I have figured out that I could simulate this behavior by moving and scaling the notification any time the current view changes. But this seems terribly ineffective and not at all elegant.

It seems that QGraphicsView should support this kind of behavior. The docs mention a flag ItemIsPanel that sounds hopeful, but mentions nothing about static placement in the view. ItemIgnoresTransformations also will help with scaling/zooming, but not panning.

Is there any built-in Qt functionality that supports this behavior?

解决方案

The naive solution of having the notification be a part of the original scene is bad - it breaks the model-view separation. You can have multiple views, all showing one scene, but generally on only one of them can the notification appear as desired.

Another simple way to do it would be to overlay a QWidget notification on top of your view. The problem is that on some architectures, overlaying regular QWidgets on top of accelerated QGLWidgets will make the former disappear. Do note that a QGraphicsView's viewport may be a QGLWidget!

Thus, the only portable solution is to explicitly do the painting on top of everything else in QGraphicsSceneView's viewport().

Below is a complete example.

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

这篇关于在相对于QGraphicsView的静态位置绘制项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆