如何使用复选框更改QGraphicsView背景 [英] How can I change QGraphicsView background using check box

查看:107
本文介绍了如何使用复选框更改QGraphicsView背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,它更改了 QGraphicsView 背景.现在,当我选中true checkBox 时,我需要更改背景.当我将其设置为 checkBox 检查 true 我需要像这样的代码设置背景.当我设置 checkBox 来检查 false 时.我需要将 QGraphicsView 设置为常规默认方式.我该怎么办.

这是我的代码:

mainwindow.cpp

  #include"mainwindow.h"#include< QGraphicsTextItem>MainWindow :: MainWindow(QWidget * parent):QMainWindow(父级){场景=新场景(this);scene-> setSceneRect(10,10,260,200);view =新的QGraphicsView(scene);setCentralWidget(view);} 

mainwindow.h

  #ifndef MAINWINDOW_H#define MAINWINDOW_H#include< QMainWindow>#include< QGraphicsView>#include"scene.h"#include"customrectitem.h"类MainWindow:公共QMainWindow{上市:显式MainWindow(QWidget * parent = 0);私人的:QGraphicsView *视图;QGraphicsScene *场景;};#endif//MAINWINDOW_H 

Scene.h

  #ifndef SCENE_H#define SCENE_H#include< QGraphicsScene>#include< QPainter>#include< QApplication>类场景:公共QGraphicsScene{Q_OBJECT上市:显式场景(QObject * parent = 0);int getGridSize()const {返回this-> gridSize;}受保护的:void drawBackground(QPainter * painter,const QRectF& rect);私人的:int gridSize;};#endif//SCENE_H 

Scene.cpp

  #include"scene.h"Scene :: Scene(QObject * parent):QGraphicsScene(parent),gridSize(20){Q_ASSERT(gridSize> 0);}无效Scene :: drawBackground(QPainter * painter,const QRectF& rect){QColor c(10,140,​​255,155);painter-> setPen(c);qreal左= int(rect.left())-(int(rect.left())%gridSize);qreal top = int(rect.top())-(int(rect.top())%gridSize);QVarLengthArray< QLineF,100>线;for(qreal x =左; x< rect.right(); x + = gridSize)lines.append(QLineF(x,rect.top(),x,rect.bottom()));for(qreal y = top; y  

解决方案

尽管

更改大小后:

纠正这两个错误的解决方案是覆盖

更改大小后:

完整的示例可以在下面的链接

In this code,which change the QGraphicsView background.Now I need to change the background when I check true checkBox.When I set to checkBox to check true I need to set the background like this code.When I set checkBox to check false. I need to set QGraphicsView for normal default way. How can I do this.

here is my code:

mainwindow.cpp

#include "mainwindow.h"
#include <QGraphicsTextItem>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    scene = new Scene(this);
    scene->setSceneRect(10,10,260,200);
    view = new QGraphicsView(scene);
    setCentralWidget(view);
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsView>
#include "scene.h"
#include "customrectitem.h"

class MainWindow : public QMainWindow
{
public:
    explicit MainWindow(QWidget *parent = 0);
private:
    QGraphicsView* view;
    QGraphicsScene* scene;
};

#endif // MAINWINDOW_H

Scene.h

#ifndef SCENE_H
#define SCENE_H

#include <QGraphicsScene>
#include <QPainter>
#include <QApplication>

class Scene : public QGraphicsScene
{
    Q_OBJECT
public:
    explicit Scene(QObject *parent = 0);
    int getGridSize() const {return this->gridSize;}

protected:
    void drawBackground (QPainter* painter, const QRectF &rect);
private:
    int gridSize;

};

#endif // SCENE_H

Scene.cpp

#include "scene.h"

Scene::Scene(QObject *parent) : QGraphicsScene(parent), gridSize(20)
{
    Q_ASSERT(gridSize > 0);
}

void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
        QColor c (10,140,255,155);
        painter->setPen(c);
        qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
        qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
        QVarLengthArray<QLineF,100> lines;
        for (qreal x = left; x < rect.right(); x += gridSize)
            lines.append(QLineF(x,rect.top(),x,rect.bottom()));

        for (qreal y = top; y < rect.bottom(); y += gridSize)
            lines.append(QLineF(rect.left(),y,rect.right(),y));
        painter->drawLines(lines.data(),lines.size());
}

解决方案

Although @Scheff's solution is good, it has the following problems:

  • the first solution makes it only cover a small space, if you change the size of QGraphicsView it will not cover all the space.

  • The second solution has the problem that you are using the viewport() coordinate, and not the scene, if you add an item and then enlarge the QGraphicsView without moving the item, then you see a scroll.

Initial:

After changing the size:

A solution that corrects both errors is to overwrite the drawBackground method of QGraphicsScene:

#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H

#include <QGraphicsScene>
#include <QPainter>


class GraphicsScene: public QGraphicsScene{

    int mGridSize;
    QColor mGridColor;
    bool mGridVisible;

    Q_OBJECT
public:
    GraphicsScene(QObject *parent = Q_NULLPTR):QGraphicsScene(parent),
        mGridSize(20), mGridColor(0x0a8affu), mGridVisible(true)
    {

    }
    bool gridVisible() const {
        return mGridVisible;
    }
    void setGridVisible(bool gridVisible){
        if(mGridVisible != gridVisible){
            mGridVisible = gridVisible;
            update();
        }
    }
protected:
    void drawBackground(QPainter *painter, const QRectF &rect){
        if(mGridVisible) {
            QRect r = rect.toRect();
            int xmin  =r.left() - r.left()%mGridSize - mGridSize;
            int ymin = r.top() - r.top()%mGridSize - mGridSize;
            int xmax = r.right() - r.right()%mGridSize + mGridSize;
            int ymax = r.bottom() - r.bottom()%mGridSize + mGridSize;
            for(int x=xmin ; x <= xmax; x += mGridSize ){
                painter->drawLine(x, r.top(), x, r.bottom());
            }

            for(int y=ymin ; y <= ymax; y+= mGridSize ){
                painter->drawLine(r.left(), y, r.right(), y);
            }
        }
    }
};

#endif // GRAPHICSSCENE_H

The advantage of this solution is that it does not load any new object, besides that the graph is in the coordinates of the scene, so there is no displacement of the second solution.

Initial:

After changing the size:

The complete example can be found at the following link

这篇关于如何使用复选框更改QGraphicsView背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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