Search code examples
c++qtqt5stylesheetqtstylesheets

Unable to set stylesheet properties using qss file


I am trying to set some styles to all the QLineEdits in my application. Following is the code:

QLineEdit {
    border: none;
    padding-bottom: 2px;
    border-bottom: 1px solid black;
    color: #000000;
    background-color:rgba(0,0,0,0);
}
QLineEdit:focus{
    border: 0px solid white;
    border-bottom: 2px solid #2196F3;
    color: #000000;
}

When I input this style using the GUI i.e by setting the stylesheet option in form editor for each individual lineEdit, it works.

enter image description here

However when I try to add the same code using a qss file in resources, it doesn't work. I use the following code for applying stylesheet:

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <conio.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    QFile styleFile( ":/Stylesheets/QLineEdit.qss" );
//    styleFile.open( QFile::ReadOnly );
//    std::printf("hi0");

//    // Apply the loaded stylesheet
//    QString style( styleFile.readAll() );
//    a.setStyleSheet( style );

    QFile file(":/Stylesheets/QLineEdit.qss");
    file.open(QFile::ReadOnly);
    QString styleSheet = QLatin1String(file.readAll());
    a.setStyleSheet(styleSheet);

    MainWindow w;
    w.show();
    return a.exec();
}

What could be the problem here?

Edit: Adding code for the QPushButton:

QPushButton, QPushButton:focus {
  background-color:#2196F3;
  border: none;
  color: white;
  padding: 3px 20px;
}


QPushButton:hover, QPushButton:hover:focus {
  background-color: #1976D2;
  border-color: #ffffff;
}



QPushButton:pressed,
QPushButton:pressed:focus {
  background-color: #388E3C;
  border: none;
  color: white;
}

QPushButton:disabled {
    color: #cccccc;
    background-color: #cccccc;
    border: none;
}

Solution

  • Let's summarize the outcome of the discussion.

    Replace the file.open(QFile::ReadOnly); with file.open(QFile::ReadOnly | QFile::Text); QFile::Text is important, because:

    The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.

    Furthermore, when setting the stylesheet globally, there are some specifics which should be taken into account:

    A stylesheet affects the widget and everything below it in the widget's hierarchy. If set for a widget explicitly (from the code or using the form editor) the parents of the widget are not affected, as if it were set for the whole application. E.g. if you set the following: QWidget { background-color: red; } for a particular widget, this widget and all of its children will have a red background. If you set the same stylesheet from the qss file for the whole application, all the widgets will have a red background. So a great deal of care should be taken about the inheritance between the widgets. Using the right selector types is then crucial.