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. [Solved] QTgraphics using Designer
Forum Updated to NodeBB v4.3 + New Features

[Solved] QTgraphics using Designer

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.2k 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.
  • P Offline
    P Offline
    Peregrinus
    wrote on last edited by
    #1

    I wold like to display a Jpeg image on a QGraphicsView objet I have created with the designer.
    I followed several examples I found here but the image window is always white.
    This is the method I call from main after the main window is displayed.

    @void ImaferensMainWindow::ShowIma()
    {
    QGraphicsView qtv = ui->graphicsView;

    qtv.setRenderHint(QPainter::Antialiasing);
    QPixmap img = QPixmap("C:\\ProgettiPC\\QT\\imaferens\\aereo.jpg");
    qtv.setBackgroundBrush(img);
    qtv.setCacheMode(QGraphicsView::CacheBackground);
    qtv.setAlignment(Qt::AlignCenter);
    qtv.setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
    qtv.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Map Test"));
    qtv.resize(720, 576);
    qtv.show();
    

    }@

    Thank you.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      issam
      wrote on last edited by
      #2

      Well I didn't work with the Designer before but I worked with GraphicsView framework !
      So I think that you have to use QGraphicsScene ?

      @
      scene = new QGraphicsScene(qobject_parent);
      qtv->setScene(scene);
      @

      Without a scene the window is always white !
      To display the image you have firstly add it to the scene using :

      @
      scene->addPixmap(image);
      @

      The scene is the model (data) and the view will display the items of the scene ;)

      http://www.iissam.com/

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Peregrinus
        wrote on last edited by
        #3

        Thank you Issam. I tried a lot but it does not work.
        You say the code shold be something like this:

        @void ImaferensMainWindow::ShowIma()
        {

        QGraphicsScene scene = new QGraphicsScene(this);
        QGraphicsView *qtv =  ui->graphicsView;
        
        QPixmap img = QPixmap("C:\\ProgettiPC\\QT\\imaferens\\aereo.jpg");
        
        scene.addPixmap(img);
        
        qtv->setScene(&scene);
        
        qtv->setRenderHint(QPainter::Antialiasing);
        qtv->setBackgroundBrush(img);
        qtv->setCacheMode(QGraphicsView::CacheBackground);
        qtv->setAlignment(Qt::AlignCenter);
        qtv->setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
        qtv->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Test"));
        qtv->resize(720, 576);
        qtv->show();
        

        }
        @

        It does not work. It seems that the view is not connected to the scene, the image pointer is not null, but the view area is always deep white.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          You can make the following changes in your code. This works for me

          @QGraphicsScene *scene = new QGraphicsScene();
          QGraphicsView *qtv = ui->graphicsView;

          QPixmap img = QPixmap("C:/ProgettiPC/QT/imaferens/aereo.jpg"); // you can use '/' instead of '\' does the same.

          scene->addPixmap(img);
          qtv->setScene(scene);
          qtv->setRenderHint(QPainter::Antialiasing);
          qtv->setBackgroundBrush(img);
          qtv->setCacheMode(QGraphicsView::CacheBackground);
          qtv->setAlignment(Qt::AlignCenter);
          qtv->setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
          qtv->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Test"));
          qtv->resize(720, 576);
          qtv->show();@

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

            It works, I understand my error.
            Thank you.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

              Great, Kindly edit your first post and prepend "Solved" to it :)

              Happy Coding.

              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