GraphicsView doesn't display Pixmap



  • 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;
    }
    @



  • 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?



  • 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,



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



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



  • 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 ;).



  • Still doesn't work :/.



  • 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);
    @



  • Thank u :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.