Qt使用单个矩形网格创建场景

时间:2015-03-04 03:15:49

标签: c++ qt qgraphicsview qgraphicsscene

我想创建一个22x22个矩形的网格。我将使用基于预定义色标的颜色填充每个矩形。

目前我的头文件如下所示:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtGui>
#include <QtCore>
#include <QGraphicsScene>

#define GRIDDIM 22

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;
    QGraphicsScene *scene;
    QGraphicsRectItem *rec[GRIDDIM*GRIDDIM];




protected:
    void paintEvent(QPaintEvent *e);
};

#endif // DIALOG_H

所以我在rec []数组中声明了22x22个单独的矩形对象。

我的.cpp文件如下所示:

#include "dialog.h"
#include "ui_dialog.h"
#include <QGraphicsView>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    //rec = new QGraphicsRectItem;
    ui->graphicsView->setScene(scene);


    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackPen(Qt::black);
    QPen bluePen(Qt::blue);
    blackPen.setWidth(0);
    bluePen.setWidth(0);

    int height = 15;
    int width = 15;
    int xpos = 0;
    int ypos = 0;


    for (int i=0;i<GRIDDIM;i++){
        for(int j=0;j<GRIDDIM;j++){
        scene->addRect(i*(width),j*(height),width,height,blackPen);
        }
    }

但这只是使用addrect分配通用rect对象。

是否可以将我的rec []数组实现到场景中,以便我可以说例如set rec [10] blue?

使用:

QGraphicsRectItem *rec = new QGraphicsRectItem(QRect(xpos,ypos,width,height));

回复错误:

allocation of incomplete type 'QGraphicsRectItem'
forward declaration of 'QGraphicsRectItem'

0 个答案:

没有答案