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. Set an image for QGraphicsRectItem

Set an image for QGraphicsRectItem

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 2.4k 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.
  • W Offline
    W Offline
    westernCiv
    wrote on last edited by
    #1

    I want to set a 24*24px PNG image for a QGraphicsRectItem on a QGraphicsScene/View
    I tried the following:

        scene = new QGraphicsScene(this);
        view= new QGraphicsView(this);
        view->setScene(scene);
        view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      
        setCentralWidget(view);
        
        QBrush q;
        q.setTextureImage(QImage("resources/list.png"));
        
        QGraphicsRectItem* d  = new QGraphicsRectItem;
        d->setRect(30,30,24,24);
        d->setBrush(q);
        scene->addItem(d);
    

    But it doesn't look as expected: alt text
    The image is not centered in the RectItem and also appears 2x partially...
    Also I'm not sure if setTextureImage() is a good way to set an image

    Ni.SumiN 1 Reply Last reply
    0
    • W westernCiv

      I want to set a 24*24px PNG image for a QGraphicsRectItem on a QGraphicsScene/View
      I tried the following:

          scene = new QGraphicsScene(this);
          view= new QGraphicsView(this);
          view->setScene(scene);
          view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        
          setCentralWidget(view);
          
          QBrush q;
          q.setTextureImage(QImage("resources/list.png"));
          
          QGraphicsRectItem* d  = new QGraphicsRectItem;
          d->setRect(30,30,24,24);
          d->setBrush(q);
          scene->addItem(d);
      

      But it doesn't look as expected: alt text
      The image is not centered in the RectItem and also appears 2x partially...
      Also I'm not sure if setTextureImage() is a good way to set an image

      Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by Ni.Sumi
      #2

      @westernCiv

      If QGraphicsRectItem is not matter,

      simply you can use this way.

      MainWindow::MainWindow(QWidget* parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow) {
        ui->setupUi(this);
      
        QGraphicsScene* scene = new QGraphicsScene(this);
        QPixmap pixmapItem("/your/image/path/imageNAme.png");
        QPixmap pixmapItems = pixmapItem.scaled(QSize(24,24),  Qt::KeepAspectRatio);
        QGraphicsView* view = new QGraphicsView(this);
      
        scene->addPixmap(pixmapItems);
        view->setScene(scene);
        setCentralWidget(view);
      }
      

      Edit:
      This is very simple and rough solution. there are many better ways also.

      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