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. Dinamic add resource[Solved]
QtWS25 Last Chance

Dinamic add resource[Solved]

Scheduled Pinned Locked Moved General and Desktop
21 Posts 4 Posters 19.3k Views
  • 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.
  • R Offline
    R Offline
    Ruzik
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jim_kaiser
      wrote on last edited by
      #2

      [ 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.
      ]

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I have never tried to use this, but shouldn't you use the QResource::registerResource method if you want to use a dynamic resource this way?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jim_kaiser
          wrote on last edited by
          #4

          Adding dynamic resources in a QTextDocument is quite simple though.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ruzik
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jim_kaiser
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Ruzik
                wrote on last edited by
                #7

                I understand that you have misunderstood me - I just need to connect the required resources for its program during its execution (ie when you run the plug will connect zanogo resources), I do not need to edit or save resources

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jim_kaiser
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Ruzik
                    wrote on last edited by
                    #9

                    bq. If you could explain your use case, I could suggest to you how to go about it.

                    I try: 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

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Ruzik
                        wrote on last edited by
                        #11

                        Andre, can you write code to do it

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          Like I said: I did not try to do this myself. I could write code to do it, but only as a commercial consultant.

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            Ruzik
                            wrote on last edited by
                            #13

                            Clearly, very pity

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              loladiro
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                Ruzik
                                wrote on last edited by
                                #15

                                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

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  jim_kaiser
                                  wrote on last edited by
                                  #16

                                  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.. ]

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    Ruzik
                                    wrote on last edited by
                                    #17

                                    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

                                    1 Reply Last reply
                                    0
                                    • J Offline
                                      J Offline
                                      jim_kaiser
                                      wrote on last edited by
                                      #18

                                      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 :) ]

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        Ruzik
                                        wrote on last edited by
                                        #19

                                        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

                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          jim_kaiser
                                          wrote on last edited by
                                          #20

                                          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? :)

                                          1 Reply Last reply
                                          2

                                          • Login

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