Render a widget on top of others at runtime
-
I have a mainwindow derived class having a QStackedWidget in the central part. Now when some event occur I need to show a custom widget at runtime on top of the central part. I tried using raise but it's not working
// When I press a Game button i need to show the custom widget on top void BeastBurst::onGameButtonClicked(GameButton *button) { if (button == nullptr) { if (BeastBurst::m_activeGame) { BeastBurst::m_activeGame->hide(); BeastBurst::m_activeGame = nullptr; } m_gamesToolBar->setSelectedButton(nullptr); return; } if (BeastBurst::m_activeGame && button == m_gamesToolBar->getSelectedButton()) { BeastBurst::m_activeGame->hide(); BeastBurst::m_activeGame = nullptr; m_gamesToolBar->setSelectedButton(nullptr); return; } if (BeastBurst::m_activeGame) { BeastBurst::m_activeGame->hide(); BeastBurst::m_activeGame = nullptr; } std::shared_ptr<Game> selectedGame; for (auto game : m_games) { if (game->getGameId() == button->getGameId()) { game->refresh(); selectedGame = game; break; } } if (selectedGame) { // m_gameWindow is the custom widget I need to show on top of m_ui->mainContainer m_gameWindow->showGame(selectedGame->getGameId()); m_gameWindow->showContent(0); QPoint pos = m_ui->mainContainer->mapTo(m_ui->centralWidget, QPoint(0, 0)); m_gameWindow->move(pos); m_gameWindow->resize(m_ui->mainContainer->size()); m_gameWindow->show(); m_ui->mainContainer->lower(); // I tried to lower mainContainer m_gameWindow->raise(); // and raise gameWindow but it's not working BeastBurst::m_activeGame = selectedGame; m_gamesToolBar->setSelectedButton(button); } }
Can I have a help
-
@franco-amato said in Render a widget on top of others at runtime:
I have a mainwindow derived class having a QStackedWidget in the central part. Now when some event occur I need to show a custom widget at runtime on top of the central part. I tried using raise but it's not working
What do you mean by "on top"? As separate window? What's the point of the stacked widget then?
Why don't you just switch the stacked widget page to show your game widget?!@franco-amato said in Render a widget on top of others at runtime:
m_gameWindow->show(); m_ui->mainContainer->lower(); // I tried to lower mainContainer m_gameWindow->raise(); // and raise gameWindow but it's not working
Of course it's not... because this is not what
lower()
andraise()
are made for...The overall design seems to have some flaws which you should fix.
-
Hi,
what I would like to achieve is to to create a semi translucent Game widget in order to "see" the widget that's behing it.
Let's say that in my mainwindow I have a central widget that's a webview wich render some web sites. When I press a button I need to show a game widget in front of it and the website should be partially visible (due to the semi transparency of the Game widget) -
@Pl45m4 said in Render a widget on top of others at runtime:
What do you mean by "on top"? As separate window? What's the point of the stacked widget then?
Why don't you just switch the stacked widget page to show your game widget?!From what I understand this is meant as a non-modal popup/overlay. Everything doesn't have to be in the same plane in a UI, overlap is sometime wanted.
@franco-amato How is
m_gameWindow
created?
If you want to show it on top ofm_ui->mainContainer
, just create it with mainContainer as the parent. Don't add it to a layout or the QStackedWidget. The mapTo shouldn't be necessary since its position would be relative to its parent. raise shouldn't be needed if its added the the container last.