PDA

View Full Version : Drawing a Rubberband



daviddoria
3rd May 2008, 16:34
Clearly this is not useful, i need to do this on mouse events, but as a demo I thought I would just draw a static rectangle. Surprisingly I see no rectangle! Where have I gone wrong?


//compile and link with
// g++ main.cpp -o ss -I/usr/include/QtGui -lQtGui

#include <QWidget>
#include <QDesktopWidget>
#include <QApplication>
#include <QPixmap>
#include <QRubberBand>
#include <iostream>

using namespace std;

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();

//pause
int a;
cin >> a;

return 0;
}



Thanks!
Dave

bunjee
3rd May 2008, 16:38
Where do you "exec()" your application ?

daviddoria
3rd May 2008, 16:51
I don't want an application - i just want to use the rubberband :)

I did something similar for a screenshot - and that seems to work fine:



//compile and link with
// g++ main.cpp -o ss -I/usr/include/QtGui -lQtGui

#include <QWidget>
#include <QDesktopWidget>
#include <QApplication>
#include <QPixmap>
#include <QRubberBand>
#include <iostream>

using namespace std;

void shootScreen(char* fileName);

int main(int argc, char *argv[])
{
QApplication MyScreenshot(argc,argv);
shootScreen("test.png");

return 0;
}

void shootScreen(char* fileName)
{
QPixmap originalPixmap;
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), 100, 500, 200 , 50);//x, y, width, height
originalPixmap.save(fileName);
}

EnErGy[CSDX]
3rd May 2008, 17:34
QRubberBand inherit QWidget sow you should call exec() to process application events, paint event at least

daviddoria
3rd May 2008, 18:21
ok cool, that drew the static rectangle!
now to handle mouse events - i was trying to use these functions (below) but they don't seem to catch the events?


// 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 <iostream>

using namespace std;

void mouseMoveEvent( QMouseEvent * e);
void mousePressEvent(QMouseEvent *event);

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 mouseMoveEvent( QMouseEvent * e)
{
cout << "Mouse moved!" << endl;
}

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

bunjee
3rd May 2008, 18:55
Take a look here: http://doc.trolltech.com/4.3/eventsandfilters.html#event-handlers

EnErGy[CSDX]
3rd May 2008, 18:55
in sample above you have only one widget, you can try catch event to this widget

daviddoria
3rd May 2008, 19:11
So what did I do wrong? It seems that I overloaded the function as it said to in that link? I guess I haven't attached it properly? But all the examples have a big class and stuff, I just want this small function - can I not do it like this?

bunjee
3rd May 2008, 19:32
That's not how QT works.

Qt has a widget per widget approach,

So you have two options :

- Connect to a "clicked" signal (when your widget has one, which is not the case of QRubberBand).

- Reimplement your own widget, and subclass the event functions.

In your case I would create a class screenshotWidget and reimplement events.
But I suggest you take a look at examples and tutorials in the Qt documentation.

daviddoria
4th May 2008, 15:32
so a "widget" Is like a "form" in VB? (a graphical window?) Is there no way to catch mouse events without having anything graphical? I just want to select a region of the screen during a c++ program - is using Qt not the right way to go?