QScollArea doesn't autoscroll when dragging inside it - qt

I have some widgets inside a QScrollArea and I'm dragging between these widgets, the problem is that the QScrollArea doesn't scroll when I'm dragging inside it, so if I want to drag between a widget A to the widget B and the widget B is not visible on the viewport, the QScrollArea doesn't automatically scroll when the mouse moves to the edge of the viewport.

Subclass your scroll area, and add in one of the functions below.
http://qt-project.org/doc/qt-4.8/qwidget.html#mousePressEvent
http://qt-project.org/doc/qt-4.8/qwidget.html#dragMoveEvent
http://qt-project.org/doc/qt-4.8/qwidget.html#dragLeaveEvent // Probably just need this one
When the dragMoveEvent reaches the edge of your widget, or when the dragLeaveEvent happens, detect which edge it is, or left at, and then scroll your area in that direction.
Hope that helps.

Related

Floating QDockWidget appears not on the same screen as the main window when dragged

I'm completely stuck with the following problem. When I drag a dock widget out of the main window, which is placed on the second screen, the floating dock appears on the first screen. When I click on the float button, it appears undocked correctly, i.e. near the main window, but only if I didn't drag it out before. If I did, docked it after and then clicked the float button, it appears detached on the first screen as well.
When I do all this on the first screen, it works fine.
First I thought I could force the dock widget to appear where I want it to by using something like this:
connect(dock, &QDockWidget::topLevelChanged, [this, dock](bool) {
dock->setGeometry(this->x(), this->y(), 200, 500);
});
It solved buggy floating button behavior, but didn't help with dragging, so my dock widget appears 200x500 px on the first screen again.
I also don't see any possibility to subclass and override anything here.
Is there a way to make the dock widget appear on the screen where the main window is, when it's detached by dragging?

Scrolling QTableWidget smoothly

I have a QTableWidget with custom Widgets. These widgets are big and fill almost the whole scroll area in height, so that only one row is visible.
I can scroll with the mouse wheel or by dragging the scroll bar, but the row always jumps.
Is there a way to configure the QTableWidget to smoothly scroll, without jumping?
Try to use this:
view->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel)
From Qt documentation:
enum ScrollMode { ScrollPerItem, ScrollPerPixel }
verticalScrollMode : ScrollMode
This property holds how the view scrolls its contents in the vertical
direction.
This property controls how the view scroll its contents vertically.
Scrolling can be done either per pixel or per item.

QScrollArea - how to do precise programmatic scrolling

I am using Qt to build a view for multipage documents. I'm drawing each page to a separate QLabel widget, like in the ImageViewer example app.
The QLabels are organized vertically using QVBoxLayout. This all works nicely, with a little grey margin between the pages.
What I want now is, when the user does page down, to move the scroll so that the top of a particular QLabel appears right at the top of the window. the "ensure" functions might do that, but I'm not immediately seeing how.
Has anyone done something like this?
If a child widget is taller that the viewport height ensureWidgetVisible scrolls to the middle of the widget.
If you need to scroll to the top of the widget you can do it easily with a little calculation:
//childWidget - QLabel you want to move to
//area - QScrollArea
// calculate childWidget position in coordinates of the viewport
const QPoint p = childWidget->mapTo(area, QPoint(0,0));
// move scroll bar
area->verticalScrollBar()->setValue(p.y() + area->verticalScrollBar()->value());

Orphan QScrollArea remains after adding to layout

I'm having trouble adding a QScrollArea to a layout within a QDialog widget. When I add the scroll area to the layout, it appears where I want it with the widget I've assigned to it and it scrolls just fine. However, I am also left with a blank scroll area stuck to the top left of my dialog, as if I hadn't added the scroll area to the layout at all!
Why is this happening?

drag and drop in Canvas

I'm doing an application in flex where I draw different sprites inside a canvas. Depending of the dimensions, scrollbars can be appear. I would like to move the "image" with the movement of the mouse as you can see at the Adobe Reader when you are reading a document with zoom (hand mouse icon). In this way, you dont have to touch the scrollbar.
I'm start trying with drag and drop properties of the canvas, setting the position of the scrollbar according with the movement of the mouse but that is not as I expect.
Any ideas or suggestions?
Thanks in advance.
Recipe:
Listen for the mouseMove (MouseEvent.MOUSE_MOVE) event on the canvas
In event listener, examine event.localX, event.localY
Based on those values and the canvas's width and height, set the canvas's horizontalScrollPosition and verticalScrollPosition accordingly
Hope this helps.
What you can do is, change the position of the scrollbar , when the mouse reaches the last 20 pixels on the left or the right, or the top and bottom.
What you would really need to do is, have a mouseMove listener on the whole application, and when the mouse is within the end ranges of any of the sides, you can use the scroll.scrollTo function to move the scrollbars.

Resources