Results 1 to 12 of 12

Thread: Display continous images on the screen

  1. #1
    Join Date
    Dec 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Display continous images on the screen

    Hi, I have many images need to modify. I want to modify and display one by one (display each image after modifying it). Now I just can do this work for 1 image, how do I do it for many images? Here is the code that do the work to modify and display 1 image:
    Qt Code:
    1. RF2Img::RF2Img()
    2. {
    3. imageLabel = new QLabel;
    4. imageLabel->setBackgroundRole(QPalette::Base);
    5. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    6. imageLabel->setScaledContents(true);
    7.  
    8. createActions();
    9. createMenus();
    10.  
    11. processRFImg("1.bmp"); // This will process 1.bmp
    12. loadRFImg("1.bmp"); // This will display 1.bmp
    13.  
    14. setWindowTitle(tr("RF2IMG"));
    15. resize(800, 800);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. RF2Img rf2img;
    5. rf2img.show();
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    So then, How do i do it for many images: 2.bmp, 3.bmp...? Thanks !

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display continous images on the screen

    Are you familiar with the 'for' loop concept?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display continous images on the screen

    But when I try to use this code:
    Qt Code:
    1. for(int i = 0; i < 10; i++) {
    2. QString str;
    3. str.append(QString("%1").arg(i+1));
    4. str.append(".bmp");
    5. processRFImg(str);
    6. loadRFImg(str);
    7. }
    To copy to clipboard, switch view to plain text mode 
    It just display the first imge, how do I show all the image one by one while modifying them?
    Last edited by tuent; 22nd December 2010 at 10:09.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Display continous images on the screen

    Have a look at http://doc.trolltech.com/qq/qq27-responsive-guis.html. That will solve your problem!

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display continous images on the screen

    It just display the last imge, how do I show all the image one by one while modifying them?
    I though more along the lines of:
    Qt Code:
    1. MyClass::processImage(const QString &strFileName)
    2. {
    3. imageLabel = new QLabel(this);
    4. imageLabel->setBackgroundRole(QPalette::Base);
    5. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    6. imageLabel->setScaledContents(true);
    7.  
    8. createActions();
    9. createMenus();
    10.  
    11. processRFImg(strFileName); // This will process 1.bmp
    12. loadRFImg(strFileName); // This will display 1.bmp
    13.  
    14. setWindowTitle(tr("RF2IMG"));
    15. resize(800, 800);
    16. }
    17.  
    18. ...
    19. //and then
    20. QStringList slstFileNames; //needs to be filled with the file names of your images
    21. for(int i = 0; i < slstFileNames.size(); i++) {
    22. pMyClass->processImage(slstFileNames.at(i));
    23. }
    To copy to clipboard, switch view to plain text mode 

    And note what Lykurg said as well.

    NOTE:
    these are suggestions, they are not fully working application.
    YOU have to make this work, but that would be the general way to about it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Dec 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display continous images on the screen

    Thanks for your comment lykurg and high_flyer but your article http://doc.trolltech.com/qq/qq27-responsive-guis.html is quite difficult for me. Could you show me some more detail advises for my problem.
    I have a class RF2Img derived QMainWindow
    Qt Code:
    1. class RF2Img : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. RF2Img();
    7. public slots:
    8. // Load RF image
    9. void loadRFImg(QString fileName); // Attach the image to image label
    10. void processImg(QString fileName); // Process image
    11. private:
    12. void createActions();
    13.  
    14. void updateActions();
    15.  
    16. QLabel *imageLabel;
    17. };
    To copy to clipboard, switch view to plain text mode 
    in main.cpp I have
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. RF2Img rf2img;
    5. rf2img.show();
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    The image will be displayed by imageLabel, because the processImage function is quite slow so the image must be displayed one by one, how do I use multithread or some other solutions to display the image one by one using my current function? Thanks for your advices .

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Display continous images on the screen

    Do the image scaling in a thread or even easier use QtConcurrent for that. There is also an example in the docs: QtConcurrent Image Scaling Example. Or simply call
    Qt Code:
    1. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    after setting the image to the label. But the last will still block your gui.

  8. #8
    Join Date
    Dec 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display continous images on the screen

    I solved my problems! Thanks for your help !

  9. #9
    Join Date
    Dec 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display continous images on the screen

    Hi all, I'm using the QtConcurrent example http://doc.trolltech.org/4.7-snapsho...aling-cpp.html for my project. My process image algorithm will replace scale function.
    Qt Code:
    1. #include "imagescaling.h"
    2. #include "math.h"
    3.  
    4. #ifndef QT_NO_CONCURRENT
    5.  
    6. const int imageSize = 100;
    7.  
    8. QImage scale(const QString &imageFileName)
    9. {
    10. QImage image(imageFileName);
    11. return image.scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    12. }
    13.  
    14. Images::Images(QWidget *parent)
    15. : QWidget(parent)
    16. {
    17. setWindowTitle(tr("Image loading and scaling example"));
    18. resize(800, 600);
    19.  
    20. imageScaling = new QFutureWatcher<QImage>(this);
    21. connect(imageScaling, SIGNAL(resultReadyAt(int)), SLOT(showImage(int)));
    22. connect(imageScaling, SIGNAL(finished()), SLOT(finished()));
    23.  
    24. openButton = new QPushButton(tr("Open Images"));
    25. connect(openButton, SIGNAL(clicked()), SLOT(open()));
    26.  
    27. cancelButton = new QPushButton(tr("Cancel"));
    28. cancelButton->setEnabled(false);
    29. connect(cancelButton, SIGNAL(clicked()), imageScaling, SLOT(cancel()));
    30.  
    31. pauseButton = new QPushButton(tr("Pause/Resume"));
    32. pauseButton->setEnabled(false);
    33. connect(pauseButton, SIGNAL(clicked()), imageScaling, SLOT(togglePaused()));
    34.  
    35. QHBoxLayout *buttonLayout = new QHBoxLayout();
    36. buttonLayout->addWidget(openButton);
    37. buttonLayout->addWidget(cancelButton);
    38. buttonLayout->addWidget(pauseButton);
    39. buttonLayout->addStretch();
    40.  
    41. imagesLayout = new QGridLayout();
    42.  
    43. mainLayout = new QVBoxLayout();
    44. mainLayout->addLayout(buttonLayout);
    45. mainLayout->addLayout(imagesLayout);
    46. mainLayout->addStretch();
    47. setLayout(mainLayout);
    48. }
    49.  
    50. Images::~Images()
    51. {
    52. imageScaling->cancel();
    53. imageScaling->waitForFinished();
    54. }
    55.  
    56. void Images::open()
    57. {
    58. // Cancel and wait if we are already loading images.
    59. if (imageScaling->isRunning()) {
    60. imageScaling->cancel();
    61. imageScaling->waitForFinished();
    62. }
    63.  
    64. // Show a file open dialog at QDesktopServices::PicturesLocation.
    65. QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Images"),
    66. QDesktopServices::storageLocation(QDesktopServices::PicturesLocation),
    67. "*.jpg *.png");
    68.  
    69. if (files.count() == 0)
    70. return;
    71.  
    72. // Do a simple layout.
    73. qDeleteAll(labels);
    74. labels.clear();
    75.  
    76. int dim = sqrt(qreal(files.count())) + 1;
    77. for (int i = 0; i < dim; ++i) {
    78. for (int j = 0; j < dim; ++j) {
    79. QLabel *imageLabel = new QLabel;
    80. imageLabel->setFixedSize(imageSize,imageSize);
    81. imagesLayout->addWidget(imageLabel,i,j);
    82. labels.append(imageLabel);
    83. }
    84. }
    85.  
    86. // Use mapped to run the thread safe scale function on the files.
    87. imageScaling->setFuture(QtConcurrent::mapped(files, scale));
    88.  
    89. openButton->setEnabled(false);
    90. cancelButton->setEnabled(true);
    91. pauseButton->setEnabled(true);
    92. }
    93.  
    94. void Images::showImage(int num)
    95. {
    96. labels[num]->setPixmap(QPixmap::fromImage(imageScaling->resultAt(num)));
    97. }
    98.  
    99. void Images::finished()
    100. {
    101. openButton->setEnabled(true);
    102. cancelButton->setEnabled(false);
    103. pauseButton->setEnabled(false);
    104. }
    105.  
    106. #endif // QT_NO_CONCURRENT
    To copy to clipboard, switch view to plain text mode 
    But my images are too big (2MB/image) and when the program runs the memory loaded too much because it loads all the images to memory. How do I delete the image memory after display it.
    I tried this way but it can't release the displayed image data:
    I try delete the displayed image data in showImage(int):
    Qt Code:
    1. void Images::showImage(int num)
    2. {
    3. labels[num-1].clear();
    4. labels[num]->setPixmap(QPixmap::fromImage(imageScaling->resultAt(num)));
    5. }
    To copy to clipboard, switch view to plain text mode 
    How do i optimize the memory in my program? Thanks for your help !

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display continous images on the screen

    How do I delete the image memory after display it.
    The question is when?
    If you delete it just after diplaying it, it will just be there for a short moment and be gone.

    You need to specify how your program is supposed to behave in regard to showing the images.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Dec 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display continous images on the screen

    Quote Originally Posted by high_flyer View Post
    The question is when?
    If you delete it just after diplaying it, it will just be there for a short moment and be gone.

    You need to specify how your program is supposed to behave in regard to showing the images.
    I want to delete the previous image when the current image is displayed. My program is just like the Qtconcurrent example. (The function Scale() take quite long time to run each image)

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display continous images on the screen

    You have a problem with your design.
    I don't understand why do you need concurrent execution here, if you are showing one image at a time?
    That is why you have the problem - you allocate only one image per scan() but your scans run in parallel, resulting in multiple images allocated.
    If for what ever reason you have to do things this way, one way would be to manage the thread pool, so that only one a certain maximum number of threads will be started, and, you might want to allocate on the heap and not on the stack, since stack size is very limited.
    You will have much more freedom on the heap - but you will have to watch your limits there too, otherwise your system might suffocate, specially if its embedded.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. How to display DDS images?
    By jamsession in forum Qt Programming
    Replies: 5
    Last Post: 12th June 2013, 22:18
  2. problem in drwing several images on screen
    By d@nyal in forum Newbie
    Replies: 7
    Last Post: 1st May 2010, 01:32
  3. How to Keep Splash Screen and App on Same Display
    By ajb_advance in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2009, 11:49
  4. Display a QWidget using multi-screen
    By tarod in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 14:02
  5. how to display a window full screen??
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 8th May 2006, 12:07

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.