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. QGraphicsPixmapItem not showing when added to scene
QtWS25 Last Chance

QGraphicsPixmapItem not showing when added to scene

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 801 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.
  • S Offline
    S Offline
    SlappyDanger
    wrote on last edited by SlappyDanger
    #1

    My understanding is as follows:

    I create a QGraphicsView, QGraphicsScene, and QPixmap.
    The resource file allows me to customize the string for the file location. In this case, it's set to ":/piece/b_bishop.png" according to the documentation for resource files.
    I create a QGraphicsPixmapItem using the QPixmap I created earlier.
    I set the QGraphicsPixmapItem to the QGraphicsScene.
    I set the scene to the view.
    I show the view, but nothing happens.
    Just to make sure, I changed the background color.

    Below, I have my code.

    #include <QApplication>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsPixmapItem>
    #include <QPixmap>
    int main(int argc, char **argv)
    {
        QApplication app (argc, argv);
        QGraphicsView view;
        QGraphicsScene scene;
        scene.setBackgroundBrush(Qt::green);
        
        QPixmap map (":/piece/b_bishop.png");
        QGraphicsPixmapItem bishop(map);
        scene.addItem(&bishop);
    
        view.setScene(&scene);
        view.show();
    
        return app.exec();
    }
    
    <RCC>
        <qresource prefix="/piece">
            <file>chesspieces/b_bishop.png</file>
        </qresource>
    </RCC>
    

    The image, as shown in the QT Creator:
    1174bd28-43cb-422d-883b-8168344e3c78-image.png

    I've currently tried: adjusting the syntax in multiple ways. By everything I have seen, I have done this correctly. I must be missing something. Thanks for your help.

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

      Shouldn't it be

      /piece/hesspieces/b_bishop.png

      ?
      Please check with QFile::exists() if your path is really correct.

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

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Shouldn't it be

        /piece/hesspieces/b_bishop.png

        ?
        Please check with QFile::exists() if your path is really correct.

        S Offline
        S Offline
        SlappyDanger
        wrote on last edited by SlappyDanger
        #3

        @Christian-Ehrlicher

        I took your advice, and tried these:

        QFile file1(":/piece/chesspieces/b_bishop.png");
        QFile file2(":/piece/b_bishop.png");
        QFile file3(":/chesspieces/b_bishop.png");
        
        if(file1.exists())
            qDebug() << "file 1 exists";
        else
            qDebug() << "file 1 does not exist";
        if(file2.exists())
            qDebug() << "file 2 exists";
        else
            qDebug() << "file 2 does not exist";
        if(file3.exists())
            qDebug() << "file 3 exists";
        else
            qDebug() << "file 3 does not exist";
        

        None of them exist, assuming I wrote that code correctly to check lol.

        Here is my file location:
        36dd1713-b1d5-4d69-9303-0819a4ff4c43-image.png
        0d64aafd-5070-4074-bb9c-564336f892d8-image.png

        Ignore the other files, i'm just using main to test this.

        I'm creating the resource file with the QT Creator, and adding files to it the same way.

        f6a5be2f-d5f6-4baa-b179-71cdc3434c8e-image.png

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

          Did you instantiate a Q(Core)Application before your test?

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

          S 3 Replies Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            Did you instantiate a Q(Core)Application before your test?

            S Offline
            S Offline
            SlappyDanger
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            Honestly I have no idea what that is, so probably not.

            I just tried.

            "QWidget: Cannot create a QWidget without QApplication"

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Did you instantiate a Q(Core)Application before your test?

              S Offline
              S Offline
              SlappyDanger
              wrote on last edited by SlappyDanger
              #6

              @Christian-Ehrlicher

              absolute path works.

              5c8d8acb-6a29-4260-9344-dfe2c25deff2-image.png

              I would much prefer not having to use absolute paths, though

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Maybe a silly question but did you list your .qrc file in your .pro file ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  Did you instantiate a Q(Core)Application before your test?

                  S Offline
                  S Offline
                  SlappyDanger
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher

                  I'm real dumb, I figured it out. Your solution worked, just had to keep digging. I had mixed up file names apparently.

                  1 Reply Last reply
                  1

                  • Login

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