gpt4 book ai didi

c++ - 如何刷新QGraphicsView以显示QGraphicsScene的背景更改

转载 作者:行者123 更新时间:2023-12-02 10:08:43 27 4
gpt4 key购买 nike

我有一个自定义的QGraphicsViewQGraphicsScene。在QGraphicsScene内部,我已经覆盖了void drawBackground(QPainter *painter, const QRectF &rect),并且基于一个 bool(boolean) 标志,我想打开和关闭一个网格。我尝试在函数中调用clear()paintereraseRect(sceneRect()),但没有用。因此,在阅读了一些内容之后,我认为它不起作用,因为更改场景后,您需要刷新 View 。这就是为什么我发出一个称为signalUpdateViewport()的信号的原因

void Scene::drawBackground(QPainter *painter, const QRectF &rect) {
if(this->gridEnabled) {
// Draw grid
}
else {
// Erase using the painter
painter->eraseRect(sceneRect());
// or by calling
//clear();
}

// Trigger refresh of view
emit signalUpdateViewport();
QGraphicsScene::drawBackground(painter, rect);
}

然后由我的观点捕获:
void View::slotUpdateViewport() {
this->viewport()->update();
}

不用说这是行不通的。使用 不起作用我的意思是仅在更改窗口小部件(例如触发调整大小事件)时才使结果(从场景内部或 View 内部刷新)可见。

如何正确刷新场景的 View 以显示在场景背景中所做的更改?

编码:

scene.h
#ifndef SCENE_HPP
#define SCENE_HPP

#include <QGraphicsScene>

class View;

class Scene : public QGraphicsScene
{
Q_OBJECT
Q_ENUMS(Mode)
Q_ENUMS(ItemType)
public:
enum Mode { Default, Insert, Select };
enum ItemType { None, ConnectionCurve, ConnectionLine, Node };

Scene(QObject* parent = Q_NULLPTR);
~Scene();

void setMode(Mode mode, ItemType itemType);
signals:
void signalCursorCoords(int x, int y);

void signalSceneModeChanged(Scene::Mode sceneMode);
void signalSceneItemTypeChanged(Scene::ItemType sceneItemType);
void signalGridDisplayChanged(bool gridEnabled);

void signalUpdateViewport();
public slots:
void slotSetSceneMode(Scene::Mode sceneMode);
void slotSetSceneItemType(Scene::ItemType sceneItemType);

void slotSetGridStep(int gridStep);
void slotToggleGrid(bool flag);
private:
Mode sceneMode;
ItemType sceneItemType;

bool gridEnabled;
int gridStep;

void makeItemsControllable(bool areControllable);
double round(double val);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void keyPressEvent(QKeyEvent *event);

void drawBackground(QPainter *painter, const QRectF &rect);
};

Q_DECLARE_METATYPE(Scene::Mode)
Q_DECLARE_METATYPE(Scene::ItemType)

#endif // SCENE_HPP

scene.cpp
#include <QGraphicsItem>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QRectF>
#include <QKeyEvent>

#include <QtDebug>

#include "scene.h"

Scene::Scene(QObject* parent)
: QGraphicsScene (parent),
sceneMode(Default),
sceneItemType(None),
gridEnabled(true),
gridStep(30)
{

}

Scene::~Scene()
{
}

void Scene::setMode(Mode mode, ItemType itemType)
{
this->sceneMode = mode;
this->sceneItemType = itemType;

QGraphicsView::DragMode vMode = QGraphicsView::NoDrag;

switch(mode) {
case Insert:
{
makeItemsControllable(false);
vMode = QGraphicsView::NoDrag;
break;
}
case Select:
{
makeItemsControllable(true);
vMode = QGraphicsView::RubberBandDrag;
break;
}
case Default:
{
makeItemsControllable(false);
vMode = QGraphicsView::NoDrag;
break;
}
}

QGraphicsView* mView = views().at(0);
if(mView) {
mView->setDragMode(vMode);
}
}

void Scene::slotSetSceneMode(Scene::Mode sceneMode)
{
this->sceneMode = sceneMode;
qDebug() << "SM" << (int)this->sceneMode;
emit signalSceneModeChanged(this->sceneMode);
}

void Scene::slotSetSceneItemType(Scene::ItemType sceneItemType)
{
this->sceneItemType = sceneItemType;
qDebug() << "SIT:" << (int)this->sceneItemType;
emit signalSceneItemTypeChanged(this->sceneItemType);
}

void Scene::slotSetGridStep(int gridStep)
{
this->gridStep = gridStep;
}

void Scene::slotToggleGrid(bool flag)
{
this->gridEnabled = flag;
invalidate(sceneRect(), BackgroundLayer);
qDebug() << "Grid " << (this->gridEnabled ? "enabled" : "disabled");
update(sceneRect());
emit signalGridDisplayChanged(this->gridEnabled);
}

void Scene::makeItemsControllable(bool areControllable)
{
foreach(QGraphicsItem* item, items()) {
if(/*item->type() == QCVN_Node_Top::Type
||*/ item->type() == QGraphicsLineItem::Type
|| item->type() == QGraphicsPathItem::Type) {
item->setFlag(QGraphicsItem::ItemIsMovable, areControllable);
item->setFlag(QGraphicsItem::ItemIsSelectable, areControllable);
}
}
}

double Scene::round(double val)
{
int tmp = int(val) + this->gridStep/2;
tmp -= tmp % this->gridStep;
return double(tmp);
}

void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mousePressEvent(event);
}

void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
emit signalCursorCoords(int(event->scenePos().x()), int(event->scenePos().y()));
QGraphicsScene::mouseMoveEvent(event);
}

void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseReleaseEvent(event);
}

void Scene::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_M) {
slotSetSceneMode(static_cast<Mode>((int(this->sceneMode) + 1) % 3));
}
else if(event->key() == Qt::Key_G) {
slotToggleGrid(!this->gridEnabled);
}

QGraphicsScene::keyPressEvent(event);
}

void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
// FIXME Clearing and drawing the grid happens only when scene size or something else changes
if(this->gridEnabled) {
painter->setPen(QPen(QColor(200, 200, 255, 125)));
// draw horizontal grid
double start = round(rect.top());
if (start > rect.top()) {
start -= this->gridStep;
}
for (double y = start - this->gridStep; y < rect.bottom(); ) {
y += this->gridStep;
painter->drawLine(int(rect.left()), int(y), int(rect.right()), int(y));
}
// now draw vertical grid
start = round(rect.left());
if (start > rect.left()) {
start -= this->gridStep;
}
for (double x = start - this->gridStep; x < rect.right(); x += this->gridStep) {
painter->drawLine(int(x), int(rect.top()), int(x), int(rect.bottom()));
}
}
else {
// Erase whole scene's background
painter->eraseRect(sceneRect());
}

slotToggleGrid(this->gridEnabled);

QGraphicsScene::drawBackground(painter, rect);
}
View当前不包含任何内容-全部为默认设置。我的 Scene中的 ViewQMainWindow实例的设置如下:
void App::initViewer()
{
this->scene = new Scene(this);
this->view = new View(this->scene, this);
this->viewport = new QGLWidget(QGLFormat(QGL::SampleBuffers), this->view);
this->view->setRenderHints(QPainter::Antialiasing);
this->view->setViewport(this->viewport);
this->view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
this->view->setCacheMode(QGraphicsView::CacheBackground);

setCentralWidget(this->view);
}

编辑:我尝试在 sceneRect()BackgroundLayer或任何尝试触发刷新的调用之前调用invalidate( update()clear())。

我也从场景中尝试过 QGraphicsScene::update(),但是它没有用将两个参数都没有传递给函数调用,然后传递 sceneRect()并没有导致与上面所描述的有所不同。

最佳答案

更改网格设置时,无论是打开还是关闭(或颜色等),都需要调用QGraphicsScene::update。这也是一个插槽,因此您可以根据需要连接信号。您也可以指定暴露区域。如果不这样做,那么它将使用所有内容的默认值。对于网格,这可能就是您想要的。

您无需清除网格。更新调用可确保清除已更新的区域,然后在需要网格的情况下对其进行绘制,或者在不应该存在网格的情况下不对其进行绘制。

关于c++ - 如何刷新QGraphicsView以显示QGraphicsScene的背景更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40665960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com