Results 1 to 4 of 4

Thread: my QGraphicView problem

  1. #1
    Join Date
    Jan 2008
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default my QGraphicView problem


    Hi,
    I used the code below normally in QApplication

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QPixmap pix;
    5. //QImage image;
    6. QString currentFile = QFileDialog::getOpenFileName(0,"Open File", QDir::currentPath());
    7. QImage image(currentFile);
    8. //pix.load(":/Resources/open.png");
    9. //pix.load(currentFile);
    10.  
    11. scene.addPixmap(QPixmap::fromImage(image));
    12. gr= new QGraphicsView();
    13. gr->setScene(&scene);
    14. //setCentralWidget(gr);
    15. gr->show();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    and this works. I can see the image loaded.

    But when I used the same code in QMainWindow
    such as

    Qt Code:
    1. deneme::deneme(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. QPixmap pix;
    6. //QImage image;
    7. QString currentFile = QFileDialog::getOpenFileName(0,"Open File", QDir::currentPath());
    8. QImage image(currentFile);
    9.  
    10. scene.addPixmap(QPixmap::fromImage(image));
    11. gr= new QGraphicsView();
    12. //gr->setBackgroundBrush(QImage(currentFile));
    13. //gr->setForegroundBrush(QImage(currentFile));
    14. gr->setScene(&scene);
    15. //setCentralWidget(gr);
    16. gr->show();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 
    this does not work. And I can only see white graphics widget when I loaded the image.

    How can I solve this problem. Thanks in advance
    regards
    Last edited by jpn; 21st July 2008 at 16:58. Reason: changed [b] to [code] tags

  2. #2
    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: my QGraphicView problem

    try this -
    Qt Code:
    1. deneme::deneme(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. QPixmap pix;
    6. QString currentFile = QFileDialog::getOpenFileName(0,"Open File", QDir::currentPath());
    7. QImage image(currentFile);
    8.  
    9. scene->addPixmap(QPixmap::fromImage(image));
    10. gr= new QGraphicsView();
    11. gr->setScene(scene);
    12. setCentralWidget(gr);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Does this work for you ?? You are creating scene as local object which will be destroyed after the constructor code .

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

    irmakci (19th July 2008)

  4. #3
    Join Date
    Jan 2008
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: my QGraphicView problem

    Yes, when scene was made as private variable in class, then it worked. Thanks. But Why is it destroyed I can't understand? Because graphics view widget is not destroyed. And When I also made this simple code in button click, same thing did not work although it is not in the constructor.
    Anyway It must be global (w.r.t. class) variable in the class.

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: my QGraphicView problem

    If you declare a variable in a method, it will be destroyed when it goes out of scope (i.e. when the method is left).

    Your scene is local to the constructor's body. It is destroyed after the constructor is completed!

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  4. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.