[Solved]Load qss as global.



  • Is the below the correct way to load a style sheet globally? I am trying to make things a bit more efficient than:
    @middleIntText -> setStyleSheet("QLineEdit { border: 1px solid gray;border-radius: 5px;padding: 0 8px;selection-background-color:darkgray;height:40px;font-size:15px;}");@

    I thought the following would work at loading QLineEdit the one time for all QLineEdit widgets:
    qss file:
    @QLineEdit { border: 1px solid gray;
    border-radius: 5px;
    padding: 0 8px;
    selection-background-color:darkgray;
    height:40px;
    font-size:15px;}@

    cpp file:
    @QApplication a(argc, argv);
    QFile stylesheet("formStyle.qss");
    stylesheet.open(QFile::ReadOnly);
    QString setSheet = QLatin1String(stylesheet.readAll());
    a.setStyleSheet(setSheet);@

    Perhaps this is right and I am doing something else wrong?



  • It is and you are not.



  • Okay that is good to read, thank you very much. Now I have to find out why its not working. Back to the books! Maybe I will get this figured out by tomorrow... hopeful.


  • Moderators

    Don't know if that would help, but you can try setting the style sheet on your topmost widget (usually a QMainWindow) with ::setStyleSheet(). Settings are then propagated down the meta object hierarchy.



  • That makes great sense. Need to get some sleep I think cause everything I tried on your suggestion was a no go though. I tried applying in the cardUI class and to QMainWindow and to it in main with cardUI widget.

    Like:
    @QMainWindow::setStyleSheet(setSheet);@
    and
    @cardUI::setStyleSheet(setSheet);@


  • Moderators

    You have to set it on an existing object, not a class itself, but you probably know that. Yeah, get some sleep, that usually helps in problem solving :) Your code looks good, I don't know why it's not working. Maybe check your QSS file's encoding (with/ without BOM, etc.)?


  • Moderators

    Ah, one thing. Be sure to check if the QFile::open() was successful (it returns a handy bool for that). Maybe it's just that the file is not where you point to :)



  • [quote author="weblife" date="1343372467"]That makes great sense. Need to get some sleep I think cause everything I tried on your suggestion was a no go though. I tried applying in the cardUI class and to QMainWindow and to it in main with cardUI widget.
    [/quote]

    Applying global stylesheets to QApplication instance is fine but make sure that your qss is correct. Here is a working example source that I had to use recently for my project following the official "Qt Style Sheets examples":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qcheckbox :

    @
    QString sCheckBoxStyle = "QCheckBox::indicator { width: 30px; height: 30px; }";
    sCheckBoxStyle += " QCheckBox::indicator:unchecked { image: url(:/images/checkbox_unchecked.png); }";
    sCheckBoxStyle += "QCheckBox::indicator:unchecked:hover { image: url(:/images/checkbox_unchecked.png); }";
    sCheckBoxStyle += "QCheckBox::indicator:unchecked:pressed { image: url(:/images/checkbox_unchecked.png); }";
    sCheckBoxStyle += " QCheckBox::indicator:checked { image: url(:/images/checkbox_checked.png); }";
    sCheckBoxStyle += "QCheckBox::indicator:checked:hover { image: url(:/images/checkbox_checked.png); }";
    sCheckBoxStyle += "QCheckBox::indicator:checked:pressed { image: url(:/images/checkbox_checked.png); }";
    sCheckBoxStyle += "QCheckBox::indicator:indeterminate:hover { image: url(:/images/checkbox_checked.png); }";
    sCheckBoxStyle += "QCheckBox::indicator:indeterminate:pressed { image: url(:/images/checkbox_checked.png); }";

    app->setStyleSheet(sCheckBoxStyle);
    

    @



  • @sierdzio and
    @leon.anavi

    You guys are rock stars. Helped me and taught me useful insight, thank you so much. Even though I haven't tried out your suggestions to the fullest I see the answer on the horizon, but now I must sleep.



  • It was the target object. Told you I was tired. In my UI class was centralized on QWidget. I also had to point to the correct location.

    @QWidget::setStyleSheet(setSheet);@



  • [quote author="weblife" date="1343398537"]It was the target object. Told you I was tired. In my UI class was centralized on QWidget.

    @QWidget::setStyleSheet(setSheet);@[/quote]

    I am happy that you have found and fixed your issue. Well done :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.