Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QResource::registerResource with rccData instead rccFileName
Qt 6.11 is out! See what's new in the release blog

QResource::registerResource with rccData instead rccFileName

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Simply read the content of your resource into a QByteArray and pass QByteArray::constData() to registerResource(). The internal logic knows how long the resource data is.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    1
    • LazyTL Offline
      LazyTL Offline
      LazyT
      wrote on last edited by
      #3

      Thanks for your answer, but this doesn't work.

      For example:

      QResource rcc(":/rcc/icons.rcc");
      QByteArray ba = rcc.uncompressedData();
      qDebug() << rcc.isValid() << QResource::registerResource(ba.constData());
      

      Result: true false

      Christian EhrlicherC 1 Reply Last reply
      0
      • LazyTL LazyT

        Thanks for your answer, but this doesn't work.

        For example:

        QResource rcc(":/rcc/icons.rcc");
        QByteArray ba = rcc.uncompressedData();
        qDebug() << rcc.isValid() << QResource::registerResource(ba.constData());
        

        Result: true false

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @LazyT said in QResource::registerResource with rccData instead rccFileName:

        but this doesn't work.

        Because you have to pass the content of the resource file - read it with QFile. It's meant to deal with external resource files.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • LazyTL Offline
          LazyTL Offline
          LazyT
          wrote on last edited by
          #5

          Makes no difference.

          QResource and QFile shows the content of the RCC file, but register fails.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Please show your code. How did you create the external resource file?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • LazyTL Offline
              LazyTL Offline
              LazyT
              wrote on last edited by
              #7

              Thanks for your patience.

              The RCC was created with

              rcc --binary icons.qrc -o icons.rcc
              

              contains SVGs and works fine with the rccFileName variant.

              QFile rcc("/tmp/icons.rcc");
              
              if(rcc.open(QIODevice::ReadOnly))
              {
              	QByteArray ba = rcc.readAll();
              	rcc.close();
              	qDebug() << ba << QResource::registerResource(ba.constData());
              }
              else
              {
              	qDebug() << "RCC open failed";
              }
              

              Result is the content of the RCC file and false.

              Christian EhrlicherC 1 Reply Last reply
              0
              • LazyTL LazyT

                Thanks for your patience.

                The RCC was created with

                rcc --binary icons.qrc -o icons.rcc
                

                contains SVGs and works fine with the rccFileName variant.

                QFile rcc("/tmp/icons.rcc");
                
                if(rcc.open(QIODevice::ReadOnly))
                {
                	QByteArray ba = rcc.readAll();
                	rcc.close();
                	qDebug() << ba << QResource::registerResource(ba.constData());
                }
                else
                {
                	qDebug() << "RCC open failed";
                }
                

                Result is the content of the RCC file and false.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @LazyT said in QResource::registerResource with rccData instead rccFileName:

                QResource::registerResource(ba.constData());

                registerResource() takes a const uchar *. You're passing a const char * which is not automatically convertible to a const uchar * but instead implicitly converted into a QString because you did not define QT_NO_CAST_FROM_ASCII which you should to avoid such errors.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JonBJ 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @LazyT said in QResource::registerResource with rccData instead rccFileName:

                  QResource::registerResource(ba.constData());

                  registerResource() takes a const uchar *. You're passing a const char * which is not automatically convertible to a const uchar * but instead implicitly converted into a QString because you did not define QT_NO_CAST_FROM_ASCII which you should to avoid such errors.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher Wow!!

                  1 Reply Last reply
                  0
                  • LazyTL Offline
                    LazyTL Offline
                    LazyT
                    wrote on last edited by
                    #10

                    Hm,

                    DEFINES += QT_NO_CAST_FROM_ASCII
                    

                    gives tons of "is private within this context" errors.

                    QResource::registerResource(reinterpret_cast<const uchar*>(ba.constData()));
                    

                    results in true, but icons are not available because "qt.svg: Cannot open file 'xxx', because: No such file or directory".

                    I give up for today.

                    Thanks for your hints.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • LazyTL LazyT

                      Hm,

                      DEFINES += QT_NO_CAST_FROM_ASCII
                      

                      gives tons of "is private within this context" errors.

                      QResource::registerResource(reinterpret_cast<const uchar*>(ba.constData()));
                      

                      results in true, but icons are not available because "qt.svg: Cannot open file 'xxx', because: No such file or directory".

                      I give up for today.

                      Thanks for your hints.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by Christian Ehrlicher
                      #11

                      @LazyT said in QResource::registerResource with rccData instead rccFileName:

                      gives tons of "is private within this context" errors.

                      Because you have a lot of implicit casts from char* to QString which are for sure not what you want.

                      but icons are not available because

                      Works fine for me:

                      <RCC>
                          <qresource prefix="/">
                              <file>stylesheet.qss</file>
                              <file>icons/menuIndicatorChecked.png</file>
                              <file>icons/menuIndicatorUnchecked.png</file>
                              <file>icons/SimulationModeOnNormal.png</file>
                              <file>icons/dashedline.png</file>
                          </qresource>
                      </RCC>
                      
                      int main(int argc, char *argv[])
                      {
                      QApplication app(argc, argv);
                      QFile f("style.rcc");
                      f.open(QIODevice::ReadOnly);
                      auto ba = f.readAll();
                      qDebug() << ba.size();
                      qDebug() << QResource::registerResource(reinterpret_cast<const uchar*>(ba.constData()));
                      QFile fRes(":/icons/menuIndicatorChecked.png");
                      qDebug() << fRes.exists() << fRes.isOpen() << fRes.readAll().size();
                      return 0;
                      

                      -->

                      25888
                      true
                      true true 274

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      2
                      • LazyTL Offline
                        LazyTL Offline
                        LazyT
                        wrote on last edited by
                        #12

                        The problem was

                        "Warning: The data must remain valid throughout the life of any QFile that may reference the resource data."

                        Moved the resource from the subroutine and it works now.

                        Thanks!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved