PDA

View Full Version : QAbstractScrollArea and paintEvent



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

wysota
28th July 2008, 15:04
Do you do anything with the viewport of the scrollarea?

babu198649
28th July 2008, 15:11
no ,if i substitue viewport() with this then i get "QPainter::begin: Widget painting can only begin as a result of a paintEvent" this message.

wysota
28th July 2008, 16:03
But you had to setup the viewport somehow in the first place... otherwise scrollbars wouldn't work. Do they?

babu198649
28th July 2008, 16:07
i have not tested it yet,but now i solved the problem.Instead of calling update(), calling viewport()->update() works fine.

babu198649
28th July 2008, 16:13
But you had to setup the viewport somehow in the first place... otherwise scrollbars wouldn't work. Do they?

yes thats true .i only want to show a pixmap on the viewport ,how to get the scrollbars .if i need the scrollbars should i have to paint on the viewport widget.

wysota
28th July 2008, 16:21
If you don't want the scrollbars, why do you use QAbstractScrollArea instead of QWidget?

babu198649
28th July 2008, 17:41
of course i want scrollbars,i am making a image viewer with scrollbars, if i need the scrollbars should i have to paint on the viewport widget.

wysota
28th July 2008, 18:46
So why not put a QLabel inside QScrollArea and forget about the whole problem?

As for the current situation scrollbars will only appear if you declare the viewport size larger than the scrollarea size. Currently you didn't declare anything so the viewport size is practically undetermined. Unless you implement scrollbar handling yourself (setting the sizes and handling moving them) you won't be able to use them.

wysota
28th July 2008, 18:47
So why not put a QLabel inside QScrollArea and forget about the whole problem?

As for the current situation scrollbars will only appear if you declare the viewport size larger than the scrollarea size. Currently you didn't declare anything so the viewport size is practically undetermined. Unless you implement scrollbar handling yourself (setting the sizes and handling moving them) you won't be able to use them.

babu198649
29th July 2008, 08:22
So why not put a QLabel inside QScrollArea and forget about the whole problem?

if i need to have zoom and image moving features(through wheel event and mouse move event) ,should i have to reimplement the QLabel.

wysota
29th July 2008, 10:33
No, you just set the zoomed pixmap to the label. Panning should work out of the box.

babu198649
2nd August 2008, 13:52
So why not put a QLabel inside QScrollArea and forget about the whole problem?
i am following the same .I need rubberband zooming .Is it possible with QScrollArea.

wysota
4th August 2008, 11:56
Yes, of course. Use QRubberBand and calculate the zoom rectangle based on the position of the viewport.