babu198649
28th July 2008, 14:20
hi
In the below code the pixmap is moved(mouse click and move) only when the window is resized ,where is the problem.ie.the postion of pixmap is updated only when the window is resized . If i inherit from the QWidget there is no problem.
#include <QtGui>
class Widget : public QAbstractScrollArea
{
private:
QPixmap pixmap;
QPoint mouse_press;
QPoint offset;
public:
Widget()
{
offset.setX(0);
offset.setY(0);
pixmap = QPixmap(100, 100);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.drawRect(0, 0, 50, 50);
}
protected:
void paintEvent(QPaintEvent *event)
{
QAbstractScrollArea::paintEvent(event);
if (pixmap.isNull())
return;
QPainter painter(viewport());
painter.drawPixmap(offset, pixmap);
}
void mousePressEvent(QMouseEvent *event)
{
mouse_press=event->pos();
event->ignore();
}
void mouseMoveEvent(QMouseEvent * event)
{
offset=event->pos()-mouse_press+offset;
mouse_press=event->pos();
update();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
In the below code the pixmap is moved(mouse click and move) only when the window is resized ,where is the problem.ie.the postion of pixmap is updated only when the window is resized . If i inherit from the QWidget there is no problem.
#include <QtGui>
class Widget : public QAbstractScrollArea
{
private:
QPixmap pixmap;
QPoint mouse_press;
QPoint offset;
public:
Widget()
{
offset.setX(0);
offset.setY(0);
pixmap = QPixmap(100, 100);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.drawRect(0, 0, 50, 50);
}
protected:
void paintEvent(QPaintEvent *event)
{
QAbstractScrollArea::paintEvent(event);
if (pixmap.isNull())
return;
QPainter painter(viewport());
painter.drawPixmap(offset, pixmap);
}
void mousePressEvent(QMouseEvent *event)
{
mouse_press=event->pos();
event->ignore();
}
void mouseMoveEvent(QMouseEvent * event)
{
offset=event->pos()-mouse_press+offset;
mouse_press=event->pos();
update();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}