문제

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