gpt4 book ai didi

qt - 不要在 Qt 中的 setVisble 后立即重新绘制

转载 作者:行者123 更新时间:2023-12-02 22:41:48 27 4
gpt4 key购买 nike

我正在使用 QPushButton在我的扫雷游戏中。

从简单模式更改为困难模式后,QPushButton 的数量应该从 9x9 更改为 30x16。

因此,我在 MainWindow 的构造函数中将具有最大数量(硬模式)的 QPushButton 添加到 GridLayout

btnArr = new QPushButton[HARD_WIDTH * HARD_HEIGHT]; // member element
int index = 0;
for (int i = 0; i < HARD_HEIGHT; ++i) {
for (int j = 0; j < HARD_WIDTH; ++j) {
ui->mainGrid->addWidget(&btnArr[index], i, j, 1, 1,
Qt::AlignCenter);
++index;
}
}

然后如果用户更改模式(例如:简单模式到困难模式),resetBtn(HARD_WIDTH, HARD_HEIGHT); 将被调用。

void MainWindow::resetBtn(const int width, const int height)
{
int index = 0;
for (int i = 0; i < HARD_HEIGHT; ++i) {
for (int j = 0; j < HARD_WIDTH; ++j) {
if (j < width && i < height) {
btnArr[index].setVisible(true);
} else {
btnArr[index].setVisible(false);
}
++index;
}
}
}

问题是,每次调用 setVisible 时,小部件似乎都会重新绘制。所以在困难模式的情况下,它会被调用 30x16 次,这导致了这样的奇怪效果: enter image description here

那么如何设置小部件在此循环期间不重绘呢?

提前致谢。

最佳答案

我认为您正在尝试解决错误的问题。您不应该像这样更新小部件。如果您一定要这样做,那么在更改之前隐藏布局的父窗口小部件并在之后再次显示它应该可行。

更好的方法是使用 QStackedWidget并首先准备好所有电路板。切换到不同的面板只是切换事件小部件的问题。

关于qt - 不要在 Qt 中的 setVisble 后立即重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com