PDA

View Full Version : Mouse Events



daviddoria
4th May 2008, 21:00
I am trying to draw a rectangle anywhere on the screen when the user drags the mouse - as an easier problem I was trying to draw a static rectangle when the mouse is either moved or clicked. Here's what I came up with by following the examples/tutorials, but it doesn't do anything??



// g++ rubber.cpp -o ss -lQtGui

#include <QtGui/QWidget>
#include <QtGui/QDesktopWidget>
#include <QtGui/QApplication>
#include <QtGui/QPixmap>
#include <QtGui/QRubberBand>
#include <QtGui/QMouseEvent>
#include <QtGui/QPushButton>
#include <QtGui/QFont>

#include <iostream>

using namespace std;

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
protected:
void mouseMoveEvent( QMouseEvent * e);
void mousePressEvent(QMouseEvent *event);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);

QPushButton *quit = new QPushButton(tr("Quit"), this);
quit->setGeometry(62, 40, 75, 30);

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}



int main(int argc, char *argv[])
{
QApplication MyScreenshot(argc,argv);

/*
QPoint TopLeft(100,100);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);

QRubberBand outline (QRubberBand::Rectangle);
outline.setGeometry(SelectionRectangle);
outline.show();
*/
return MyScreenshot.exec();
}

void MyWidget::mouseMoveEvent( QMouseEvent * e)
{
cout << "Mouse moved!" << endl;

QPoint TopLeft(100,100);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);

QRubberBand outline (QRubberBand::Rectangle);
outline.setGeometry(SelectionRectangle);
outline.show();

}

void MyWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
cout << "Left click!" << endl;
}

QPoint TopLeft(100,100);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);

QRubberBand outline (QRubberBand::Rectangle);
outline.setGeometry(SelectionRectangle);
outline.show();

}

aamer4yu
4th May 2008, 21:48
You are making multiple QRubberBane objects in mousemove and mouse press events.
Cant say how wil they work. What you need is one QRubberband object in the widget. On mouse press event just show the rubberband with some smallest size. On mousemove event, set the geometry of the rubberband only.

If you just want to show static rectangle, u cud keep a variable for mousepressed. In the paintevent of the widget, check that variable and draw a rectangle. something like -


paintEvent()
{
QPainter painter(this);
painter.drawRectangle();
}


I hope you got the idea :)

wysota
4th May 2008, 21:50
The rubber band is a local object that gets destroyed one the flow leaves the mousePressEvent method. Anyway if you want to do region selection for a screenshot program, it might be simpler to grab the pixmap of the whole desktop, show it over the real desktop and then do the selection.

daviddoria
4th May 2008, 22:17
Do I have to create an instance of MyWidget (as shown in main() below)? If so, then how do I access the "outline" object in the widget from the mouse event function?



// g++ rubber.cpp -o ss -lQtGui

#include <QtGui/QWidget>
#include <QtGui/QDesktopWidget>
#include <QtGui/QApplication>
#include <QtGui/QPixmap>
#include <QtGui/QRubberBand>
#include <QtGui/QMouseEvent>
#include <QtGui/QPushButton>
#include <QtGui/QFont>

#include <iostream>

using namespace std;

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
QRubberBand outline (QRubberBand::Rectangle);
protected:
void mousePressEvent(QMouseEvent *event);

};

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{

}

int main(int argc, char *argv[])
{
QApplication MyScreenshot(argc,argv);
QPoint TopLeft(100,100);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);

MyWidget A();
A.outline.setGeometry(SelectionRectangle);
A.outline.show();

return MyScreenshot.exec();
}

void MyWidget::mousePressEvent(QMouseEvent *event)
{
//move the rectangle
QPoint TopLeft(100, 200);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);

A.outline.setGeometry(SelectionRectangle); //this doesn't work because A is not declared in this scope
}

wysota
5th May 2008, 06:56
Skip the "A." part in the event handler - you are already in the "A" object.

daviddoria
5th May 2008, 12:43
haha oh yea of course. then how about this:

in the mouse event function:
error: ‘((MyWidget*)this)->MyWidget::outline’ does not have class type




// g++ rubber.cpp -o ss -lQtGui

#include <QtGui/QWidget>
#include <QtGui/QDesktopWidget>
#include <QtGui/QApplication>
#include <QtGui/QPixmap>
#include <QtGui/QRubberBand>
#include <QtGui/QMouseEvent>
#include <QtGui/QPushButton>
#include <QtGui/QFont>
#include <QtGui/QPainter>

#include <iostream>

using namespace std;

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
QRubberBand outline();

protected:
void mousePressEvent(QMouseEvent *event);

};

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{

}

int main(int argc, char *argv[])
{
QApplication MyScreenshot(argc,argv);
QPoint TopLeft(100,100);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);

MyWidget A();
return MyScreenshot.exec();
}

void MyWidget::mousePressEvent(QMouseEvent *event)
{

QPoint TopLeft(100, 200);
QPoint BottomRight(200,200);
QRect SelectionRectangle(TopLeft, BottomRight);
outline.setGeometry(SelectionRectangle);
outline.show();
}

jpn
13th May 2008, 11:55
g++ rubber.cpp -o ss -lQtGui
You should use qmake to generate a Makefile. Please, take a look at Qt Tutorial 1 (http://doc.trolltech.com/4.4/tutorials-tutorial-t1.html) (pay special attention to the qmake commands in the end).