QGridLayout 不显示添加的小部件
QGridLayout not displaying added widgets
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 class 构造函数的摘录。我不明白的是为什么 paddingLayout 不显示添加到它的任何小部件,在本例中为 "Cells"。有什么想法吗?
编辑:
class"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);
}
如果需要更多代码,请说!
首先,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 保存指向已删除对象的指针。
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 class 构造函数的摘录。我不明白的是为什么 paddingLayout 不显示添加到它的任何小部件,在本例中为 "Cells"。有什么想法吗?
编辑:
class"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);
}
如果需要更多代码,请说!
首先,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 保存指向已删除对象的指针。