Search code examples
qtqlineeditqmenuqt-contextmenu

Qt: How to set context menu stylesheet for whole application's QLineEdit


As you can see in the following screenshot, the selected item's font color is white as its background color, which cause we can not see the item text, this issue exists in all the QLineEdits in my application, so i just want to set the selected item's font color to black for all QLineEdits' context menu, is there any fast way with less code to do it ?

enter image description here


Solution

  • Just set css for your application

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        a.setStyleSheet("QLineEdit QMenu::item {color: rgb(0, 0, 255);}");
    
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    This code should be corrected if you want to set font color for selected (active) item only

    a.setStyleSheet("QLineEdit QMenu::item {\ncolor: rgb(0, 0, 255);\n} QLineEdit QMenu::item::selected{ color: rgb(255, 0, 0)}");