Results 1 to 9 of 9

Thread: Display an image in a QGraphicsview

  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Display an image in a QGraphicsview

    Hello,

    I'm developing an application that displays a default image in a QGraphicsiew (named viewer) when it's executed and displays other image when a button it's clicked but my code doesn't work:

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. catiz w;
    5. w.showFullScreen();
    6.  
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    catiz.cpp
    Qt Code:
    1. ...
    2. catiz::catiz(QWidget *parent) : QWidget(parent),ui(new Ui::catiz)
    3. {
    4. ui->setupUi(this);
    5.  
    6. QPixmap pixmap(":/init.jpg");
    7. scene.addPixmap(pixmap);
    8. ui->viewer->setScene(&scene);
    9. }
    10. ...
    To copy to clipboard, switch view to plain text mode 

    What could be wrong? I'm using QtCreator.

    Best regards.

  2. #2
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display an image in a QGraphicsview

    Hi emilio,

    try to use a graphic view widget on your frame. and add the following code in your app:
    Qt Code:
    1. ...
    2. catiz::catiz(QWidget *parent) : QWidget(parent),ui(new Ui::catiz)
    3. {
    4. ui->setupUi(this);
    5.  
    6. QPixmap pixmap(":/init.jpg");
    7. scene.addPixmap(pixmap);
    8. //ui->viewer->setScene(&scene);
    9.  
    10. // add here
    11. ui->graphicsView->setScene(scene);
    12. ui->graphicsView->show();
    13. }
    14. ...
    To copy to clipboard, switch view to plain text mode 

    hope it works.

  3. #3
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display an image in a QGraphicsview

    Hello cooper,

    Qt Code:
    1. ui->viewer
    To copy to clipboard, switch view to plain text mode 
    it's a QGraphicsview widget, thanks anyway.

  4. #4
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display an image in a QGraphicsview

    does it work after you add:
    Qt Code:
    1. ui->viewer->show();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display an image in a QGraphicsview

    I tried but it still doesn't work.

  6. #6
    Join Date
    Mar 2011
    Location
    Ankara , Turkey
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Display an image in a QGraphicsview

    Hello, I am new to Qt an C++. I had the same problem. After looking for the forum I managed to display an image on an graphicsView which is on QWidget user form
    I created a new project with QtGui App. put a push button and a graphicsView widget. when I push the button. it creates a scene object, a pixmap and attach it to the viewer
    Below is the code
    Qt Code:
    1. #include <QtGui>
    2. #include "resimoku.h"
    3. #include "ui_resimoku.h"
    4.  
    5. ResimOku::ResimOku(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::ResimOku)
    8. {
    9. ui->setupUi(this);
    10.  
    11. }
    12.  
    13. ResimOku::~ResimOku()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void ResimOku::on_pushButton_clicked()
    19. {
    20. QPixmap pixmap("C:/resim.jpg");
    21. scene->addPixmap(pixmap);
    22. ui->view->setScene(scene);
    23. ui->view->show();
    24. }
    To copy to clipboard, switch view to plain text mode 

    The decleration of the scene object is solved the problem I think. It is a matter of C++ knowledge which i have a little.
    Thanks for forum users.

  7. #7
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Display an image in a QGraphicsview

    Try to create the QGraphicsScene dinamically... because static variables are deleted when you are out of the scope. If it's dinamic going out of the scope only deletes the pointer.

  8. #8
    Join Date
    Mar 2011
    Location
    Ankara , Turkey
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Display an image in a QGraphicsview

    I made a change to scene object decleration in order to use it out of pushbuttons scope. I declare it as a private member of ResimOku object. create it in ResimOku decleration and use it in pushbutton events. With this the scope of scene object is ResimOku object scope.
    Header file
    Qt Code:
    1. #ifndef RESIMOKU_H
    2. #define RESIMOKU_H
    3. #include <QtGui>
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class ResimOku;
    8. }
    9.  
    10. class ResimOku : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit ResimOku(QWidget *parent = 0);
    16.  
    17. ~ResimOku();
    18.  
    19. private:
    20. Ui::ResimOku *ui;
    21. private slots:
    22. void on_pushButton_2_clicked();
    23. void on_pushButton_clicked();
    24. };
    25.  
    26. #endif // RESIMOKU_H
    To copy to clipboard, switch view to plain text mode 

    CPP FILE

    Qt Code:
    1. #include "resimoku.h"
    2. #include "ui_resimoku.h"
    3.  
    4.  
    5. ResimOku::ResimOku(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::ResimOku)
    8. {
    9. ui->setupUi(this);
    10. scene = new QGraphicsScene;
    11. }
    12.  
    13. ResimOku::~ResimOku()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void ResimOku::on_pushButton_clicked()
    19. {
    20. QPixmap pixmap("C:/resim.jpg");
    21. scene->addPixmap(pixmap);
    22. ui->view->setScene(scene);
    23. ui->view->show();
    24. }
    25.  
    26. void ResimOku::on_pushButton_2_clicked()
    27. {
    28. QPixmap pixmap("C:/resim2.jpg");
    29. scene->addPixmap(pixmap);
    30. ui->view->setScene(scene);
    31. ui->view->show();
    32. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Hello

    Hello QtCentre......

    How can we display the image on GraphicsView in widget.ui using the above code through pushbutton signals???
    I had used QWidget and unable to succeed in doing so.
    plzz help me in achieving the same

Similar Threads

  1. Display cursor position in QGraphicsView
    By dbrmik in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2009, 15:06
  2. image on qgraphicsview
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2009, 12:34
  3. MDI display Item in QGraphicsView
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 00:11
  4. Display image
    By Pesho in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 00:21
  5. Display Images in QGraphicsView
    By robertaandrews in forum Qt Tools
    Replies: 4
    Last Post: 15th May 2007, 18:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.