Results 1 to 14 of 14

Thread: QGraphics View

  1. #1
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphics View

    Hi to all..I'm creating a application,object QMainWindow. I have created application.in UI form I have dragged QGraphicsView. I have created Graphic scene also. I'm posting the code.i'm able to get scene but it by deafult coming at topleft corner. other than coming inside QGraphicsView.suggest me,where I might have gone wrong.
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7. #include <QBrush>
    8.  
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow {
    15. Q_OBJECT
    16. public:
    17. MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. protected:
    21. void changeEvent(QEvent *e);
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. QGraphicsView *view = new QGraphicsView(this);
    11. scene->setBackgroundBrush(Qt::red);
    12. view->setScene(scene);
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::changeEvent(QEvent *e)
    21. {
    22. QMainWindow::changeEvent(e);
    23. switch (e->type()) {
    24. case QEvent::LanguageChange:
    25. ui->retranslateUi(this);
    26. break;
    27. default:
    28. break;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    I'm insering the image also.image.png

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphics View

    You didn't put the view in your layout so it pops up at (0,0) coordinates of its parent. Usually you'd create the view in the UI file and attach the scene to it in CPP code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:


  4. #3
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Qt Code:
    1. ui->setupUi(this);
    2.  
    3. view=new QGraphicsView(this);
    4. view=ui->graphicsView;
    5.  
    6. scene=new QGraphicsScene(view);
    7. view->setScene(scene);
    8. view->setFixedSize(500,500);
    9.  
    10. QPixmap pix("/:pin30_red.png");
    11. scene->addPixmap(pix);
    12. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    I tried it fix it. Still same image is coming

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Remove the line where you create the QGraphicsView

    And make sure your Designer UI is properly layouted and remove the setFixedSize() call

    Cheers,
    _

  6. #5
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Thanks for the reply. I tried as per your suggestion but still same image exists in the output.im posting full code in zip format.plz look into it.I request ammend if its wrong.


    I'm finding difficult to resolve this So I have posted full code in zip format.
    Attached Files Attached Files

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    You had incorrectly used the qrc image path...
    Also the view is unnecessary as you have ui->graphicsView.

    Use the code below in ctor and delete uses of view variable -

    Qt Code:
    1. {
    2. ui->setupUi(this);
    3.  
    4. scene=new QGraphicsScene(ui->graphicsView);
    5. QPixmap pix(":/pin30_red.png");
    6. scene->addPixmap(pix);
    7. ui->graphicsView->setScene(scene);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to aamer4yu for this useful post:

    Vivek1982 (18th December 2013)

  9. #7
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    thanks alot for reply. I got the output.. Now working on to move the pixmap on known co-ordinates of graphics View,by using Itemat or pos methods.(GraphicsPixmapItem) let me check.. thanks alot
    Last edited by Vivek1982; 18th December 2013 at 06:38.

  10. #8
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    hi.. Im trying to move the image (pixmap loaded in graphisscene). To do this I have defined size of Graphicsscene intially.then I'm adding the pixmap via scene.addPixmap(image1). To get the pointer i'm calling graphicsItem. QGraphicsItem item=scene.addpixmap(image1).Then atlast Im setPixmap on the GraphicsView.In this correct? plz suggest. Pixmap will be moving on the scene on Qtimer by setpixmap on scene.

  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    You don't have to call anything on the view. Once you've added the pixmap by creating a pixmap item (which you do when you call scene.addPixmap()) then it will become visible in all views working with that scene.

    If you want to move the pixmap, you set a new position on its graphicsitem

    Cheers,
    _

  12. #10
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    I did the same, I have added QGraphicsItem also tried QGraphicsPixmap also to move. While moving the images(setPos). I'm getting twice images and If I increase the pos. Images are moving far on the view rather than same created image should move.
    Qt Code:
    1. scene=new QGraphicsScene(0,0,500,300);
    2. scene=new QGraphicsScene(ui->graphicsView);
    3. QPixmap pix(":/pin30_red.png");
    4. scene->addPixmap(pix);
    5. QGraphicsItem *pix1=scene->addPixmap(pix);
    6. //pix1->setPixmap(pix);
    7. pix1->setPos(350,100);
    8. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphics View

    You are calling addPixmap twice so you get two items. Seems pretty obvious...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #12
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Ya.. but If i try to just give QGraphicsItem *pix1= pix; pass the pointer. Im getting "error: cannot convert 'QPixmap' to 'QGraphicsItem*' in initialization" or if i try to comment on scene.addPixmap(pix). by default image will appear on horizontal bottom left.whether,I need to type cast in QGraphicsItem line?

  15. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Quote Originally Posted by Vivek1982 View Post
    Ya.. but If i try to just give QGraphicsItem *pix1= pix; pass the pointer. Im getting "error: cannot convert 'QPixmap' to 'QGraphicsItem*' in initialization"
    Well, if you look at the class declaration of QPixmap you will see that it does not inherit from QGraphicsItem.

    But the QGraphicsPixmapItem that you get from QGraphicsScene::addPixmap() does.

    Btw, your code snippet also creates an additional QGraphicsScene and then throws loses the pointer to it.

    Cheers,
    _

  16. The following user says thank you to anda_skoa for this useful post:

    Vivek1982 (20th December 2013)

  17. #14
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Finally I got it.. The error was setting of images on the co-ordinates and in UI form also..

    Thanks alot for all.. But this forum has helped alot and thought me many things about Qt programming.
    Last edited by Vivek1982; 20th December 2013 at 12:09.

Similar Threads

  1. Items in QGraphics View not starting from the top.
    By penny in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 13:10
  2. Creating Qgraphics View code
    By amagdy.ibrahim in forum Qt Programming
    Replies: 9
    Last Post: 15th June 2008, 15:43
  3. Qgraphics View
    By amagdy.ibrahim in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2008, 10:10
  4. QGraphics view update : Help needed
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2007, 12:54
  5. QGraphics view problem
    By kiranraj in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 21:28

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
  •  
Qt is a trademark of The Qt Company.