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. GraphicsView doesn't display Pixmap
Qt 6.11 is out! See what's new in the release blog

GraphicsView doesn't display Pixmap

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 8.4k 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.
  • G Offline
    G Offline
    grizz
    wrote on last edited by
    #1

    Hi, I wrote program which has to display an image. I used Pixmap, GraphicsScene and GraphicsView, but it doesn't work... Could you tell me, what's wrong?
    QtCreator 2.4.0

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QPixmap *obraz = new QPixmap("C:\chrome.png");
    QGraphicsScene *scena = new QGraphicsScene(this);
    scena->addPixmap(*obraz);
    QGraphicsView *widok = new QGraphicsView(this);
    widok->setScene(scena);
    widok->setGeometry(50,50,270,270);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      Do you even display your widgets in MainWindow?

      If you placed it graphically, try calling it from namespace ui.

      Regards,
      Jake

      P.S.: Are you maybe from Slovenia?


      Code is poetry

      1 Reply Last reply
      0
      • G Offline
        G Offline
        grizz
        wrote on last edited by
        #3

        This is my main.cpp:

        @
        #include <QtGui/QApplication>
        #include "mainwindow.h"

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();

        return a.exec&#40;&#41;;
        

        }
        @

        When I add text to scene, GraphicsView displays it.

        No, I'm not from Slovenia. Greetings from Poland,

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          You've forgotten to set your QGraphicsView as the central widget.
          @
          MainWindow::MainWindow(QWidget *parent) : ...
          {
          ...
          setCentralWidget(widok);
          }
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            grizz
            wrote on last edited by
            #5

            It doesnt't help. Of course I included <QGraphicsView>.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jake007
              wrote on last edited by
              #6

              Declare your QGraphicsView in your class header file.
              Like this:

              @class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();

              QGraphicsView *view;
              QGraphicsScene *scene;

              private:
              Ui::MainWindow *ui;
              };@

              And your .cpp

              @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
              {
              ui->setupUi(this);

              view = new QGraphicsView;
              scene = new QGraphicsScene;

              view->setScene(scene);

              this->setCentralWidget(view);
              }@

              Similar words.
              Easily replaced ;).


              Code is poetry

              1 Reply Last reply
              0
              • G Offline
                G Offline
                grizz
                wrote on last edited by
                #7

                Still doesn't work :/.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jake007
                  wrote on last edited by
                  #8

                  Your image path is also incorrect.
                  If you are using backslash, you should put \, or you can also put one slash:
                  "C:/chrome.png"

                  @ ui->setupUi(this);

                  view = new QGraphicsView;
                  scene = new QGraphicsScene;
                  QPixmap *pix = new QPixmap("C:/pix.png");
                  scene->addPixmap(*pix);
                  view->setScene(scene);

                  this->setCentralWidget(view);
                  @


                  Code is poetry

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    grizz
                    wrote on last edited by
                    #9

                    Thank u :)

                    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