Results 1 to 3 of 3

Thread: select QPixmap area with mouse

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default select QPixmap area with mouse

    Hi. I have a code (please see below) that displays a user-provided bitmap image (image_ori.bmp) when a pushbutton is clicked. I want the user to be able to select two coordinates on the image using mouse (one left click and one right click) and by doing it, crop the picture. Then I want the cropped picture to be displayed instead of the original picture. When the pushbutton is clicked, the original picture (uncropped) is displayed again.
    I can get this to sort of work. But the coordinates are off and selection is offset. I think this is because I use global coordinates but I need local to that QLabel widget. How do you get local coordinates with mouse? Do you need to subclass mouse event for that (in the same subclass with the QLabel that displays the picture?)?
    Also, what's the best way of avoiding having to save an load files every time you crop the picture like I rather unprofessionally do it?
    I know you say that the QLabel approach is not the best for cropping and displaying but I kind of need to stick to this weird appraoch because a program that I write this code for requires this approach. Thanks a lot guys!

    Qt Code:
    1. ////////////////////////main.cpp//////////////////////////////
    2. #include <QApplication>
    3. #include <QtGui>
    4. #include "tom.h"
    5.  
    6. int right_x, right_y, left_x, left_y;
    7.  
    8. MyWidget::MyWidget(QWidget* parent): QWidget(parent)
    9. {
    10. m_pixmapLabel = new QLabel;
    11. m_drawButton = new QPushButton("&Load Original Image");
    12. connect(m_drawButton, SIGNAL(clicked()), this, SLOT(refresh()));
    13. QVBoxLayout* mainLayout = new QVBoxLayout;
    14. mainLayout->addWidget(m_pixmapLabel);
    15. mainLayout->addWidget(m_drawButton);
    16. setLayout(mainLayout);
    17. }
    18.  
    19. void MyWidget::refresh()
    20. {
    21. QPixmap pm3;
    22. pm3.load("image_ori.bmp");
    23. pm3.save("image.bmp", 0, -1);
    24. testDraw();
    25. }
    26.  
    27. void MyWidget::testDraw()
    28. {
    29. right_x=-1;
    30. right_y=-1;
    31. left_x=9999;
    32. left_y=9999;
    33. QPixmap pm;
    34. pm.load("image.bmp");
    35. m_pixmapLabel->setPixmap(pm);
    36. }
    37.  
    38. void MyWidget::mousePressEvent(QMouseEvent *event)
    39. {
    40. if (event->button() == Qt::RightButton)
    41. {
    42. right_x=event->x();
    43. right_y=event->y();
    44. }
    45. if
    46. (event->button() == Qt::LeftButton)
    47. {
    48. left_x=event->x();
    49. left_y=event->y();
    50. }
    51.  
    52. if ( (right_x - left_x) >=0 && (right_y - left_y >=0) && (left_x !=9999) && (right_x !=-1))
    53. {
    54. QPixmap pm2;
    55. pm2.load("image.bmp");
    56. pm2 = pm2.copy(left_x,left_y,right_x,right_y);
    57. pm2.save("image.bmp", 0, -1);
    58. m_pixmapLabel->setPixmap(pm2);
    59. }
    60.  
    61. else QWidget::mousePressEvent(event);
    62. }
    63.  
    64.  
    65. int main(int argc, char *argv[])
    66. {
    67. QApplication application(argc, argv);
    68. MyWidget window;
    69. window.show();
    70. return application.exec();
    71. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ////////////////////////////////////tom.h///////////////////////////////////////
    2. #ifndef TOM_H
    3. #define TOM_H
    4.  
    5. #include <QtGui>
    6. class MyWidget : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. MyWidget (QWidget* parent = 0);
    11. protected:
    12. void mousePressEvent(QMouseEvent *event);
    13.  
    14. public slots:
    15. void testDraw();
    16. void refresh();
    17. private:
    18. QLabel* m_pixmapLabel;
    19. QPushButton* m_drawButton;
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 16th November 2007 at 20:07. Reason: missing [code] tags

Similar Threads

  1. Weird behaviour of mouse events and QMenu pop-ups
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 07:46
  2. select a particular area
    By vishesh in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2007, 21:44

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.