PDA

View Full Version : diffrent resouce file at run time



bartarya.parul
21st July 2016, 08:22
I have 2 images resouce files one for 96dpi, one for 120dpi. and i want to load these diffrent resouce(qrc)files at run time.
can u help me with example.i have searched it,but don't get exactly ,what i have to do.

anda_skoa
21st July 2016, 08:56
Do you create two rcc files along side your application or do you build both resources into the executable?

Cheers,
_

bartarya.parul
21st July 2016, 10:58
yes, i want to load resourc file at run time ,this is because i m try to develop high dpi application .and in my application i m set image at label.but after high dpi my image looks streched.thats y i want to load two diffrent resouce file at run time(normal, high dpi)with diffrent image sizes .
thanku

anda_skoa
21st July 2016, 11:44
yes, i want to load resourc file at run time ,this is because i m try to develop high dpi application .and in my application i m set image at label.but after high dpi my image looks streched.thats y i want to load two diffrent resouce file at run time(normal, high dpi)with diffrent image sizes .
thanku

That would be possible with both images compiled in so I wanted to check if this is about loading resource files :-)

http://doc.qt.io/qt-5/resources.html#external-binary-resources

Cheers,
_

bartarya.parul
21st July 2016, 12:28
how to add images in .rcc file.

anda_skoa
21st July 2016, 17:26
I am afraid I don't understand the question.

You add images to a Qt resource file like any other file.

Maybe you should read the whole section in the documentation on resources?

Cheers,
_

bartarya.parul
22nd July 2016, 13:45
one more thing is that.i m not able to load images of binary .rcc file at run time.
i generated two diffrent .qrc file,which same file name.like
res.qrc - i.png.
myres.qrc - i.png.
then run app
if(string=99)
{
set path=....res.qrc.
ui->label->setPixmap(QPixmap(":/i.png"));
}
else if(string=120)
{
set path=....myres.qrc.
ui->label->setPixmap(QPixmap(":/i.png"));
}
and label set i.png of res.qrc file always.
can u help now.

Added after 6 minutes:

can u help me with example.

Lesiok
22nd July 2016, 15:14
Create one qrc file like this :

<RCC>
<qresource prefix="/99">
<file>i.png</file>
</qresource>
<qresource prefix="/120">
<file>i.png</file>
</qresource>
</RCC>
then use it :
if(string==99)
{
ui->label->setPixmap(QPixmap(":/99/i.png"));
}
else if(string==120)
{
ui->label->setPixmap(QPixmap(":/120/i.png"));
}or

QString pic_name(":/%1/i.png");
ui->label->setPixmap(QPixmap(pic_name.arg(string)));

bartarya.parul
25th July 2016, 05:54
sorry but u don't get my point.
i only load diffrent .qrc (in which same image file name exist) at run time.
when i press 99 then .qrc1 images files should display,and when i press 120 then .qrc2 images(@2x)should display.
i dont want to change name of images.because there are 30+ images. in my qrc file.
thanku.

Lesiok
25th July 2016, 13:17
Think : what is a result of this expression
if(string=99) ?

d_stranz
25th July 2016, 16:54
what is a result of this expression

It assigns the value 99 to string, then returns true :rolleyes:


i only load diffrent .qrc (in which same image file name exist) at run time.
when i press 99 then .qrc1 images files should display,and when i press 120 then .qrc2 images(@2x)should display.
i dont want to change name of images.because there are 30+ images. in my qrc file.

If your qrc file contains 30+ images, what size are they? 99 or 120?

Do you not understand that the contents of qrc files are compiled into your program as binary resources. They are no different than source code C++ files in that way - they are compiled, and after they are compiled, your program no longer needs the qrc file at run time. So it makes no sense at all to say "I want to load qrc1 or qrc2" because your program can't load a qrc file and do anything with it. What you are asking for is not possible.

What Lesiok and others have been trying to tell you is that the only way you can load images with different resolution is to put them all into the same resource file and decide which one you want to load at run time.

If each or your 30+ files has the same name for 99 and 120 resolution, the qrc architecture allows that: you put them in different subdirectories of the resource file tree. This is exactly what Lesiok said a few posts ago. Go back and read it again:


Create one qrc file like this :

anda_skoa
25th July 2016, 17:23
Do you not understand that the contents of qrc files are compiled into your program as binary resources. They are no different than source code C++ files in that way - they are compiled, and after they are compiled, your program no longer needs the qrc file at run time. So it makes no sense at all to say "I want to load qrc1 or qrc2" because your program can't load a qrc file and do anything with it. What you are asking for is not possible.

While you are of course correct that qrc files are used at built time to describe contents of the QResource system, it is possible to generate "binary resource" files and load them during runtime (see my link in comment #4).



What Lesiok and others have been trying to tell you is that the only way you can load images with different resolution is to put them all into the same resource file and decide which one you want to load at run time.

That is the usual option, but see above.

Cheers,
_

d_stranz
26th July 2016, 02:43
it is possible to generate "binary resource" files

Yes, had forgotten that. I doubt that the OP could overcome the logistics of deploying an app with external resources loaded at runtime.

bartarya.parul
26th July 2016, 05:35
QString str="C:/Users/dev/Desktop/dpi.txt";


QFile file(str);
if(!file.open(QIODevice::Text | QIODevice::ReadOnly)){
return;
}
QString strData = file.readAll();


file.close();

int iValue = strData.toInt();
if(iValue==120)
{
qrcFile="C:/Users/dev/Desktop/m_image/myres.qrc";
}
else if(iValue==144)
qrcFile="C:/Users/dev/Desktop/m_image/res/m_icon.qrc";

QFileInfo qrcFileInfo(qrcFile);
QString rccFileName = qrcFileInfo.baseName() + ".rcc";
QString rccPath = qrcFileInfo.absolutePath() + "/" + rccFileName;
qDebug()<<qrcFile<<rccFileName<<rccPath;
QStringList args;


args << "-binary";
args << qrcFileInfo.fileName();
args << "-o" << rccPath;

QProcess rccProc;

QDir::setCurrent(qrcFileInfo.absolutePath());

bool isLoaded = false;
int ret = -1;


ret = rccProc.execute("rcc", args);


QFile filercc(rccPath);
if(!filercc.open(QFile::ReadOnly |
QIODevice::Text))
{
qDebug() << " Could not open file for writing";
return;
}
else
qDebug()<<"open";

while(!filercc.atEnd())
qDebug() << filercc.readLine();






if (ret == 0) // rcc executed successfully
{
qDebug()<<ret<<qrcFileInfo.absolutePath();


isLoaded = QResource::registerResource(rccPath,("/virtual/C:/Users/dev/Desktop/m_image/res"));
qDebug()<<"second path--"<<rccPath;
qDebug()<<isLoaded;
if (isLoaded)



ui->label->setPixmap(QPixmap(":/1/CHAT.png"));

}

this is my example code. my output is i get same image with diffrent dpi.
thank u all.

anda_skoa
26th July 2016, 09:36
if you require the file to exist at runtime, you could just load it directly.

But if we overlook that at the moment then we see that your are not trying to access any files from the loaded resource, so how did you arrive at the conclusion that it always loads the same one?

Cheers,
_

d_stranz
27th July 2016, 00:23
Wow. I was wrong about the deployment problem. Deploy different qrc files and deploy the resource compiler to compile the qrc files at runtime on the user's system. I should have thought of that when I posted my original answer.

After all, the OP's question was "different resource file at run time" and sure enough, there's a way to do it.

jefftee
27th July 2016, 04:06
Somewhere, someone must be teaching that Qt resource files are the solution to every problem... Seems like a rash of posts related to resource files lately... :)