Dinamic add resource[Solved]
-
Andre, can you write code to do it
-
Like I said: I did not try to do this myself. I could write code to do it, but only as a commercial consultant.
-
Clearly, very pity
-
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
-
I try some information, and maybe find needed method:
@ QResource r;
r.setFileName(QFileDialog::getOpenFileName());
Q_INIT_RESOURCE(r);@
But compliler get me a error: Link unresolved external symbol -
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 :) ]
-
I try to access the file in normal Qt code:
@ QResource::registerResource(QFileDialog::getOpenFileName());
QIcon ic;
ic.addFile(":/ico/icos/document-preview.png");
this->setWindowIcon(ic);@
But it is not work -
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? :)
-
Many, many thanks for your help!!!