Results 1 to 8 of 8

Thread: QPainter doesn't seem to draw QImage

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.