Results 1 to 17 of 17

Thread: diffrent resouce file at run time

  1. #1
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default diffrent resouce file at run time

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    Do you create two rcc files along side your application or do you build both resources into the executable?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    Quote Originally Posted by bartarya.parul View Post
    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...nary-resources

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    how to add images in .rcc file.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    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,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    bartarya.parul (22nd July 2016)

  8. #7
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    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.
    Last edited by bartarya.parul; 22nd July 2016 at 13:45.

  9. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    Create one qrc file like this :
    Qt Code:
    1. <RCC>
    2. <qresource prefix="/99">
    3. <file>i.png</file>
    4. </qresource>
    5. <qresource prefix="/120">
    6. <file>i.png</file>
    7. </qresource>
    8. </RCC>
    To copy to clipboard, switch view to plain text mode 
    then use it :
    Qt Code:
    1. if(string==99)
    2. {
    3. ui->label->setPixmap(QPixmap(":/99/i.png"));
    4. }
    5. else if(string==120)
    6. {
    7. ui->label->setPixmap(QPixmap(":/120/i.png"));
    8. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QString pic_name(":/%1/i.png");
    2. ui->label->setPixmap(QPixmap(pic_name.arg(string)));
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    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.

  11. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    Think : what is a result of this expression
    Qt Code:
    1. if(string=99)
    To copy to clipboard, switch view to plain text mode 
    ?

  12. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: diffrent resouce file at run time

    what is a result of this expression
    It assigns the value 99 to string, then returns true

    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 :
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    Quote Originally Posted by d_stranz View Post
    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).

    Quote Originally Posted by d_stranz View Post
    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,
    _

  14. The following user says thank you to anda_skoa for this useful post:

    d_stranz (26th July 2016)

  15. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: diffrent resouce file at run time

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  16. #14
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    Qt Code:
    1. QString str="C:/Users/dev/Desktop/dpi.txt";
    2.  
    3.  
    4. QFile file(str);
    5. if(!file.open(QIODevice::Text | QIODevice::ReadOnly)){
    6. return;
    7. }
    8. QString strData = file.readAll();
    9.  
    10.  
    11. file.close();
    12.  
    13. int iValue = strData.toInt();
    14. if(iValue==120)
    15. {
    16. qrcFile="C:/Users/dev/Desktop/m_image/myres.qrc";
    17. }
    18. else if(iValue==144)
    19. qrcFile="C:/Users/dev/Desktop/m_image/res/m_icon.qrc";
    20.  
    21. QFileInfo qrcFileInfo(qrcFile);
    22. QString rccFileName = qrcFileInfo.baseName() + ".rcc";
    23. QString rccPath = qrcFileInfo.absolutePath() + "/" + rccFileName;
    24. qDebug()<<qrcFile<<rccFileName<<rccPath;
    25.  
    26.  
    27. args << "-binary";
    28. args << qrcFileInfo.fileName();
    29. args << "-o" << rccPath;
    30.  
    31. QProcess rccProc;
    32.  
    33. QDir::setCurrent(qrcFileInfo.absolutePath());
    34.  
    35. bool isLoaded = false;
    36. int ret = -1;
    37.  
    38.  
    39. ret = rccProc.execute("rcc", args);
    40.  
    41.  
    42. QFile filercc(rccPath);
    43. if(!filercc.open(QFile::ReadOnly |
    44. QIODevice::Text))
    45. {
    46. qDebug() << " Could not open file for writing";
    47. return;
    48. }
    49. else
    50. qDebug()<<"open";
    51.  
    52. while(!filercc.atEnd())
    53. qDebug() << filercc.readLine();
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60. if (ret == 0) // rcc executed successfully
    61. {
    62. qDebug()<<ret<<qrcFileInfo.absolutePath();
    63.  
    64.  
    65. isLoaded = QResource::registerResource(rccPath,("/virtual/C:/Users/dev/Desktop/m_image/res"));
    66. qDebug()<<"second path--"<<rccPath;
    67. qDebug()<<isLoaded;
    68. if (isLoaded)
    69.  
    70.  
    71.  
    72. ui->label->setPixmap(QPixmap(":/1/CHAT.png"));
    73.  
    74. }
    To copy to clipboard, switch view to plain text mode 
    this is my example code. my output is i get same image with diffrent dpi.
    thank u all.
    Last edited by anda_skoa; 26th July 2016 at 09:32. Reason: missing [code] tags

  17. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: diffrent resouce file at run time

    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,
    _

  18. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: diffrent resouce file at run time

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  19. #17
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: diffrent resouce file at run time

    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...
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QIcon Active-/Normal Mode have no diffrent effect
    By cic in forum Qt Programming
    Replies: 0
    Last Post: 4th September 2013, 09:49
  2. Replies: 1
    Last Post: 11th November 2009, 12:22
  3. Replies: 0
    Last Post: 6th March 2009, 08:19
  4. How to set the file's date and time.
    By santosh.kumar in forum General Programming
    Replies: 3
    Last Post: 12th October 2007, 11:13
  5. converting file formats at run time
    By Aditi in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2006, 10:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.