PDA

View Full Version : pixmap onmousemove event help



bluesguy82
8th August 2006, 19:04
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 :

dragPosition = event->globalPos() - frameGeometry().topLeft()
and

event->accept()


in the onmousemove event:
i check to see if the

pixmap.rect().contains(dragposition)
and then i do

pixmap.rect().moveCenter(dragPosition.x())
then finally i do:

update(pixmap.rect())
event->accept()


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.

jacek
8th August 2006, 19:06
QWidget::setMouseTracking()

bluesguy82
8th August 2006, 19:10
sorry i forgot to mention that i did that already. below is the src code



#include <QtGui>

#include <stdlib.h>

#include "caliper.h"

Caliper::Caliper(QWidget *parent)
: QWidget(parent)
{
currentDistance = 0;
currentAngle = 0;
rotating = false;
moving = false;
mainPt = QPointF(0.0, 0.0);
piecePt = QPointF(mainPix().rect().width(),mainPix().rect(). height()/3.38);
movePt = QPointF(mainPix().rect().width() + piecePix().rect().width(), -2.0);

setMouseTracking(true);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setWindowTitle(tr("Caliper Widget"));
}

void Caliper::setAngle(int angle)
{
if (currentAngle == angle)
return;
currentAngle = angle;
update(movePix().rect());
emit angleChanged(currentAngle);
}

void Caliper::setDistance(int distance)
{
if (currentDistance == distance)
return;
currentDistance = distance;
emit distanceChanged(currentDistance);
}


void Caliper::startRotate()
{
if (isRotating())
return;
else rotating=true;
}

bool Caliper::isMoving() const
{
return moving;
}

bool Caliper:: isRotating() const
{
return rotating;
}

void Caliper::startMeasure()
{
if (isMoving())
return;
else moving = true;
}

void Caliper::moveMeasurer()
{
movePix().rect().moveRight(movePix().rect().x() + currentDistance);
update();
}

void Caliper::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}

void Caliper::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
if (movePix().rect().contains( event->globalPos() - frameGeometry().topLeft()) )
{
movePix().rect().moveRight( dragPosition.x() - movePix().rect().x() );
update();
event->accept();
// setDistance(event->globalPos().x() - frameGeometry().topLeft().x());
}
else if (mainPix().rect().contains( dragPosition ) )
{
move(event->globalPos() - dragPosition);
event->accept();
}
}
}

void Caliper::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
event->accept();
return;
}
}

void Caliper::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);

paintMove(painter);
paintMain(painter);
paintPiece(painter);

fullPix = mainPix().rect().unite(movePix().rect().unite(piec ePix().rect()));
//QRegion maskedRegion(fullPix, QRegion::Rectangle);
//setMask(maskedRegion);
}

QPixmap Caliper::movePix() const
{
QPixmap result(":/imgs/moveBody.png");
return result;
}

QPixmap Caliper::mainPix() const
{
QPixmap result(":/imgs/mainBody2.png");
QPixmap copy = result;
copy.setMask(copy.mask());
return result;
}

QPixmap Caliper::piecePix() const
{
QPixmap result(":/imgs/pieceBody.png");
return result;
}

void Caliper::paintMove(QPainter &painter)
{
painter.drawPixmap(movePt, movePix());
}

void Caliper::paintMain(QPainter &painter)
{
painter.drawPixmap(mainPt, mainPix());

}

void Caliper::paintPiece(QPainter &painter)
{
painter.drawPixmap(piecePt, piecePix().scaled((movePix().rect().x()+movePix(). rect().width() ) - mainPix().rect().width(),
piecePix().rect().height(), Qt::IgnoreAspectRatio));

}

QSize Caliper::sizeHint() const
{
return QSize(400,300);
}

jacek
8th August 2006, 19:26
Everything looks OK, what happens when you change Caliper::mouseMoveEvent like this:
void Caliper::mouseMoveEvent(QMouseEvent *event)
{
qDebug( __PRETTY_FUNCTION__ );
if (event->buttons() == Qt::LeftButton)
{
qDebug( "\tinside if" );
...
}
}
(make sure you start your application from the console)?

bluesguy82
8th August 2006, 19:35
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::onMouseMove to:


void Caliper::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{

if (movePix().rect().contains( event->globalPos() - frameGeometry().topLeft() ) )
{
qDebug( __PRETTY_FUNCTION__ );
qDebug("\tinside move");
movePix().rect().moveRight( (event->globalPos().x() - frameGeometry().topLeft().x()) - movePix().rect().x() );
update();
event->accept();
// setDistance(event->globalPos().x() - frameGeometry().topLeft().x());
}
else if (mainPix().rect().contains( event->globalPos() - frameGeometry().topLeft() ) )
{
qDebug( __PRETTY_FUNCTION__ );
qDebug("\tinside main");
move(event->globalPos() - dragPosition);
event->accept();
}
}
}

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

however still the moveRight func doesn't work

bluesguy82
8th August 2006, 20:24
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!!!

jacek
8th August 2006, 20:47
event->globalPos() - frameGeometry().topLeft()
event->globalPos().x() - frameGeometry().topLeft().x()
Wouldn't it be easier to use just "event->pos()" and "event->pos().x()"?

bluesguy82
8th August 2006, 20:52
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?

jacek
8th August 2006, 21:11
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.

bluesguy82
8th August 2006, 21:42
I suppose that means that I will have to have QWidget:resizeEvent(QResizeEvent *event) routine?

jacek
8th August 2006, 22:03
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.

bluesguy82
9th August 2006, 14:15
i am not sure why my resize is not working. at the end of the paint event i call



resize(int width, int height); // desired width/height of the new window based on the redrawn pixmap widths