Dinamic add resource[Solved]
-
Hellow, i can dinamic add resoucre to my program. For example user selected some resource file in the file system and add it to my program
I try use class QResource, but it is doesnt work:
@QResource res;
res.setFileName(QFileDialog::getOpenFileName());
//QSS
QFile file(":/Other/qss/default.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);@
I try class QDesignerFormWindowInterface, but compiler say that flie QDesignerFormWindowInterface doesnt find
Please, correct if I wrote something wrong, I do not know English very well -
[ Edit2: My previous statements were wrong so here's the deal. The resources need to be compiled by rcc, so they have to be added to the .qrc file before that and the rcc step generates the .rcc file.
@ rcc -binary myresource.qrc -o myresource.rcc @
@ QResource::registerResource("/path/to/myresource.rcc"); @
So unless you create qrc files on the fly and externally run the rcc compiler and then registerResource(), its not possible! In that case you probably shouldn't be doing that anyway. Just read it as a QFile and use it.
][ Edit: From QResource documentation
bq. Dynamic Resource Loading
A resource can be left out of an application's binary and loaded when it is needed at run-time by using the registerResource() function. The resource file passed into registerResource() must be a binary resource as created by rcc. Further information about binary resources can be found in The Qt Resource System documentation.Some reading should solve this for you.
] -
Adding dynamic resources in a QTextDocument is quite simple though.
-
QResource::registerResource dont work too
@ QResource::registerResource(QFileDialog::getOpenFileName());
//QSS
QFile file(":/Other/qss/default.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);@
jim_kaiser, you tell about opening through QFile, but what i must do after opening to add resource -
Sorry, I have confused you by editing my post too many times. I said.. just open() using QFile, readAll() and use it. Why do you want to save in the resources. Also, it is not possible to dynamically add resources... unless you do (just theoretical)(from my above comment edit):
The resources need to be compiled by rcc compiler, so they have to be added to the .qrc file before that and the rcc compiler generates the .rcc file.
@
rcc -binary myresource.qrc -o myresource.rcc
@
@
QResource::registerResource("/path/to/myresource.rcc");
@So unless you create qrc files on the fly and externally run the rcc compiler and then registerResource(), its not possible!
-
The way resources work is this:
When you develop, you add a resources file (.qrc) to your project. This .qrc file will store the paths to the resources you have added in the .qrc. This is then compiled by Qt's rcc compiler and bundled with the executable. So, you can load a file at runtime and use it but not as a resource unless you have added it prior to compilation in the .qrc.
If you could explain your use case, I could suggest to you how to go about it.
-
-
[quote author="jim_kaiser" date="1308066076"]The way resources work is this:
When you develop, you add a resources file (.qrc) to your project. This .qrc file will store the paths to the resources you have added in the .qrc. This is then compiled by Qt's rcc compiler and bundled with the executable. So, you can load a file at runtime and use it but not as a resource unless you have added it prior to compilation in the .qrc.
If you could explain your use case, I could suggest to you how to go about it.[/quote]
You can also create external resources. My guess is, that the topic starter was attempting to do this.
-
It is really not that difficult. @jim_kaiser has already posted everything you need to know. Otherwise have a look at the "documentation":http://doc.qt.nokia.com/4.7/resources.html
-
bq. @ r.setFileName(QFileDialog::getOpenFileName()); @
Hehehe.. you didn't find the holy grail mate... The file name to be passed in this function is of the .qrc file ... not a css or image file..
bq. i develop Qss Editor -and i want that user can add any resources files to my program to function “preview” can preview all design with pictures from resources-
If only I could understand what the problem is.. Why do you want resources.. you can read a file's data and store a QFile in memory or store file paths and create QFile when needed and use the data for whatever purpose.. store a list of QFiles, or paths.. whats the problem!
[ Edit: According to what you state, you clearly want resources to be added dynamically to your .exe. Only way like I said before... add new .qrc or edit your existing .qrc (its just xml).. and add new entry for your resource.. then registerResource() or if you edited existing .qrc.. unregisterResource() first maybe before register.. I cannot be more clear.. good luck.. ]
-
bq. The file name to be passed in this function is of the .qrc file … not a css or image file..
I understand and i open resource file(.qrc)
bq. If only I could understand what the problem is..I need that image and other files from dinamic resource work in program and when i write in the Qss
@QPushButton{
border-image: url(a.png);
}@
button has this image as your border image, despite the fact that the resource file has been connected to the last line of code
Sorry for my vary bad english, i think that i insufficient understand you, can i write a example of code, about way you say -
hmm... okay.. in general when we refer to resources we use the syntax :/.../...
i.e. If your .qrc is in x folder. Then the file x/images/a.png can be referred to by :/images/a.png..
By the Qt philosophy that should work in the same way in Qss too.. though I'm not too familiar with Qss.
After the registerResource(<your_qrc>), could you try to access the file outside the Qss in normal Qt code, load using QFile or QImage? If that works, then the resource is loaded.
[ Edit: Sorry for that bit of impatience before.. I think it's clear now :) ]
-
Okay, Mr Dynamic Resource, Here you go, tested working code for dynamic resource loading...
@
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QtCore/QFileInfo>
#include <QtCore/QProcess>
#include <QtGui/QFileDialog>
#include <QtCore/QResource>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
QString qrcFile = QFileDialog::getOpenFileName(this, tr("Select resource file"));
QFileInfo qrcFileInfo(qrcFile);
QString rccFileName = qrcFileInfo.baseName() + ".rcc";
QString rccPath = qrcFileInfo.absolutePath() + "/" + rccFileName;QStringList args; args << qrcFile; args << "-binary"; args << "-o" << rccPath; QProcess rccProc; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); QString qtDirPath = env.value("QTDIR"); bool isLoaded = false; int ret = -1; rccProc.setWorkingDirectory(qtDirPath + "bin/"); ret = rccProc.execute("rcc", args); if (ret == 0) // rcc executed successfully { isLoaded = QResource::registerResource(rccPath); if (isLoaded) ui->label->setPixmap(QPixmap(":/test.jpg")); }
}
@Of course I expect you to understand, adapt it to your needs. In my case, i'm loading a qrc with one file test.jpg dynamically. And mark it [ Solved ] in the title please? :)