質問

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();
}
役に立ちましたか?

解決

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top