Results 1 to 7 of 7

Thread: Simple ready-to-use widget to display an image

  1. #1
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face Simple ready-to-use widget to display an image

    Hello... I'm writing a multi-agent model and for the GUI I am using Qt.

    I need a widget where I can pass it an image and the widget display it, and has methods to zoom and pan it with the mouse (or keyboard), with optional call of "autofitting" it.

    Nothing more.

    I saw the mandelbrot example and the plotter example (the latter one on Chapter 5 of "C++ GUI programming with Qt4") but they are all too complex for my understanding of GUI programming (that has its own terminology and concepts well ahead of my level).

    Does anyone can suggest me a similar widget but ready to be used??

    Cheers,
    Sylvaticus

  2. #2
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simple ready-to-use widget to display an image

    You could try QGraphicsView with QGraphicsPixmapItem, it appears dificult at first but it's really easy

  3. #3
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple ready-to-use widget to display an image

    I saw that QGraphicsView and QGraphicsItems are suggested when there is a complex scene to diplay, with many items.
    Is it right to use it when the item is only one ( a pixmap) ??

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple ready-to-use widget to display an image

    It's fine although you might implement a simple subclass of QScrollArea and draw the image yourself too.

  5. #5
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: Simple ready-to-use widget to display an image

    I solved writing my own widget taking mandrebot as example (from default Qt examples).

    For those interested this is the code.. (or see the attached zip)

    MapBox.h
    Qt Code:
    1. #ifndef MAPBOX_H
    2. #define MAPBOX_H
    3.  
    4. #include <QPixmap>
    5. #include <QWidget>
    6.  
    7. class MapBox : public QWidget {
    8. Q_OBJECT
    9.  
    10. public:
    11. MapBox(QWidget *parent = 0);
    12.  
    13. protected:
    14. void paintEvent(QPaintEvent *event);
    15. void keyPressEvent(QKeyEvent *event);
    16. void wheelEvent(QWheelEvent *event);
    17. void mousePressEvent(QMouseEvent *event);
    18. void mouseMoveEvent(QMouseEvent *event);
    19.  
    20. private slots:
    21. void updatePixmap(const QImage &image);
    22. void fitInWindows();
    23.  
    24. private:
    25. void zoom(double zoomFactor);
    26. void scroll(int deltaX, int deltaY);
    27. QPixmap pixmap;
    28. QPoint lastDragPos;
    29. double sx1, sy1, sx2, sy2; ///< coordinates of corner pixels of source - pixmap - rectangle
    30. double dx1, dy1, dx2, dy2; ///< coordinates of corner pixels of destination - widget - rectangle
    31. };
    32.  
    33. #endif
    To copy to clipboard, switch view to plain text mode 

    MapBox.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <math.h>
    3. #include "MapBox.h"
    4.  
    5. const double widgetX = 400;
    6. const double widgetY = 400;
    7. const double ZoomOutFactor = 0.8;
    8. const double ZoomInFactor = 1 / ZoomOutFactor;
    9. const double ScrollStep = 20;
    10.  
    11. MapBox::MapBox(QWidget *parent) :QWidget(parent) {
    12.  
    13. //qRegisterMetaType<QImage>("QImage");
    14.  
    15. setWindowTitle(tr("MapBox"));
    16. setCursor(Qt::CrossCursor);
    17. resize(400, 400);
    18.  
    19. // setting source and destination init corners..
    20. sx1 = 0;
    21. sy1 = 0;
    22. sx2 = widgetX;
    23. sy2 = widgetY;
    24. dx1 = 0;
    25. dy1 = 0;
    26. dx2 = widgetX;
    27. dy2 = widgetY;
    28.  
    29. QImage image;
    30. image.load("biggerO.png");
    31. updatePixmap(image);
    32.  
    33. }
    34.  
    35. void
    36. MapBox::paintEvent(QPaintEvent *){
    37. QPainter painter(this);
    38. painter.fillRect(rect(), Qt::black);
    39. if (pixmap.isNull()) return;
    40. QRectF source (sx1, sy1, sx2-sx1, sy2-sy1); // the second point is in coordinates origin of the firt point !!!!
    41. QRectF destination(dx1, dy1, dx2-dx1, dy2-dy1); // the second point is in coordinates origin of the firt point !!!!
    42. /*
    43. This is the main function of the widget... the good pointa are:
    44. A) It takes into account the low level details of scaling, such interpolation
    45. B) If the destination is outside the widgets bounds, it doesn't matter. It make its job
    46. on the widget without any error (in this sence it isnot like an array luckily...)
    47. */
    48. painter.drawPixmap(destination, pixmap, source);
    49. }
    50.  
    51. void
    52. MapBox::keyPressEvent(QKeyEvent *event) {
    53. switch (event->key()) {
    54. case Qt::Key_Plus:
    55. zoom(ZoomInFactor);
    56. break;
    57. case Qt::Key_Minus:
    58. zoom(ZoomOutFactor);
    59. break;
    60. case Qt::Key_Left:
    61. scroll(+ScrollStep, 0);
    62. break;
    63. case Qt::Key_Right:
    64. scroll(-ScrollStep, 0);
    65. break;
    66. case Qt::Key_Down:
    67. scroll(0, -ScrollStep);
    68. break;
    69. case Qt::Key_Up:
    70. scroll(0, +ScrollStep);
    71. break;
    72. default:
    73. QWidget::keyPressEvent(event);
    74. }
    75. }
    76.  
    77. void
    78. MapBox::wheelEvent(QWheelEvent *event){
    79. int numDegrees = event->delta() / 8;
    80. double numSteps = numDegrees / 15.0f;
    81. zoom(pow(ZoomInFactor, numSteps));
    82. }
    83.  
    84. void
    85. MapBox::mousePressEvent(QMouseEvent *event){
    86. if (event->button() == Qt::LeftButton)
    87. lastDragPos = event->pos();
    88. }
    89.  
    90. void
    91. MapBox::mouseMoveEvent(QMouseEvent *event) {
    92. if (event->buttons() & Qt::LeftButton) {
    93. scroll(event->pos().x()-lastDragPos.x(), event->pos().y()-lastDragPos.y());
    94. lastDragPos = event->pos();
    95. update();
    96. }
    97. }
    98.  
    99. void
    100. MapBox::updatePixmap(const QImage &image){
    101. pixmap = QPixmap::fromImage(image);
    102. fitInWindows();
    103. }
    104.  
    105. void MapBox::fitInWindows(){
    106.  
    107. double tempXScale = widgetX/pixmap.width();
    108. double tempYScale = widgetY/pixmap.height();
    109.  
    110. sx1 = 0;
    111. sy1 = 0;
    112. sx2 = pixmap.width();
    113. sy2 = pixmap.height();
    114. dx1 = 0;
    115. dy1 = 0;
    116.  
    117. if ( tempXScale >= tempYScale){
    118. dx2 = pixmap.width()*tempYScale;
    119. dy2 = widgetY;
    120. } else {
    121. dx2 = widgetX;
    122. dy2 = pixmap.height()*tempXScale;
    123. }
    124. update();
    125. }
    126.  
    127. void
    128. MapBox::zoom(double zoomFactor){
    129. double dx1new, dx2new, dy1new, dy2new;
    130. dx1new = dx2- (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
    131. dx2new = dx1+ (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
    132. dy1new = dy2- (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
    133. dy2new = dy1+ (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
    134. dx1 = dx1new;
    135. dy1 = dy1new;
    136. dx2 = dx2new;
    137. dy2 = dy2new;
    138. update();
    139. }
    140.  
    141. void
    142. MapBox::scroll(int deltaX, int deltaY){
    143. dx1 += ((double) deltaX);
    144. dx2 += ((double) deltaX);
    145. dy1 += ((double) deltaY);
    146. dy2 += ((double) deltaY);
    147. update();
    148. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by jacek; 28th December 2007 at 14:43. Reason: wrapped too long line

  6. #6

    Default JavaScript zoomifier alternative

    If you are not tied to using QT, you could do it in JavaScript.

    Magic Zoom is a neat script that has a good zoom effect on rollover. It's controlled via the mouse.

    http://www.magictoolbox.com/magiczoom

  7. #7
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: JavaScript zoomifier alternative

    1) My program is a desktop applicaiton, not a web-site one (hovewer I am aware Qt can be scripted with a javascript-alike scripting engine, but I never used it and I don't know if this is the case);
    2) The program you cited is not free, and this would be a problem for my gpl-program;
    3) I already solved in the above way.

    Thank you any how...
    Sylvaticus

Similar Threads

  1. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  2. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  3. Replies: 4
    Last Post: 10th March 2007, 18:01
  4. Simple display of an image
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 15th January 2007, 23:49
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

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.