Results 1 to 17 of 17

Thread: Displaying a QImage through pixels and QGraphicsView

  1. #1
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Displaying a QImage through pixels and QGraphicsView

    Hey guys,

    I was wondering if anyone could help me with a problem regarding to QImage and QGraphicsView. I currently used coding to create the image through QImage from a tutorial on Trolltech. The problem I am having is that I am uncertain if I'm getting the right image displayed since the image is just several dots when I try running the code. Is this incorrect? If it is correct, is there a way to enlarge the pixels shown in the image?

    Qt Code:
    1. QImage image(3, 3, QImage::Format_Indexed8);
    2. QRgb value;
    3.  
    4. value = qRgb(122, 163, 39); // 0xff7aa327
    5. image.setColor(0, value);
    6.  
    7. value = qRgb(237, 187, 51); // 0xffedba31
    8. image.setColor(1, value);
    9.  
    10. value = qRgb(189, 149, 39); // 0xffbd9527
    11. image.setColor(2, value);
    12.  
    13. image.setPixel(0, 1, 0);
    14. image.setPixel(1, 0, 0);
    15. image.setPixel(1, 1, 2);
    16. image.setPixel(2, 1, 1);
    17.  
    18. QGraphicsScene *scene = new QGraphicsScene (this);
    19. QPixmap pixmap = QPixmap::fromImage(image);
    20. scene->addPixmap(pixmap);
    21. delete QGraphicsViewscene();
    22. QGraphicsView.setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Strateng

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    The image you are creating is 3pixels x 3pixels big.. what do you expect?

    what do you do in delete QGraphicsViewscene(); ?? Usually you only setup your scene once. And delete/add items to it.

    Is this related to your other posting regarding OpenGL? You can set the QGraphicsView to use an OpenGL Viewport.
    Qt will then automatically draw your image with OpenGL.. no need for drawpixels..

    Joh
    Last edited by JohannesMunk; 23rd March 2010 at 11:29.

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    My last post somehow didn't register as post to the thread..

  4. #4
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    Thanks for your reply. The two threads are linked somewhat but I was wondering in particular in this thread if there was a way to zoom in to check if the pixels are the correct colour. The main problem i seem to be having is that when I increase the width and height of the QImage to 300 by 300, I get an image of the first pixel. I thank the delete QGraphicsViewScene is just to erase any previous set scene so that it changes when you want to add a new scene. There seems to be an error with this line since when I copied it over a part was cut off.

    Thanks,
    Strateng

    Quote Originally Posted by JohannesMunk View Post
    The image you are creating is 3pixels x 3pixels big.. what do you expect?

    what do you do in delete QGraphicsViewscene(); ?? Usually you only setup your scene once. And delete/add items to it.

    Is this related to your other posting regarding OpenGL? You can set the QGraphicsView to use an OpenGL Viewport.
    Qt will then automatically draw your image with OpenGL.. no need for drawpixels..

    Joh

  5. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Don't delete the scene. Thats very wrong! You set it up, once! Look at the QGraphicsView examples.

    You need to initalize the pixmap. You can't just make it bigger..

    HIH

    Johannes

  6. #6
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    Line 21 should be:

    delete QGraphicsViewscene.scene();

    instead of

    delete QGraphicsViewscene();

    Thanks,
    Strateng

  7. #7
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    Sorry I'm a little confused by this. Did you mean don't delete the actual QGraphicsScene? Cause if so there was an error when I copied the code over from QT. When you mentioned initalizing the pixmap, I am not sure what you meant exactly by that. Can you explain that a little further?

    Thanks,
    Strateng

  8. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Ok.. I have coded a litte example:

    Qt Code:
    1. project file:
    2.  
    3. TARGET = imgloader
    4. TEMPLATE = app
    5. SOURCES += main.cpp
    6.  
    7. main.cpp
    8.  
    9. #include <QApplication>
    10. #include <QtGui>
    11. #include <QtCore>
    12.  
    13. class MainForm : public QWidget
    14. { Q_OBJECT
    15. public:
    16. MainForm() {
    17. scene = new QGraphicsScene();
    18. view = new QGraphicsView(scene);
    19. QHBoxLayout* hl = new QHBoxLayout();
    20. QPushButton* loadimg_pb = new QPushButton("Load Image(s) ..");
    21. connect(loadimg_pb,SIGNAL(clicked()),this,SLOT(LoadImages()));
    22. hl->addWidget(loadimg_pb,0);
    23. hl->addStretch(1);
    24. QVBoxLayout* vl = new QVBoxLayout();
    25. vl->addWidget(view);
    26. vl->addLayout(hl);
    27. setLayout(vl);
    28. }
    29. protected slots:
    30. void LoadImages() {
    31. QStringList filenames = QFileDialog::getOpenFileNames(0, "Open Image", "", "Images (*.jpg;*.jpeg;*.bmp;*.png)");
    32. for (int i=0;i<filenames.length();++i)
    33. {
    34. QImage img(filenames.at(i));
    35. if (!img.isNull()) {
    36. QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(img).scaledToWidth(50));
    37. pi->setFlag(QGraphicsItem::ItemIsMovable,true);
    38. pi->setFlag(QGraphicsItem::ItemIsSelectable,true);
    39. pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
    40. }
    41. }
    42. }
    43.  
    44. private:
    45. };
    46.  
    47. int main(int argc, char *argv[])
    48. {
    49. QApplication app(argc, argv);
    50. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    51.  
    52. MainForm form;
    53. form.setGeometry(100,100,800,600);
    54. form.show();
    55.  
    56. return app.exec();
    57. }
    58.  
    59. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    You can see.. the scene and view are setup only once.. afterwards items are added..

    QPixmap setup: You need to define a color value for each pixel. In your code example you are using indexed colors. Thats why they first setup the 3 colors and then set the values of the pixels.. But this has nothing to do with your graphicsscene but with QPixmap setup.. In the example above I simply load the pixmap from file..

    What do you want to show?

    Johannes
    Last edited by JohannesMunk; 25th March 2010 at 00:07. Reason: Load several images at once. Movable. Selectable.. Gotta stop :->

  9. #9
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    I'm just wondering though with the setup you're doing. The image will only be loading once right? Therefore your program would crash if you tried to load another image? I'm trying to show a QImage made up of pixels only on to QGraphicsScene.

    Thanks,
    Strateng

  10. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    No.. It can load lots of items. Each time you click the button.. an image is created and added to the scene. Compile it and see for yourself!

    What pixels? What do you want to show? I didn't want to create a random image.. so I just loaded them from file..

    Any further questions?

    Johannes

  11. #11
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    I didn't store any reference to the images, though.. Maybe thats what made you wonder. The local scope of the variable img doesn't hurt here, because it gets deep copied when I add it to the scene and thats before it gets destroyed.

    Johannes

  12. #12
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    Just wondering though what do you mean by added to the scene? Does that mean the scene automatically updates and moves to the next image? Oh the pixel section is fine but would you be able to write a little example of connecting a QImage thats been converted to OpenGL to be viewed on QGraphicsView?

    Thanks,
    Strateng

  13. #13
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Just try to run the example! You would see, that it doesn't move to the next image, but it shows them all.. The item is added to the scene, the scene updates automatically, like you said, and all images are shown.

    As for OpenGL. You just call view->setViewPort(new QGLWidget()) and everything is drawn using OpenGL!

    Qt Code:
    1. pro:
    2. QT += opengl
    3. TARGET = listclass
    4. TEMPLATE = app
    5. SOURCES += main.cpp
    6.  
    7. main.cpp:
    8. #include <QApplication>
    9. #include <QDebug>
    10. #include <QtGui>
    11. #include <QtCore>
    12. #include <QtOpenGL>
    13.  
    14. class MainForm : public QWidget
    15. { Q_OBJECT
    16. public:
    17. MainForm() {
    18. scene = new QGraphicsScene();
    19. view = new QGraphicsView(scene);
    20. view->setViewport(new QGLWidget());
    21.  
    22. QPushButton* loadimg_pb = new QPushButton("Load Image(s) ..");
    23. connect(loadimg_pb,SIGNAL(clicked()),this,SLOT(LoadImages()));
    24. QPushButton* testimg_pb = new QPushButton("Test Image!");
    25. connect(testimg_pb,SIGNAL(clicked()),this,SLOT(TestImage()));
    26.  
    27. QHBoxLayout* hl = new QHBoxLayout();
    28. hl->addWidget(loadimg_pb,0);
    29. hl->addWidget(testimg_pb,0);
    30. hl->addStretch(1);
    31. QVBoxLayout* vl = new QVBoxLayout();
    32. vl->addWidget(view);
    33. vl->addLayout(hl);
    34. setLayout(vl);
    35. }
    36. protected slots:
    37. void LoadImages() {
    38. QStringList filenames = QFileDialog::getOpenFileNames(0, "Open Image", "", "Images (*.jpg;*.jpeg;*.bmp;*.png)");
    39. for (int i=0;i<filenames.length();++i)
    40. {
    41. QImage img(filenames.at(i));
    42. if (!img.isNull()) {
    43. QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(img).scaledToWidth(50));
    44. pi->setFlag(QGraphicsItem::ItemIsMovable,true);
    45. pi->setFlag(QGraphicsItem::ItemIsSelectable,true);
    46. pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
    47. }
    48. }
    49. }
    50. void TestImage() {
    51. QImage image(300, 300, QImage::Format_Indexed8);
    52. // Setup the color palette for indexed colormode
    53. QRgb value;
    54. value = qRgb(122, 163, 39); // 0xff7aa327
    55. image.setColor(0, value);
    56. value = qRgb(237, 187, 51); // 0xffedba31
    57. image.setColor(1, value);
    58. value = qRgb(189, 149, 39); // 0xffbd9527
    59. image.setColor(2, value);
    60. // Set pixels
    61. for (int x=0;x<300;++x) {
    62. for (int y=0;y<150;++y) {
    63. image.setPixel(x, y, 0);
    64. }
    65. }
    66. for (int x=0;x<150;++x) {
    67. for (int y=150;y<300;++y) {
    68. image.setPixel(x, y, 2);
    69. }
    70. }
    71. for (int x=150;x<300;++x) {
    72. for (int y=150;y<300;++y) {
    73. image.setPixel(x, y, 1);
    74. }
    75. }
    76. QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(image));
    77. pi->setFlag(QGraphicsItem::ItemIsMovable,true);
    78. pi->setFlag(QGraphicsItem::ItemIsSelectable,true);
    79. pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
    80. }
    81.  
    82. private:
    83. QGLWidget* glwidget;
    84. };
    85.  
    86. int main(int argc, char *argv[])
    87. {
    88. QApplication app(argc, argv);
    89. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    90.  
    91. MainForm form;
    92. form.setGeometry(100,100,800,600);
    93. form.show();
    94.  
    95. return app.exec();
    96. }
    97.  
    98. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Aah! The Qt Pixmap example, misses some pixels! You have to initialize the whole image with fill before.. if you want to avoid a crash :->

    Qt Code:
    1. void TestImage2() {
    2. QImage image(3,3, QImage::Format_Indexed8);
    3. // Setup the color palette for indexed colormode
    4. QRgb value;
    5. value = qRgb(122, 163, 39); // 0xff7aa327
    6. image.setColor(0, value);
    7. value = qRgb(237, 187, 51); // 0xffedba31
    8. image.setColor(1, value);
    9. value = qRgb(189, 149, 39); // 0xffbd9527
    10. image.setColor(2, value);
    11. value = qRgb(240, 240, 240); // light gray..
    12. image.setColor(3, value);
    13. // Initialize the whole image
    14. image.fill(3);
    15. // Set pixels
    16. image.setPixel(0, 1, 0);
    17. image.setPixel(1, 0, 0);
    18. image.setPixel(1, 1, 2);
    19. image.setPixel(2, 1, 1);
    20. QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(image).scaledToWidth(50));
    21. pi->setFlag(QGraphicsItem::ItemIsMovable,true);
    22. pi->setFlag(QGraphicsItem::ItemIsSelectable,true);
    23. pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
    24. }
    To copy to clipboard, switch view to plain text mode 
    Now you have two options to scale your pixmap, to see if the colors are right :->

    Johannes

  15. The following user says thank you to JohannesMunk for this useful post:

    strateng (25th March 2010)

  16. #15
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    Thanks I think you have answered all the questions I currently have. I'll get back to you if there are any other questions on the topic.

    Thanks,
    Strateng

  17. #16
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Great! Good night, then!

    Johannes

  18. #17
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a QImage through pixels and QGraphicsView

    Hey,

    I attempted using OpenGL to draw the pixel images with the following code

    Qt Code:
    1. QGraphicsView,setViewPort(new QGLWidget())
    To copy to clipboard, switch view to plain text mode 

    However I was given the following error when I attempted to run the program:

    WARNING: Application calling GLX 1.3 function "glXCreatePixmap" when GLX 1.3 is not supported! This is an application bug!
    failed to create drawable

    Can anyone help me with this problem?

    Thanks,
    Strateng

Similar Threads

  1. QImage displaying through OpenGL
    By strateng in forum Newbie
    Replies: 0
    Last Post: 23rd March 2010, 04:35
  2. best way of reading and writing QImage pixels?
    By billconan in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2010, 08:23
  3. Pixels
    By Dante in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2009, 20:50
  4. How to change the values of the pixels in an Qimage?
    By kid17 in forum Qt Programming
    Replies: 8
    Last Post: 23rd November 2008, 20:52
  5. Convert RAW 8 bit pixels into a QImage
    By danielperaza in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2008, 14:53

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.