Load global stylesheet from resource.qrc and apply it to the application?



  • can someone tell me how to load a global stylesheet from my resources and apply it to my application?

    this is my mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QFontDatabase>
    #include <QFileDialog>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->statusBar->setSizeGripEnabled(false);
        setStyleSheet("Styles/Style.qss");
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    and this is my style.qss file

    
    QToolBar {
    	border-image: url(Images/Panel1_Normal);
    	border-top: 4;
    	border-left: 4;
    	border-bottom: 4;
    	border-right: 4;
    }
    

    I feel it might have something to do with me passing the file in directly and not getting the contents as a string, however I'm not sure how to do this.

    EDIT: ok so I tried to do this

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QFontDatabase>
    #include <QFileDialog>
    #include <QTextStream>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->statusBar->setSizeGripEnabled(false);
    
        QFile file("Styles/Style.qss");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
        QTextStream in(&file);
        QString text;
        text = in.readAll();
        file.close();
    
        setStyleSheet(text);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    to convert the style sheet into a string but my application toolbar still doesn't change

    EDIT2: ok so I figured it out I just did this in my main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    #include <QFile>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
    
        // Load an application style
        QFile styleFile( ":/Styles/Resources/Styles/Style.qss");
        styleFile.open( QFile::ReadOnly );
    
        // Apply the loaded stylesheet
        QString style( styleFile.readAll() );
        a.setStyleSheet( style );
    
        w.show();
    
        return a.exec();
    }
    
    

Log in to reply
 

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