Кнопки виджета не окрашиваютсяC++

Программы на C++. Форум разработчиков
Anonymous
Кнопки виджета не окрашиваются

Сообщение Anonymous »

У меня есть собственный виджет, который создает несколько пользовательских кнопок, и я пытаюсь нарисовать их в главном окне, вызывается метод PaintEvent() из виджета и кнопок, но в окне ничего не рисуется.Это конструктор MainWindow:

Код: Выделить всё

MoaraClient::MoaraClient(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_game = std::make_shared();

Init(EBoardType::Normal, EBoardSize::Normal, m_game->GetAllNodes());
ui.baseLayout->addWidget(m_board);
m_board->update();
}
Это метод PaintEvent() из пользовательского виджета:

Код: Выделить всё

void NormalBoard::paintEvent(QPaintEvent* event)
{
std::ranges::for_each(m_buttons, [](const auto& button) {
button->update();
});
}
А это краскаEvent() пользовательской кнопки:

Код: Выделить всё

void NodeButton::paintEvent(QPaintEvent* event) {

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);

QPoint center(m_position.x, m_position.y);

painter.setBrush(Qt::white);
painter.drawEllipse(center, 20, 20);

if (m_type == EPieceType::Black) {
QRect targetRect(center.x() - 10, center.y() - 10, 20, 20);
painter.drawImage(targetRect, m_imageBlack);
}
else if (m_type == EPieceType::White) {
QRect targetRect(center.x() - 10, center.y() - 10, 20, 20);
painter.drawImage(targetRect, m_imageWhite);
}
}
Вот как я создаю кнопки, «это» — пользовательский виджет.

Код: Выделить всё

NormalBoard::NormalBoard(const Nodes& nodes)
{
for (uint8_t index = 0; index < nodes.size(); index++)
{
NodeButton* nodeButton = new NodeButton(this);
nodeButton->SetNodeIndex(index);
nodeButton->SetType(nodes[index]->GetPieceType());

m_buttons.push_back(nodeButton);
}

auto positions = LoadButtonsPositions(NORMAL_BOARD_POSITIONS);

if (positions.size() != m_buttons.size())
throw std::exception("Number of positions doesn't match number of buttons");

for (size_t index = 0; index < positions.size(); index++)
{
m_buttons[index]->SetPosition(positions[index]);
}
}
Я убедился, что пользовательский виджет является производным от QWidget, а пользовательские кнопки — от QPushButton. Запускал с помощью отладчика, кнопки инициализируются правильно, позиции не 0 (они примерно 150-400), программа входит в PaintEvent(), вызывает Painter.drawEllipse() но ничего не рисуется .

Подробнее здесь: https://stackoverflow.com/questions/787 ... ng-painted

Вернуться в «C++»