2

I'm trying to load in a CSS file for formatting across my entire Qt application. Currently I have my "stylesheet.css" file in the same folder as my built exe (both debug and release). However, upon running the program it produces no errors and simply outputs "test: ", so it's clearly not finding the file or perhaps I'm not reading it properly?

Forgive me if it's a dumb mistake - I'm fairly new to both Qt and C++.

#include "mainwindow.h"
#include "qfile.h"
#include "qtextstream.h"
#include <QApplication>
#include <iostream>

int main(int argc, char *argv[])
{
    QApplication program(argc, argv);

    QFile styleFile("stylesheet.css");
    styleFile.open(QIODevice::ReadOnly);
    QTextStream textStream(&styleFile);
    QString styleSheet = textStream.readAll();
    styleFile.close();
    program.setStyleSheet(styleSheet);

    std::cout << "test: " << styleSheet.toStdString() << std::endl;

    MainWindow w;
    w.showMaximized();

    return program.exec();
}
5
  • Copy the CSS file beside your source code while you're running the program via Qt. Commented Sep 5, 2013 at 7:27
  • QFile::open() returns true if successful. You should always check if the file was opened successfully before trying to do anything with it. You can also check the error that occurred with QFile::errorString(). Commented Sep 5, 2013 at 7:28
  • Thanks guys - though, after a bit more research it turns out that the problem was simpler than I thought. Also, I'll definitely remember to do that, thanks for the tip thuga. After digging a bit deeper, it turns out I was lacking a QRC file (which I wasn't even aware existed). So I created a resource (QRC) file, added the prefix "/style", and then added my stylesheet to that prefix. It now works flawlessly. Also, at one point I changed the .css to a .qss (thought I should mention it, although I doubt it made any difference). Commented Sep 5, 2013 at 7:45
  • @GeoffreyTucker You should post your solution as an answer and accept it when you can. Commented Sep 5, 2013 at 7:51
  • I actually tried doing that first - though, it says I have to wait 8 hours for having less than 10 reputation. I'll be sure to post it some time tomorrow Commented Sep 5, 2013 at 7:53

4 Answers 4

4

After digging a bit deeper, it turns out I was lacking a QRC file (which I wasn't even aware existed). So I created a resource (QRC) file, added the prefix "/style", and then added my stylesheet to that prefix. It now works flawlessly. Also, at one point I changed the .css to a .qss (thought I should mention it, although I doubt it made any difference).

Here's the final code:

#include "mainwindow.h"
#include "qfile.h"
#include "qtextstream.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication program(argc, argv);

    QFile styleFile(":/style/stylesheet.qss");
    if(styleFile.open(QIODevice::ReadOnly))
    {
        QTextStream textStream(&styleFile);
        QString styleSheet = textStream.readAll();
        styleFile.close();
        program.setStyleSheet(styleSheet);
    }

    MainWindow w;
    w.showMaximized();

    return program.exec();
}
Sign up to request clarification or add additional context in comments.

1 Comment

that is not why it was not working before. here you load the stylesheet from the resource. before you were trying to load the stylesheet from a relative path, and the current directory when doing so was not the one where the stylesheet was. the current directory is not necessarily the one where the exe file is.
0

Change

QFile styleFile("stylesheet.css");

to

QFile styleFile("C:/path/to/stylesheet.css");

You need to pass the full file path for your program to find the file. If you just give its name but not the directory it's in, the program will search for it in the current directory only (which is not necessarily the directory of your exe file), and if it's not there, it won't find it.

2 Comments

Or rather, use QApplication::applicationDirPath() to find the path that contains the executable.
Yes, of course, he should construct the full file path instead of hard-coding it. But that goes beyond the scope of his immediate problem, which is he doesn't understand relative vs absolute paths.
0

If you want to deploy this CSS file with your application, you are looking for the Qt resource file support.

1 Comment

Thanks! I definitely would have chosen your answer as best answer had I not figured it out myself last night (as seen in the comments to my original post).
0

its coz u shoud uncheck " shadow build " under projects. and check the path of the project . and make sure that .css file is inside the project folder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.