Drawing on an image which is inside a QScrollArea ?
Hello there, If I subclass QLabel and I add a QLabel directly to my QDialog, it works fine. If I add this label inside a ScrollArea, the thing I'm drawing doesn't show unless I resize the dialog itself. Yes, weird.
I've setup compilable example code that indicates what the problem is. What I am trying to do is to select an area of an image with my mouse, by drawing a rectangle on the corresponding area. The images my program is designed to work with can be very large, and thus, I need to have a scroll area so as the dialog to stay at a logical dimension, and not to fill the entire screen (or even multiple workspaces, if we are talking about a linux machine with multiple desktops).
Everything works fine, except that the drawing (selection-rectangle) isn't visible unless the dialog is resized - manually. I think I have to update something while drawing, but I'm not sure what. Well, here's the example code: http://paste.ubuntu.com/1151553/
Another issues that I don't know how to solve (and I want your suggestions there) are
(1) when the user is selecting an area, how to set it to automatically scroll when the user actually selects an area by pushing against to a wall of the scroll area (I guess I am understandable here).
(2) is there a way to let the user select a rectangle and then, when he left-clicks on a position with holding down the [Shift] button, the bottom right edge of his previous selection to actually go through the point he clicks at?
Thanks in advance for any help.
When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area.
If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will cause the size of the scroll area to be updated whenever the contents of the layout changes.
the question 1, you can consider call the QWidget::grabMouse () to receive mouse event and respond.
Thanks henryxuv for your post. I am more of a designer's (working with .uis) guy and I don't do very well with manually writing down the layouts. So, yes, you are right as it seems, the documentation is clear: http://doc.qt.nokia.com/4.7-snapshot/layout.html
What I don't understand is what to actually do with the layout. Should I make a new layout, add as a widget to it my image_label, set the layout's size the same as the width and height of image_label and then set this layout to my scrollArea? If yes, then what type of layout (QVBoxLayout, QHBoxLayout or what?)?
What do you mean by setting to the child's (image_label) size hint? Do you mean size policy?
Also, grabbing the mouse has to be done via my image_label or via the scrollArea (I assume the latter, because this way I will be able to detect easier if it is close to the walls of the scrollArea?)
Thanks for any answers :/
Just call your label's setMinimumSize() is ok.
Thanks, but this doesn't seem to work in any way? Did you see my code? If I add image_label->setMinimumSize(img.width(), img.height()) still nothing happens! I think, according to the documentation, that I need to place inside my ScrollArea a kind of a layout and on top of this layout my image_label !
I have debug your code. Your slot the signal in the wrong way. who would receive the mousePressEvent and moueReleaseEvent . That is absolutely not the Dialog, It should be CropLabel. If you haven't solve it, give me your email, I can send my debugged code to you.
The code is fine :) I just needed to call, along with repaint() the image_label->update() !
Ok, fine. I just think the better way is handle your mouseMove signal in the CropLabel ,not your dialog.