QGridLayout没有显示添加的小部件

问题描述 投票:0回答:1
    QWidget* centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);
    QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);
    topLineLayout = new QHBoxLayout();
    minesCounterLabel = new QLabel();
    emoticonButton = new QPushButton();
    timerLabel = new QLabel("0");
    topLineLayout -> addWidget(minesCounterLabel);
    topLineLayout -> addWidget(emoticonButton);
    topLineLayout -> addWidget(timerLabel);
    mainLayout -> addLayout(topLineLayout);
    paddingLayout = new QGridLayout();
    paddingLayout -> setSpacing(0);
    std::vector <Cell> cellsVector;
    for(int i = 0; i < paddingHeight; i++)
    {
        for(int j = 0; j < paddingWidth; j++)
        {
            Cell* cell = new Cell(&cellsVector, j, i, false);
            cellsVector.push_back(*cell);
        }
    }
    for(int i = 0; i < cellsVector.size(); i++)
    {
        paddingLayout -> addWidget(&cellsVector[i], cellsVector[i].getX(), cellsVector[i].getY());
    }
    mainLayout -> addLayout(paddingLayout);

这是mainwindow.h文件,这是MainWindow类构造函数的摘录。我不明白的是为什么paddingLayout没有显示添加到它的任何小部件,在本例中是“Cells”。有任何想法吗?

编辑:

类“Cell”的定义及其构造函数的代码:

class Cell : public QPushButton
{
    Q_OBJECT

    public:

        Cell(std::vector <Cell> *, int, int, bool, QWidget* parent = 0);
        Cell(const Cell&);
        bool hasMine();
        int getX();
        int getY();
        std::vector <Cell> * getCellsVector();
        void setHasMine(bool);
        ~Cell();
        Cell operator=(const Cell& object)
        {
            this -> cellsVector = object.cellsVector;
            this -> mine = object.mine;
            this -> x = object.x;
            this -> y = object.y;
            setFixedSize(20, 20);
            connect(this, &QPushButton::clicked, this, &Cell::cellClicked);
        }

    private:

        std::vector <Cell> * cellsVector;
        int x;
        int y;
        bool mine;
        // Determine how many mines are around this cell.
        int countMines();
        void cellClicked();
};

Cell::Cell(std::vector <Cell> * cellsVector, int x, int y, bool mine = false, QWidget* parent) : QPushButton(parent)
{
    this -> cellsVector = cellsVector;
    this -> mine = mine;
    this -> x = x;
    this -> y = y;
    setFixedSize(20, 20);
    connect(this, &QPushButton::clicked, this, &Cell::cellClicked);
}

Cell::Cell(const Cell& object)
{
    this -> cellsVector = object.cellsVector;
    this -> mine = object.mine;
    this -> x = object.x;
    this -> y = object.y;
    setFixedSize(20, 20);
    connect(this, &QPushButton::clicked, this, &Cell::cellClicked);
}

如果需要更多代码,请说出来!

qt5 qpushbutton qgridlayout
1个回答
0
投票

首先,Cell的赋值运算符存在问题,应该是

Cell& operator=(const Cell& object)
{
     if (&object == this) return *this;
     this -> cellsVector = object.cellsVector;
     this -> mine = object.mine;
     this -> x = object.x;
     this -> y = object.y;
     setFixedSize(20, 20);
     connect(this, &QPushButton::clicked, this, &Cell::cellClicked);
     return *this; // you must return value
}

最大的问题是std::vector <Cell> cellsVector;,这个载体被创建,细胞被插入它,然后在这一行

paddingLayout -> addWidget(&cellsVector[i], cellsVector[i].getX(), cellsVector[i].getY());

你获取单元格的地址,并将其传递给QGridLayout,但是在函数结束时cellsVector会发生什么?该载体被破坏,所有细胞也被破坏。 QGridLayout保存指向已删除对象的指针。

© www.soinside.com 2019 - 2024. All rights reserved.