Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Load global stylesheet from resource.qrc and apply it to the application?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Shadowblitz16S Offline
    Shadowblitz16S Offline
    Shadowblitz16
    wrote on last edited by Shadowblitz16
    #1

    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();
    }
    
    
    1 Reply Last reply
    1

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved