QGridLayout没有显示添加的小部件

时间:2017-12-16 23:28:23

标签: qt5 qpushbutton qgridlayout

    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没有显示添加到它的任何小部件,在这种情况下,&#34; Cells&#34;。有什么想法吗?

编辑:

课程的定义&#34; Cell&#34;和构造函数的代码:

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);
}

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

1 个答案:

答案 0 :(得分: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 保存指向已删除对象的指针。