简体   繁体   English

QT中指针向量的访问向量

[英]Access vector of vectors of pointers in QT

I have a problem with QT vectors. 我对QT向量有疑问。 I have a vector of vectors of pointers: 我有一个指针向量的向量:

QVector <QVector <QGraphicsRectItem*> > Board;

Functions creating QGraphicsItems return pointers, so i need this structure to be like that. 创建QGraphicsItems的函数返回指针,因此我需要这种结构。 The problem is i can't access items. 问题是我无法访问项目。 When i write 当我写

Board[Row][Column]

it only gets the column right. 它只会使列正确。

I also tried making a "proper" 2d vector of pointers like 我也尝试制作像这样的指针的“适当的” 2d向量

QVector <QVector <QGraphicsRectItem*>*> Board;

But when i do it like this the program crashes. 但是当我这样做时,程序崩溃了。

How can i deal with this problem? 我该如何处理这个问题?

Full code just in case: 完整的代码,以防万一:

chessboard.h: Chessboard.h:

#ifndef CHESSBOARD_H
#define CHESSBOARD_H
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
namespace Ui {
class ChessBoard;
}
class ChessBoard : public QWidget
{
    Q_OBJECT
public:
    explicit ChessBoard(QWidget *parent = 0);
    ~ChessBoard();
    QVector <QVector <QGraphicsRectItem*> > Board;
    void fillChessBoard();
    void Highlight (int row, int column);
private:
    Ui::ChessBoard *ui;
    QGraphicsScene *scene;
};
#endif // CHESSBOARD_H

chessboard.cpp: Chessboard.cpp:

#include "chessboard.h"
#include "ui_chessboard.h"
#define BOARD_SIZE 8

ChessBoard::ChessBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChessBoard)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    fillChessBoard();
    Highlight(3,5);



}
ChessBoard::~ChessBoard()
{
    delete ui;
}
void ChessBoard::fillChessBoard()
{
    QBrush blackBrush (Qt::black);
    QBrush whiteBrush (Qt::white);
    QPen outlinePen (Qt::black);
    outlinePen.setWidth(1);
    QGraphicsRectItem* tempRect;
    QVector <QGraphicsRectItem*> tempRow;
    bool colorSwitch=0;
    for (int i=0;i<BOARD_SIZE;i++)//j-x;i-y
    {
        for (int j=0; j<BOARD_SIZE;j++)
        {
            if(colorSwitch==0)
            {
                tempRect=scene->addRect(j*50,i*50,50,50,outlinePen,whiteBrush);
                tempRow.push_back(tempRect);
                colorSwitch=!colorSwitch;
            }
            else
            {
                tempRect=scene->addRect(j*50,i*50,50,50,outlinePen,blackBrush);
                tempRow.push_back(tempRect);
                colorSwitch=!colorSwitch;
            }
        }
        colorSwitch=!colorSwitch;
        Board.push_back(tempRow);
    }
}

void ChessBoard::Highlight(int row,int column)
{
    QBrush redBrush (Qt::red);
    Board[row][column]->setBrush(redBrush);
}

You forgot to clear tempRow : 您忘记清除tempRow

Board.push_back(tempRow);
tempRow.clear(); // add this

Without clearing, you continue to add items to the end of row which is not what you want. 如果不清除,您将继续在行末添加不必要的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM