Need to position a widget and make it semi-transparent
-
I have a QMainWindow derived object (BeastBurst) and I need to place a QWidget object (Game) at a specific position relative to the mainwindow, position is x = m_leftToolBar.width() and y = 81. I also need to make this widget semi transparent. I tried almost everything. I am using Qt 6.8
/* In mainwindow */
// selectedGame is the widget i want to place and is of type Game selectedGame->setParent(this); // select the mainwindow as parent // If I don't set the Qt::Window flag, then the position will be completely wrong selectedGame->setWindowFlags(Qt::FramelessWindowHint | Qt::Window | Qt::WindowStaysOnTopHint); // the 70% of transparency has no effect selectedGame->setStyleSheet("QWidget{background-color: rgba(37, 38, 52, 0.7);}"); // this will move the selectedGame widget selectedGame->moveRelativeToParent(this, m_leftToolBar->width(), 81); selectedGame->show();
/* in Game */
void Game::moveRelativeToParent(QWidget *parent, int xOffset, int yOffset) { if (!parent) { return; } QPoint parentGlobalPos = parent->mapToGlobal(QPoint(0, 0)); move(parentGlobalPos.x() + xOffset, parentGlobalPos.y() + yOffset); }
I also tried to set
setAttribute(Qt::WA_TranslucentBackground);
in the Game constructor but it will make completely transparent the widget and I don't want it, I want it semi transparent.
Any help please -
I discovered a problem.
Removing the Qt::Window from the flags makes the widget to go behind other widgets that's why I cannot see it.
```
selectedGame->setParent(this);selectedGame->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); selectedGame->moveRelativeToParent(this, m_leftToolBar->width(), 81); selectedGame->setStyleSheet("background-color: rgba(37, 38, 52, 0.7);"); selectedGame->show(); selectedGame->raise();
calling raise() didn't solve the problem
-
I discovered a problem.
Removing the Qt::Window from the flags makes the widget to go behind other widgets that's why I cannot see it.
```
selectedGame->setParent(this);selectedGame->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); selectedGame->moveRelativeToParent(this, m_leftToolBar->width(), 81); selectedGame->setStyleSheet("background-color: rgba(37, 38, 52, 0.7);"); selectedGame->show(); selectedGame->raise();
calling raise() didn't solve the problem
@franco-amato hi. To make your widget semi transparent, you have to follow three steps:
1- setAttribute(Qt::WA_TranslucentBackground);
2- setWindowFlags(Qt::FramelessWindowHint);
3- Call paint Event and use painter.setOpacity(0.75); //exemple valueThis is a demo project how you can achieve it https://github.com/jordanprog86/QtTransparentWindow.git
I hope it helps you
-
@Ronel_qtmaster I also have the problem of the positioning of the widget. If I don't set Qt::Window or Qt::Tool then the widget is placed behind other widgets even calling raise()
-
@Ronel_qtmaster I also have the problem of the positioning of the widget. If I don't set Qt::Window or Qt::Tool then the widget is placed behind other widgets even calling raise()
@franco-amato i think you don't need to use show() when your widget has a parent , also you can use setGeometry alongside with setWindowFlags(Qt::WindowStaysOnTopHint);
-
@Ronel_qtmaster how to show it only when I need it without using show() ? The Qt::WindowStaysOnTopHint is already added
-
@Ronel_qtmaster how to show it only when I need it without using show() ? The Qt::WindowStaysOnTopHint is already added
@franco-amato If you have assigned a parent to your widget using "this" it is not necessary to call show() again
-
The widget must be shown only when I press a button and I have to place it to a specific position in the main window.:
This is the slot executed when I press the button (selectedGame is the widget I have to show)void BeastBurst::onGameButtonClicked(GameButton *button) { std::shared_ptr<Game> selectedGame; for (auto btn : m_gamesButtonGroup.buttons()) { btn->setStyleSheet( "GameButton {" " border:none;" " background-color: transparent;" "}" "GameButton:hover {" " background-color: #30303d;" "}" ); } for(auto game : m_games) { if(game->getGameId() == button->getGameId()) { game->refresh(); selectedGame = game; break; } } button->setStyleSheet("border:none; background-color: rgba(46, 58, 74, 0.7);"); if (selectedGame->isVisible()) { selectedGame->hide(); return; } Qt::WindowFlags currentFlags = selectedGame->windowFlags(); selectedGame->setWindowFlags(currentFlags | Qt::WindowStaysOnTopHint); selectedGame->move(m_leftToolBar->width(), 84); selectedGame->show(); selectedGame->activateWindow(); selectedGame->raise(); ui->mainBody->setVisible(false); }
-
F franco.amato has marked this topic as solved on