Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. problem with "painter->drawImage" in paint() from QGraphicsRectItem
Forum Updated to NodeBB v4.3 + New Features

problem with "painter->drawImage" in paint() from QGraphicsRectItem

Scheduled Pinned Locked Moved Solved Game Development
4 Posts 2 Posters 1.1k 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.
  • L Offline
    L Offline
    louloulou
    wrote on last edited by
    #1

    Hello,

    I try to draw on image on a QGraphicsScene and it doesn't work. I create an Object "MobileUnit::MobileUnit() : QGraphicsRectItem()", and this object should load an image but it doesn't work. I can draw a rectangle but not load an image and I don't understand why ?

    Thank you for your help.

    void MobileUnit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {

    //work draw rectangle
    /*QPen pen(Qt::black, 3);
    painter->setPen(pen);
    QRectF rect=QRectF(0,0,70,70);
    painter->drawRect(rect);*/
    
    //doesn't work
    painter->setPen(Qt::NoPen);
    QImage img;
    img.load("qrc:/../images/aircraft2.png");
    QRectF r;
    r.setRect(0,0,img.width(),img.height());
    painter->drawImage(r,img);
    

    }

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

      Hi and welcome to devnet,

      @louloulou said in problem with "painter->drawImage" in paint() from QGraphicsRectItem:

      img.load("qrc:/../images/aircraft2.png");

      That path looks suspicious.

      Please take a look at the Qt Resources system documentation to see how the path are handled.

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

      L 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        @louloulou said in problem with "painter->drawImage" in paint() from QGraphicsRectItem:

        img.load("qrc:/../images/aircraft2.png");

        That path looks suspicious.

        Please take a look at the Qt Resources system documentation to see how the path are handled.

        L Offline
        L Offline
        louloulou
        wrote on last edited by
        #3

        @SGaist said in problem with "painter->drawImage" in paint() from QGraphicsRectItem:

        looks suspici

        Thank you it works.

        I didn't think about it, but yes apparently my path is broken. I change with :

        img.load("qrc:/../images/aircraft2.png"); // doesn't work
        img.load("../images/aircraft2.png"); //work

        and now it works. I feel stupid. I have a file "resources.qrc" that have the path to my image, why I cannot give the path this way "qrc:/../images/aircraft2.png" and I have to give the path this way "../images/aircraft2.png" ?

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

          Because that path is wrong. You are referencing the images as if it was on your file system. That's the issue.

          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
          0

          • Login

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