Results 1 to 12 of 12

Thread: pixmap onmousemove event help

  1. #1
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default pixmap onmousemove event help

    Hey all. I am a newb to Qt/C++ programming. I have created a widget with three pixmaps. I am trying to attach an onmousemove event to one of the pixmaps so it will move along the x-axis when the onmousemove event is active.

    in the onmousepress event i set :
    Qt Code:
    1. dragPosition = event->globalPos() - frameGeometry().topLeft()
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. event->accept()
    To copy to clipboard, switch view to plain text mode 


    in the onmousemove event:
    i check to see if the
    Qt Code:
    1. pixmap.rect().contains(dragposition)
    To copy to clipboard, switch view to plain text mode 
    and then i do
    Qt Code:
    1. pixmap.rect().moveCenter(dragPosition.x())
    To copy to clipboard, switch view to plain text mode 
    then finally i do:
    Qt Code:
    1. update(pixmap.rect())
    2. event->accept()
    To copy to clipboard, switch view to plain text mode 


    everything compiles fine, but when i run it nothing happens. i have thrown a messagebox in to make sure the contains() was being captured onmousemove. it was, so i am stuck. any help would be appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pixmap onmousemove event help


  3. #3
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: pixmap onmousemove event help

    sorry i forgot to mention that i did that already. below is the src code

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include <stdlib.h>
    4.  
    5. #include "caliper.h"
    6.  
    7. Caliper::Caliper(QWidget *parent)
    8. : QWidget(parent)
    9. {
    10. currentDistance = 0;
    11. currentAngle = 0;
    12. rotating = false;
    13. moving = false;
    14. mainPt = QPointF(0.0, 0.0);
    15. piecePt = QPointF(mainPix().rect().width(),mainPix().rect().height()/3.38);
    16. movePt = QPointF(mainPix().rect().width() + piecePix().rect().width(), -2.0);
    17.  
    18. setMouseTracking(true);
    19. setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    20. setWindowTitle(tr("Caliper Widget"));
    21. }
    22.  
    23. void Caliper::setAngle(int angle)
    24. {
    25. if (currentAngle == angle)
    26. return;
    27. currentAngle = angle;
    28. update(movePix().rect());
    29. emit angleChanged(currentAngle);
    30. }
    31.  
    32. void Caliper::setDistance(int distance)
    33. {
    34. if (currentDistance == distance)
    35. return;
    36. currentDistance = distance;
    37. emit distanceChanged(currentDistance);
    38. }
    39.  
    40.  
    41. void Caliper::startRotate()
    42. {
    43. if (isRotating())
    44. return;
    45. else rotating=true;
    46. }
    47.  
    48. bool Caliper::isMoving() const
    49. {
    50. return moving;
    51. }
    52.  
    53. bool Caliper:: isRotating() const
    54. {
    55. return rotating;
    56. }
    57.  
    58. void Caliper::startMeasure()
    59. {
    60. if (isMoving())
    61. return;
    62. else moving = true;
    63. }
    64.  
    65. void Caliper::moveMeasurer()
    66. {
    67. movePix().rect().moveRight(movePix().rect().x() + currentDistance);
    68. update();
    69. }
    70.  
    71. void Caliper::mousePressEvent(QMouseEvent *event)
    72. {
    73. if (event->button() == Qt::LeftButton)
    74. {
    75. dragPosition = event->globalPos() - frameGeometry().topLeft();
    76. event->accept();
    77. }
    78. }
    79.  
    80. void Caliper::mouseMoveEvent(QMouseEvent *event)
    81. {
    82. if (event->buttons() == Qt::LeftButton)
    83. {
    84. if (movePix().rect().contains( event->globalPos() - frameGeometry().topLeft()) )
    85. {
    86. movePix().rect().moveRight( dragPosition.x() - movePix().rect().x() );
    87. update();
    88. event->accept();
    89. // setDistance(event->globalPos().x() - frameGeometry().topLeft().x());
    90. }
    91. else if (mainPix().rect().contains( dragPosition ) )
    92. {
    93. move(event->globalPos() - dragPosition);
    94. event->accept();
    95. }
    96. }
    97. }
    98.  
    99. void Caliper::mouseReleaseEvent(QMouseEvent *event)
    100. {
    101. if (event->button() == Qt::LeftButton)
    102. {
    103. event->accept();
    104. return;
    105. }
    106. }
    107.  
    108. void Caliper::paintEvent(QPaintEvent * /* event */)
    109. {
    110. QPainter painter(this);
    111.  
    112. paintMove(painter);
    113. paintMain(painter);
    114. paintPiece(painter);
    115.  
    116. fullPix = mainPix().rect().unite(movePix().rect().unite(piecePix().rect()));
    117. //QRegion maskedRegion(fullPix, QRegion::Rectangle);
    118. //setMask(maskedRegion);
    119. }
    120.  
    121. QPixmap Caliper::movePix() const
    122. {
    123. QPixmap result(":/imgs/moveBody.png");
    124. return result;
    125. }
    126.  
    127. QPixmap Caliper::mainPix() const
    128. {
    129. QPixmap result(":/imgs/mainBody2.png");
    130. QPixmap copy = result;
    131. copy.setMask(copy.mask());
    132. return result;
    133. }
    134.  
    135. QPixmap Caliper::piecePix() const
    136. {
    137. QPixmap result(":/imgs/pieceBody.png");
    138. return result;
    139. }
    140.  
    141. void Caliper::paintMove(QPainter &painter)
    142. {
    143. painter.drawPixmap(movePt, movePix());
    144. }
    145.  
    146. void Caliper::paintMain(QPainter &painter)
    147. {
    148. painter.drawPixmap(mainPt, mainPix());
    149.  
    150. }
    151.  
    152. void Caliper::paintPiece(QPainter &painter)
    153. {
    154. painter.drawPixmap(piecePt, piecePix().scaled((movePix().rect().x()+movePix().rect().width() ) - mainPix().rect().width(),
    155. piecePix().rect().height(), Qt::IgnoreAspectRatio));
    156.  
    157. }
    158.  
    159. QSize Caliper::sizeHint() const
    160. {
    161. return QSize(400,300);
    162. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 8th August 2006 at 20:14. Reason: splitted too long line

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pixmap onmousemove event help

    Everything looks OK, what happens when you change Caliper::mouseMoveEvent like this:
    Qt Code:
    1. void Caliper::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. qDebug( __PRETTY_FUNCTION__ );
    4. if (event->buttons() == Qt::LeftButton)
    5. {
    6. qDebug( "\tinside if" );
    7. ...
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    (make sure you start your application from the console)?

  5. The following user says thank you to jacek for this useful post:

    bluesguy82 (8th August 2006)

  6. #5
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: pixmap onmousemove event help

    i get the "inside if" output. thank you for that debug, i was using QMessageBox(s) for debugging.. what a hassle <-- NEWB


    i updated the Caliper:nMouseMove to:

    Qt Code:
    1. void Caliper::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. if (event->buttons() == Qt::LeftButton)
    4. {
    5.  
    6. if (movePix().rect().contains( event->globalPos() - frameGeometry().topLeft() ) )
    7. {
    8. qDebug( __PRETTY_FUNCTION__ );
    9. qDebug("\tinside move");
    10. movePix().rect().moveRight( (event->globalPos().x() - frameGeometry().topLeft().x()) - movePix().rect().x() );
    11. update();
    12. event->accept();
    13. // setDistance(event->globalPos().x() - frameGeometry().topLeft().x());
    14. }
    15. else if (mainPix().rect().contains( event->globalPos() - frameGeometry().topLeft() ) )
    16. {
    17. qDebug( __PRETTY_FUNCTION__ );
    18. qDebug("\tinside main");
    19. move(event->globalPos() - dragPosition);
    20. event->accept();
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    i get inside move to print out, but not inside main

    however still the moveRight func doesn't work
    Last edited by bluesguy82; 8th August 2006 at 20:53.

  7. #6
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: pixmap onmousemove event help

    Thanks for your help. I solved it. In my paint function I was not updating the point function for the repaint. Thanks for the debug info though, that helps a lot!!!

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pixmap onmousemove event help

    Quote Originally Posted by bluesguy82
    event->globalPos() - frameGeometry().topLeft()
    event->globalPos().x() - frameGeometry().topLeft().x()
    Wouldn't it be easier to use just "event->pos()" and "event->pos().x()"?

  9. #8
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: pixmap onmousemove event help

    you again are correct. i updated all. thanks again. it ain't easy bein a newbie c++/qt guy



    EDIT:

    Any chance you know how when I am resizing this I can fire off a window resizeEvent in case the dragging needs to extend outside the window?
    Last edited by bluesguy82; 8th August 2006 at 22:07.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pixmap onmousemove event help

    Quote Originally Posted by bluesguy82
    Any chance you know how when I am resizing this I can fire off a window resizeEvent in case the dragging needs to extend outside the window?
    Try invoking QWidget::resize() --- it will post a resize event.

  11. #10
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: pixmap onmousemove event help

    I suppose that means that I will have to have QWidget:resizeEvent(QResizeEvent *event) routine?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pixmap onmousemove event help

    Quote Originally Posted by bluesguy82
    I suppose that means that I will have to have QWidget:resizeEvent(QResizeEvent *event) routine?
    You'll need that only when you want to react to widget size changes, but you don't need it to change the size.

  13. #12
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: pixmap onmousemove event help

    i am not sure why my resize is not working. at the end of the paint event i call

    Qt Code:
    1. resize(int width, int height); // desired width/height of the new window based on the redrawn pixmap widths
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Invalid pixmap
    By munna in forum Newbie
    Replies: 2
    Last Post: 8th June 2006, 09:00
  2. Paint XP radio button to pixmap
    By Ben.Hines in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2006, 22:15
  3. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 22:55
  4. Replies: 4
    Last Post: 23rd January 2006, 17:51
  5. What's the relationship between signal/slot and event?
    By twosnowman in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 18:13

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.