Pergunta

I am making a C++ application, and I created a function that will apply a stylesheet from a css file. This is the method:

void MainWindow::load_css_file() {
    QFile styleFile(":/light_style_css");
    styleFile.open(QFile::ReadOnly);

    QString styleSheet = styleFile.readAll();
    setStyleSheet(styleSheet);

};

This works fine, except for the fact that I need to run "make" every time I make a change to "light_style_css" (which is an alias for a css file in my project resource file).

But, when I change the method to something like this:

void MainWindow::load_css_file() {
    QFile styleFile("../stylesheets/light_style.css");
    styleFile.open(QFile::ReadOnly);

    QString styleSheet = styleFile.readAll();
    setStyleSheet(styleSheet);

};

I can make changes to the file, and the program updates without having to run "make" for the changes to take place.

Is there a way, that I can use the resource system, without having to run "make" for the changes to take place?

This is my resource file:

<!DOCTYPE RCC><RCC version="1.0">
 <qresource>
     <file alias="light_style_css">stylesheets/light_style.css</file>
 </qresource>
 </RCC>
Foi útil?

Solução 2

We do the following solution in our project (I can't provide code because of company policies):

  • First, we build all resources to separate binary .rcc file. It is not necessary, but helpful.
  • Second, In release builds we always load resources from the Qt resource system.
  • Third, only in debug builds:

    1. We check the resource folder for a main.qss file.
    2. If it exists: we open it and replace all :/ with ./resource/ string, then apply it
    3. If it doesn't exist: we load resources as in the release version.

Outras dicas

Simply said: no, you can't.

Resources are built into your executable. If you want to change them, you need to rebuild the executable.

Your second method reads the file from disk, so it doesn't have that behavior.

If this is a real problem for you, give the possibility to pass in the stylesheet as an argument on the command line, and default to the resource. That way you can debug the stylesheet as much as you want without recompiling every time. Rebuild once it's satisfactory (and before you ship!).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top