Results 1 to 4 of 4

Thread: I imitated Image Viewer example but...

  1. #1
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default I imitated Image Viewer example but...

    I exactly copied all the stuff from the Image Viewer example of Qt, except for the constructor. I modified it the constructor so it would auto load an image coming from another mainwindow that calls

    Qt Code:
    1. ImageViewer imageViewer(&image);
    To copy to clipboard, switch view to plain text mode 


    Now I am having trouble with the class because it closes/quits... and I am sure that that class's method show() ( opens for x milliseconds then closes).

    I've read this
    http://www.qtcentre.org/forum/f-qt-p...t=QApplication
    and it says two QApplication can't run at the same time. So how am I going to let the ImageViewer window stay (not instantly quit on show()). ? Because as of now I can't manually close it.. it closes on itself?

    Here goes the modified constructor of ImageViewer.

    Qt Code:
    1. ImageViewer::ImageViewer(const QImage *image) {
    2. imageLabel = new QLabel;
    3. imageLabel->setBackgroundRole(QPalette::Base);
    4. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    5. imageLabel->setScaledContents(true);
    6.  
    7. scrollArea = new QScrollArea;
    8. scrollArea->setBackgroundRole(QPalette::Dark);
    9. scrollArea->setWidget(imageLabel);
    10. setCentralWidget(scrollArea);
    11.  
    12. createActions();
    13. createMenus();
    14.  
    15. setWindowTitle(tr("Image Viewer"));
    16. resize(500, 400);
    17.  
    18. // this is where the opening of the passed const image starts
    19.  
    20. if (image->isNull() || image == 0) {
    21. QMessageBox::information(this, tr("Image Viewer"),
    22. tr("Cannot load the image."));
    23. return;
    24. }
    25.  
    26. QPixmap pixmap = QPixmap::fromImage(*image);
    27.  
    28. imageLabel->setPixmap(pixmap);
    29. scaleFactor = 1.0;
    30.  
    31. printAct->setEnabled(true);
    32. fitToWindowAct->setEnabled(true);
    33. updateActions();
    34.  
    35. if (!fitToWindowAct->isChecked())
    36. imageLabel->adjustSize();
    37.  
    38. // suddenly closes ! ! !
    39. show();
    40. }
    To copy to clipboard, switch view to plain text mode 

    I've also read http://www.qtcentre.org/forum/f-qt-p...ght=maptoscene so. how do I NOT destroy the ImageViewer after calling its constructor?
    Last edited by sincnarf; 14th October 2007 at 15:57. Reason: confusing
    Image Analysis Development Framework Using Qt (IADFUQ)

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I imitated Image Viewer example but...

    Oh, now I get it.
    You were creating the image viewer on the stack:
    Qt Code:
    1. ImageViewer imageViewer(&image);
    To copy to clipboard, switch view to plain text mode 

    Create it on the heap, so it won't be destroyed as soon as the scope in which you created it ends:
    Qt Code:
    1. ImageViewer *imageViewer = new ImageViewer(&image);
    To copy to clipboard, switch view to plain text mode 


    Or make it a class member.

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

    sincnarf (14th October 2007)

  4. #3
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I imitated Image Viewer example but...

    that was fast.. So basic... I'm really sorry. Getting pressured here. Thanks again
    Image Analysis Development Framework Using Qt (IADFUQ)

  5. #4
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I imitated Image Viewer example but...

    This may come over as nitpicking but in my opinion you are doing too much logic in your constructor... from a software engineering standpoint. I *try* to write my constructors such that they only initialize member variables. If I need more than that I will usually write an Init() method. Your case is a little different... modulizing it might be a good start.

Similar Threads

  1. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  2. Requirement for versatile image viewer
    By deekayt in forum General Discussion
    Replies: 1
    Last Post: 27th October 2006, 14:28
  3. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  4. Image Viewer
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2006, 13:29
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.