Draw whatever you want on a widget and make that widget the child of the scrollarea. Everything else will be taken care by QScrollArea.
Cheers
Draw whatever you want on a widget and make that widget the child of the scrollarea. Everything else will be taken care by QScrollArea.
Cheers
Hi, I've tried to draw on a widget and make this widget the child of the scrollarea but the widget but nothing is painted. The test code is:
FotoAreaEditor::FotoAreaEditor(EditorFotos *parent)
: QScrollArea(parent)
{
foto = new QWidget;
QPainter painter;
painter.begin(foto);
painter.setPen(Qt::blue);
painter.setBrush(Qt:ense1Pattern);
painter.drawPixmap(0, 0, QPixmap("../book.png"));
painter.end();
setWidget(foto);
setBackgroundRole(QPalette:ark);
}
I have subclassed QScrollArea because in the future is very possible that I will have to reimplement paintEvent method. Anybody could explain me why only the scrollarea is shown?
You shouldn't do your painting in the constructor because the next paint event the widget receives will simply overwrite what you have painted because you haven't reimplemented that function. You could try something like:
Qt Code:
FotoAreaEditor::FotoAreaEditor(EditorFotos *parent) { setWidget(foto); } { QPainter painter; painter.begin(viewport()); painter.setPen(Qt::blue); painter.setBrush(Qt:ense1Pattern); painter.end(); }To copy to clipboard, switch view to plain text mode
You could also subclass QWidget instead and put the above painting code directly in the paintEvent of that widget.
Cheers
Hi again, this time the problem is with the scroll area. I have subclassed QWidget class. In the constructor I resize the widget to the size of the Image. After I have reimplemented paintEvent method and with QPainter I paint the Image. The problem is that I can scroll to the bottom of the image because the scoll bars are wrong. Anybody knows why?. I update the viewport of the scrollarea after setWidget but it doesn't work
Sorry in the previous thread when I say: "The problem is that I can scroll ..." I really wanted to say: The problem is that I can't scroll..."
What do you mean with "wrong"? Can you give us a screenshot?
It's nice to be important but it's more important to be nice.
You are not using QScrollArea correctly. You need to follow 1 of two paths
1. Use QScrollArea, you will want to subclass the widget that the QScrollArea is showing, and implement the paintEvent and sizeHint(), the scrollbars will be shown depending on the size of the widget.
2. Use QAbstractScrollArea, with this one, you will want to paint inside the QAbstractScrollArea's paintEvent(with a painter opened on the viewport), you will be responsible for setting the scrollbar page step, range, and value.
Matt
Hi, I have implemented sizeHint() of the widget that the QScrollArea is showing, as the previous post coments, and it returns the size of the image. But the scroll bars are still wrong (when I move them to the bottom, the image is not in the bottom). The only way that I have been successful is making a resize of the widget in it's constructor to the size of the image, and the same resize in the paintEvent. If I don't put any of both, the scrollbars are still wrong. Anybody could explain me why or what am I doing wrong? Thanxs and sorry for my poor english.
Maybe you are setting your sizeHint too late, e.g. loading of image is done after layouting the widget?
It's nice to be important but it's more important to be nice.
The image in the widget is painted in the paintEvent method with qpainter. I dont' understand what you mean with: "setting your sizeHint too late". I only have implemented it, I have to call it anywhere directly??
The QLayout calls the sizeHint at some point, usually directly before QWidget::show is called. The question is wether your sizeHint can give the right size at this point. It could be the wrong size because your image is loaded to late.
I don't understand atm which size hint you reimplement because there is no widget (if you draw the image on your own in paintEvent of QAbstractScrollArea).
It's nice to be important but it's more important to be nice.
Sorry, I don't draw the image in paintEvent of QAbstractScrollArea, I draw it in the paintEvent of the widget.
Bookmarks