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]
Forum Updated to NodeBB v4.3 + New Features

Dinamic add resource[Solved]

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

                          Many, many thanks for your help!!!

                          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