Results 1 to 5 of 5

Thread: Is there a way to view a QImage “live” on the UI?

  1. #1
    Join Date
    Jun 2012
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Question Is there a way to view a QImage “live” on the UI?

    I’m learning Qt, and have come across a problem I can’t figure out, so I’d like to ask the experts!

    I am working on an application where I’d like to have a QImage object (using format QImage::Format_RGB888), and be able to manipulate individual pixels with the setPixel() method & save the images with QImageWriter… so far, so good. This all works.

    My way of showing this Qimage is that the QMainWIndow contains a QGraphicsView object, and I created a QGraphicsScene, and set that scene on my MainWindow graphicsView.

    The problem is that I’d like to be able to show this QImage on the UI “live”, such that the user can see the pixels changing, as they are being manipulated. Currently, I have to re-generate a QGraphicsPixmapItem from the image, and re-addPixmap() to the scene, every time I want to see new changes.

    Is there a way to view a QImage live, so that changes that are made are immediately seen? Am I using the wrong objects to hold and/or display my image?

    I have a simple example attached (just the mainwindow.cpp part… the other files are just default stuff). This UI just has a button (to trigger the QImage changes), and place to display the QImage on the screen.

    I’ve searched the internet, but haven’t come across any posts that seem relevant. If anyone has any suggestions, I’d appreciate hearing them! thanks,

    -Eric




    Qt Code:
    1. QGraphicsScene *scene = NULL;
    2. QGraphicsItem *line = NULL;
    3. QImage *image = NULL;
    4. QGraphicsPixmapItem *item = NULL;
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. scene = new QGraphicsScene();
    13. image = new QImage(60, 60, QImage::Format_RGB888 );
    14.  
    15. image->fill(Qt::cyan);
    16.  
    17. ui->retranslateUi(this);
    18. ui->graphicsView->setScene(scene);
    19. ui->graphicsView->show();
    20.  
    21. line = (QGraphicsItem*) scene->addLine( QLine( 20, 40, 300, 100 ),
    22. QPen(Qt::red, 6, Qt::DashLine, Qt::FlatCap));
    23. scene->setBackgroundBrush(QBrush(Qt::green, Qt::SolidPattern));
    24. scene->addEllipse(40, 80, 300, 240,
    25. QPen(Qt::blue, 10, Qt::DashDotDotLine, Qt::RoundCap));
    26.  
    27. item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));
    28. scene->addPixmap(item->pixmap());
    29.  
    30. // Connect the pushbutton to the buttonPressed method, below.
    31. connect( ui->pushButton, SIGNAL(pressed()),
    32. this, SLOT( buttonPressed() ) );
    33. }
    34.  
    35. // Slot connected to the button being pressed.
    36. // Manipulate some pixels, and show the results.
    37. void MainWindow::buttonPressed()
    38. {
    39. printf("Now in buttonPressed...\n");
    40. int x, y;
    41. int offset = qrand();
    42. QRgb px;
    43.  
    44. px = qRgb(20+offset, 10-offset, 30+offset);
    45.  
    46. for (x=0; x< 60; x++)
    47. for(y=0; y< 60; y++)
    48. {
    49. image->setPixel(x, y, px );
    50. }
    51. // I'd like to NOT have to re-convert the image every time.
    52. item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));
    53. scene->addPixmap(item->pixmap());
    54. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there a way to view a QImage “live” on the UI?

    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is there a way to view a QImage “live” on the UI?

    Rather than recreating the whole QGraphicsPixmapItem each time you could just call setPixmap() on it using the pointer you are already holding. You cannot avoid converting the QImage for display.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. #4
    Join Date
    Jun 2012
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Is there a way to view a QImage “live” on the UI?

    Hi Amleto, Thanks for this pointer... I will look into it! -Eric


    Added after 32 minutes:


    Hi ChrisW67, Thanks for looking at this...
    I will explore this option too! -Eric
    Last edited by ericrose44; 17th July 2012 at 01:47.

  5. #5
    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: Is there a way to view a QImage “live” on the UI?

    You should have a look at the "Queued Custom Type Example" in Qt SDK.

Similar Threads

  1. Replies: 5
    Last Post: 16th May 2011, 21:15
  2. How to switch view in sliding/moving new view from right of device to the left
    By curiouswalker in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th November 2010, 11:55
  3. Replies: 5
    Last Post: 21st November 2007, 20:38
  4. Model-view: Creating a custom view
    By taboom in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 20:36
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

Tags for this Thread

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.