Thanks again jpn. I had forgotten QPoint and I made the change. QRubberBand was declared correctly all along. Nevertheless the code won't work (compains about origin and mouseReleaseEvent). I pasted the current code again - in case you have a minute. Thanks a lot.
#include <QApplication>
#include <QtGui>
#include "tom.h"
{
origin.setX(0);
origin.setY(0);
pm.load("image.bmp");
m_pixmapLabel->setPixmap(pm);
mainLayout->addWidget(m_pixmapLabel);
setLayout(mainLayout);
}
{
rubberBand
->setGeometry
(QRect(origin, event
->pos
()).
normalized());
}
{
origin = event->pos();
rubberBand->show();
}
{
rubberBand->hide();
}
int main(int argc, char *argv[])
{
MyWidget window;
window.show();
return application.exec();
}
#include <QApplication>
#include <QtGui>
#include "tom.h"
MyWidget::MyWidget(QWidget* parent): QWidget(parent)
{
m_pixmapLabel = new QLabel;
origin = new QPoint;
origin.setX(0);
origin.setY(0);
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
QVBoxLayout* mainLayout = new QVBoxLayout;
QPixmap pm;
pm.load("image.bmp");
m_pixmapLabel->setPixmap(pm);
mainLayout->addWidget(m_pixmapLabel);
setLayout(mainLayout);
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}
void MyWidget::mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();
}
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
MyWidget window;
window.show();
return application.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef TOM_H
#define TOM_H
#include <QtGui>
{
Q_OBJECT
public:
protected:
private:
};
#endif
#ifndef TOM_H
#define TOM_H
#include <QtGui>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget (QWidget* parent = 0);
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event)
private:
QLabel* m_pixmapLabel;
QRubberBand* rubberBand;
QPoint origin;
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks