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>
{
private:
public:
Widget()
{
offset.setX(0);
offset.setY(0);
pixmap.fill(Qt::transparent);
painter.drawRect(0, 0, 50, 50);
}
protected:
{
if (pixmap.isNull())
return;
painter.drawPixmap(offset, pixmap);
}
{
mouse_press=event->pos();
event->ignore();
}
{
offset=event->pos()-mouse_press+offset;
mouse_press=event->pos();
update();
}
};
int main(int argc, char *argv[])
{
Widget w;
w.show();
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks