Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: QMenu with translucent background

Tags:

qt

popup

menu

qmenu

I use Windows and I want to set a style sheet to a QMenu to give it a translucent background. In order for that to work, I first set the FramelessWindowHint, then I set the WA_TranslucentBackground attribute. Then I set my style sheet and display the menu with the popup method. It is drawn correctly, but it behaves strangely: As soon as it has the FramelessWindowHint, it is always visible (even before calling the popup() method). It does not hide itself anymore after one of its entries has been clicked.

Here is a minimalistic example:

#include <QApplication>
#include <QMenu>
#include <QPoint>
#include <QCursor>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMenu menu;
    menu.addAction("about", &a, SLOT(aboutQt()));
    menu.addAction("exit", &a, SLOT(quit()));
    menu.setWindowFlags(Qt::FramelessWindowHint);
    menu.setAttribute(Qt::WA_TranslucentBackground);
    menu.setStyleSheet("QMenu{background:rgba(255, 0, 0, 50%);}");
    menu.popup(QCursor::pos());
    return a.exec();
}
like image 475
user1703711 Avatar asked Aug 09 '26 03:08

user1703711


1 Answers

menu.setWindowFlags(menu.windowFlags() | Qt::FramelessWindowHint);

should solve your problem. Now you are clearing all flags already set by Qt.

like image 172
Dmitry Melnikov Avatar answered Aug 13 '26 14:08

Dmitry Melnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!