Results 1 to 8 of 8

Thread: QPainter doesn't seem to draw QImage

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPainter doesn't seem to draw QImage

    Hello,
    I want to draw a QImage based off of a PNG file.
    It compiles fine, but it doesn't show my PNG file.

    Here's the piece of code I use:

    The .h
    Qt Code:
    1. #ifndef DRAWINGMAP_H
    2. #define DRAWINGMAP_H
    3.  
    4. #include <QtGui/QWidget>
    5. #include "ui_drawingmap.h"
    6. #include <QtCore/QList>
    7. #include <QtGui/QImage>
    8.  
    9. struct myPos2D
    10. {
    11. int x;
    12. int y;
    13. int yaw;
    14. };
    15.  
    16. class DrawingMap : public QWidget
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. DrawingMap(QWidget *parent = 0);
    22. ~DrawingMap();
    23.  
    24. public slots:
    25. void setPos(int x , int y , int yaw);
    26. void setRobotDiameter(int diameter);
    27.  
    28. protected:
    29. void paintEvent(QPaintEvent *event);
    30.  
    31.  
    32. private:
    33. Ui::DrawingMapClass ui;
    34.  
    35. int x_pos, y_pos, yaw;
    36. int robotDiameter;
    37. int zoom;
    38.  
    39. QImage robotImage;
    40.  
    41. QList<myPos2D> positionList;
    42.  
    43. };
    44.  
    45. #endif // DRAWINGMAP_H
    To copy to clipboard, switch view to plain text mode 

    The .cpp
    Qt Code:
    1. #include "drawingmap.h"
    2.  
    3. #include <QtGui/QPainter>
    4.  
    5. DrawingMap::DrawingMap(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. x_pos = y_pos = yaw = 0;
    9. robotDiameter = 10;
    10. zoom = 1;
    11.  
    12. ui.setupUi(this);
    13. }
    14.  
    15. DrawingMap::~DrawingMap()
    16. {
    17.  
    18. }
    19.  
    20. void DrawingMap::paintEvent(QPaintEvent * /* event */)
    21. {
    22. myPos2D newPos; //this is a struct with int x, int y, int yaw
    23.  
    24. newPos.x = x_pos;
    25. newPos.y = y_pos;
    26. newPos.yaw = yaw;
    27.  
    28. int oldX = 0, oldY = 0;
    29.  
    30. positionList.append(newPos); //This is a QList
    31.  
    32. QPainter painter(this);
    33.  
    34. painter.setPen(Qt::SolidLine);
    35. painter.setPen(Qt::red);
    36.  
    37. painter.translate(rect().width()/2, rect().height()/2);
    38.  
    39. //draw the robot trail
    40. for(int i = 0; i < positionList.size(); i++)
    41. {
    42. //draw trail
    43. painter.drawLine(oldX * zoom , oldY * zoom, positionList.at(i).x * zoom, positionList.at(i).y * zoom);
    44.  
    45. //remember old positions
    46. oldX = positionList.at(i).x;
    47. oldY = positionList.at(i).y;
    48. }
    49.  
    50. painter.setPen(Qt::NoPen);
    51. painter.setBrush(Qt::blue);
    52.  
    53. //Get and correct image
    54. robotImage = QImage("myPNGfile.png"); //robotImage is a QImage
    55. robotImage.convertToFormat(QImage::Format_ARGB32_Premultiplied ,Qt::ColorOnly);
    56. //robotImage.createMaskFromColor(0x00000000,Qt::MaskOutColor); // make black transparent
    57.  
    58. //Go to correct position
    59. painter.translate(x_pos * zoom, y_pos * zoom);
    60. painter.rotate(yaw);
    61. //draw the robot
    62. painter.drawImage(0 , 0 , robotImage);
    63.  
    64. /*
    65.   painter.drawPie(QRect((-robotDiameter/2) * zoom, (-robotDiameter/2) * zoom, robotDiameter * zoom, robotDiameter * zoom), 0, 360 * 16);
    66.   painter.setPen(Qt::SolidLine);
    67.   painter.drawLine(0 , 0, 0 , (robotDiameter + robotDiameter/4) * zoom );
    68. */
    69. }
    70.  
    71. void DrawingMap::setPos(int x , int y , int ya)
    72. {
    73. x_pos = x;
    74. y_pos = y;
    75. yaw = ya;
    76.  
    77. update();
    78. }
    79.  
    80. void DrawingMap::setRobotDiameter(int diameter)
    81. {
    82. zoom = diameter;
    83. update();
    84. }
    To copy to clipboard, switch view to plain text mode 

    The .h from the .ui file Qt spit out
    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'drawingmap.ui'
    3. **
    4. ** Created: Mon Mar 2 11:22:28 2009
    5. ** by: Qt User Interface Compiler version 4.4.3
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_DRAWINGMAP_H
    11. #define UI_DRAWINGMAP_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QWidget>
    18.  
    19. QT_BEGIN_NAMESPACE
    20.  
    21. class Ui_DrawingMapClass
    22. {
    23. public:
    24.  
    25. void setupUi(QWidget *DrawingMapClass)
    26. {
    27. if (DrawingMapClass->objectName().isEmpty())
    28. DrawingMapClass->setObjectName(QString::fromUtf8("DrawingMapClass"));
    29. DrawingMapClass->resize(400, 300);
    30.  
    31. retranslateUi(DrawingMapClass);
    32.  
    33. QMetaObject::connectSlotsByName(DrawingMapClass);
    34. } // setupUi
    35.  
    36. void retranslateUi(QWidget *DrawingMapClass)
    37. {
    38. DrawingMapClass->setWindowTitle(QApplication::translate("DrawingMapClass", "DrawingMap", 0, QApplication::UnicodeUTF8));
    39. Q_UNUSED(DrawingMapClass);
    40. } // retranslateUi
    41.  
    42. };
    43.  
    44. namespace Ui {
    45. class DrawingMapClass: public Ui_DrawingMapClass {};
    46. } // namespace Ui
    47.  
    48. QT_END_NAMESPACE
    49.  
    50. #endif // UI_DRAWINGMAP_H
    To copy to clipboard, switch view to plain text mode 

    The slots are used so that I can change the robot position and direction.
    This QWidget is inserted in another QWidget which is the main window in my program.
    I inserted my custom QWidget with Qt Designer and promoted the widget.
    So it should be all standard Qt Designer code

    It paints the Lines just fine.
    And the very simple representation of the robot as well, that is the blue dot I commented out of the program.
    Last edited by Denarius; 3rd March 2009 at 07:50. Reason: updated contents

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

    Default Re: QPainter doesn't seem to draw QImage

    have you checked that/if
    a) the image is found (maybe its in a subdirectory or in a resource (":/...") )
    b) your Qt supports png
    c) the file is a png (not a gif just named png... happened to me once ;-)


    side note: for performance reasons I would move the loading/converting of the image out of the paintEvent (maybe into the widget's constructor)

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

    Denarius (3rd March 2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't seem to draw QImage

    Quote Originally Posted by caduel View Post
    have you checked that/if
    a) the image is found (maybe its in a subdirectory or in a resource (":/...") )
    b) your Qt supports png
    c) the file is a png (not a gif just named png... happened to me once ;-)


    side note: for performance reasons I would move the loading/converting of the image out of the paintEvent (maybe into the widget's constructor)
    a) yes it's found. otherwise it would give me a compiler error stating that it didn't find the image.
    b) How do you check this?
    The Qt api states that it supports PNG files, so would this be the answer?
    c) the file extension is .png So I'm pretty shure it's a png
    I also tried the GIF extension etc. But no luck, same problem

    i'll do the performance later. I just want the image to draw first.

  5. #4
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't seem to draw QImage

    Ahw,
    I found the problem.
    i had to put a ./ before the whole path

  6. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't seem to draw QImage

    so caduel was right image file wasnt found..u r using a qrc file so it wont give a compilation error if the file isnt found..all will be done at compile time

  7. #6
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't seem to draw QImage

    new problem.

    I've changed the code for drawing the image file to this:
    Qt Code:
    1. //Correct image
    2. robotImage.convertToFormat(QImage::Format_ARGB32_Premultiplied ,Qt::AutoColor);
    3. //robotImage.createMaskFromColor(0x00000000,Qt::MaskOutColor); // make black transparent
    4. robotImage.scaled(robotDiameter * zoom, robotDiameter * zoom , Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    5. int offset_x = -robotImage.width() / 2;
    6. int offset_y = -robotImage.height() / 2;
    7.  
    8. //Go to correct position
    9. painter.translate(x_pos * zoom, y_pos * zoom);
    10. painter.rotate(yaw);
    11. //draw the robot
    12. painter.drawImage(offset_x , offset_y , robotImage, 0 , 0 , robotImage.width() * zoom , robotImage.height() * zoom);
    To copy to clipboard, switch view to plain text mode 

    But it doesn't resize or rescale the image.
    Or am I missing something?

    P.S.
    I loaded the image in the constructor now. Thanks for the suggestion

  8. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't seem to draw QImage

    Hi,

    You are scaling the image but you don't store the returning image of the method to any variable:

    Qt Code:
    1. robotImage.scaled(robotDiameter * zoom, robotDiameter * zoom , Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 

    to:

    Qt Code:
    1. QImage qScaledImage = robotImage.scaled(robotDiameter * zoom, robotDiameter * zoom , Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 

    Then you have to paint "qScaledImage"
    Òscar Llarch i Galán

  9. The following user says thank you to ^NyAw^ for this useful post:

    Denarius (3rd March 2009)

  10. #8
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter doesn't seem to draw QImage

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    You are scaling the image but you don't store the returning image of the method to any variable:

    Qt Code:
    1. robotImage.scaled(robotDiameter * zoom, robotDiameter * zoom , Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 

    to:

    Qt Code:
    1. QImage qScaledImage = robotImage.scaled(robotDiameter * zoom, robotDiameter * zoom , Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 

    Then you have to paint "qScaledImage"
    This worked like a charm.
    Thank you very much

Similar Threads

  1. use Qpainter to draw lines + problem in my painter
    By ReSu in forum Qt Programming
    Replies: 4
    Last Post: 5th March 2008, 15:44
  2. QPainter draw rounded arrow
    By xgoan in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2006, 12:32
  3. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  4. Trouble with image painting (QPainter + QImage)
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 20:25
  5. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 24th January 2006, 23:12

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.