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

Extreme confusing problem

Scheduled Pinned Locked Moved Solved General and Desktop
ressources imagressource
11 Posts 3 Posters 3.7k Views 2 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.
  • P Offline
    P Offline
    PandaBoy
    wrote on last edited by A Former User
    #1

    Hello, guys, I am trying to create an icon in tray.
    The problem I met is rather strange.
    I added my icon.png to a .qrc file and loaded the image from that. The Image itself never shows up.
    Even if I copied the path or url given by qt creator itself, it just didn't work.
    However, when I moved my icon.png to a location on my disk say "c://" it worked.

    .pro file:
    RESOURCES += \
    Ima.qrc

    .qrc file:
    <RCC>
    <qresource prefix="/new">
    <file>icon.png</file>
    </qresource>
    </RCC>

    testing code:
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QGraphicsPixmapItem item(QPixmap(":/new/icon.png"));
    /* qrc:/new/icon.png won't work either */
    scene.addItem(&item);
    view.show();

    real code:
    QIcon icon("C:\icon.png");
    trayIcon->setIcon(icon);
    setWindowIcon(icon);
    trayIcon->show();
    /* it works */

    "C:\icon.png" directory works fine for both testing code and real code.

    Thank you guys! I have already solved it.
    It turns out that Q_INIT_RESOURCE(***); is truly needed.
    And Unlinked problem when I added this line can be solved in following operation:

    1. Make your "***" in the parentheses is the name of your .qrc file and correctly typed
      2.Clean your project
      3.Qmake your project
      4.Run again and problem solved.
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      is your png file in the same directory of the .qrc??

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      P 1 Reply Last reply
      0
      • M mcosta

        Hi and welcome to devnet,

        is your png file in the same directory of the .qrc??

        P Offline
        P Offline
        PandaBoy
        wrote on last edited by PandaBoy
        #3

        @mcosta
        Yes.And I tried creating a folder in the same directory of .qrc file and move png to that folder. It didn't work

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by mcosta
          #4

          Have you tried to use the resource icon in your real code??

          To show an Item in a GraphicsView you have to set correctly GraphicsScene rect and the view

          This code

          ui->label->setPixmap(QPixmap(":/images/image.png"));
          

          works well for me using the resource

          <RCC>
              <qresource prefix="/images">
                  <file>image.png</file>
              </qresource>
          </RCC>
          

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

            Maybe the Q_INIT_RESOURCE(<qrc file name>) is missing from main?

            int main(int argc, char *argv[])
            {
                Q_INIT_RESOURCE(application);
                QApplication app(argc, argv);
               
                MainWindow mainWin;
                mainWin.show();
                return app.exec();
            }
            P 1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              @Rondog Q_INIT_RESOURCE is not required if you add the resource file in the .pro file

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • R Rondog

                Maybe the Q_INIT_RESOURCE(<qrc file name>) is missing from main?

                int main(int argc, char *argv[])
                {
                    Q_INIT_RESOURCE(application);
                    QApplication app(argc, argv);
                   
                    MainWindow mainWin;
                    mainWin.show();
                    return app.exec();
                }
                P Offline
                P Offline
                PandaBoy
                wrote on last edited by
                #7

                @Rondog Thank you Randog, But it is not the problem.
                I think that this line might be unnecessary. But you brought this up, I have another question for this line.
                I have tried this even before I asked here. And when I add this line to my main.cpp, It told me " LNK2019 unresolved symbol".
                There is an example in Qt document using this line, and I had the exact same libraries included.

                Note:

                1. resource file has been included in .pro file.
                2. Q_INIT_RESOURCE(Ima); /* name of my qrc file is Ima.qrc now*/
                1 Reply Last reply
                0
                • M mcosta

                  Have you tried to use the resource icon in your real code??

                  To show an Item in a GraphicsView you have to set correctly GraphicsScene rect and the view

                  This code

                  ui->label->setPixmap(QPixmap(":/images/image.png"));
                  

                  works well for me using the resource

                  <RCC>
                      <qresource prefix="/images">
                          <file>image.png</file>
                      </qresource>
                  </RCC>
                  
                  P Offline
                  P Offline
                  PandaBoy
                  wrote on last edited by
                  #8

                  @mcosta Yes, I have. It doesn't work.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mcosta
                    wrote on last edited by
                    #9

                    @PandaBoy could you attach your project? (the whole project directory)

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rondog
                      wrote on last edited by
                      #10

                      The unresolved linker error will occour if the name inside the macro Q_INIT_RESOURCE cannot be found.

                      I was not aware that this wasn't required, I tried it and sure enough it worked without it. It is interesting that when you did add it you ended up with a link error which means it can't be loaded (I have always added this line inside of main and get this error when I don't set the name properly).

                      From a sample program I have the following (that does work):

                      // qrc file =====================
                      <!DOCTYPE RCC><RCC version="1.0">
                      	<qresource prefix="/gui">
                      		<file>autocapture.png</file>
                      	</qresource>
                      </RCC>
                      
                      // pro file =====================
                      // note:  I put all the resources in a folder called 'res'
                      RESOURCES += res/autocapture.qrc
                      
                      // usage example =====================
                      	d_start_button->setIcon(QIcon(":/gui/autocapture.png"));
                      

                      A couple of observations that might be helpful.

                      The resources line in the .PRO file ...

                      RESOURCES += \
                      Ima.qrc
                      

                      ... might be a problem. Why not this instead:

                      RESOURCES += Ima.qrc
                      

                      The .qrc file is missing the first XML identification line (i.e. '<!DOCTYPE RCC>' ). Maybe the file is not recognized as a resource file without this line?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mcosta
                        wrote on last edited by
                        #11

                        Hi,

                        <!DOCTYPE RCC> is not needed.

                        You have to add Q_INIT_RESOURCE when you want to load resources not embedded in your application.

                        I'm not sure to understand; the simple icon example works or not??
                        Your app with QGraphicsScene??

                        Once your problem is solved don't forget to:

                        • Mark the thread as SOLVED using the Topic Tool menu
                        • Vote up the answer(s) that helped you to solve the issue

                        You can embed images using (http://imgur.com/) or (http://postimage.org/)

                        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