How to make non-smooth / stepwise scolling in a qgraphicsview - c++

I have a scene which has a basically table-like layout. Thus I'd like it to scroll like a table... one row or column at a time. Specifically, the upper left visible item should have it's upper left corner in the upper left corner of the viewport, unless the scrollbars are at their maximum (in which case it is the bottom/right item that is exactly in view). Pressing an arrow key should display the next row or column in that direction.
Normally that is easily achievable by inheriting QAbstractScrollArea and setting it up as appropriate, but QGraphicsView already does this. Is there someone who can think of a clever method for achieving this effect?

just override QGraphicsScene::keyPressEvent and...
right: move a cells width in positive x
left: move a cells width in negative x
up: move a cells height in negative y
down: move a cells height in positive y
you'll have to disable this movement at the ends of the table, but it sounds a lot simpler that you might have initially thought
EDIT
overload QGraphicsView::scrollContentsBy to handle the scroll bar movement. You could store the dx and dy parameters and only scroll the movement when this value is greater than the size of a cell

Related

Making QGraphicsEllipseItem always on top of all figures

I created small game when player controls the ellipse and while he moves I draw other Rect items on the screen. While I like that they overlay all other existing items, I dont want new items to cover that ellipse. Is there any way to make that ellipse always on top of anything else?
You can use QGraphicsItem::setZValue on your rectangles and ellipse.
For example, upon creating an ellipse item you give it value of 2 and upon creating a rectangle item you give it value of 1. This way ellipse will always overlay any given rectangle.
If you like the way new rectangles overlay existing ones, you can recalculate z values of existing rectangle items upon creating, giving the new one z value of the last created rectangle item incremented by one, while also incrementing z value of ellipse item so it is guaranteed to be more than z value of any given rectangle item.

How can I get the location of a QDockWidget in its current dock area

How can I get the location of a QDockWidget in its current dock area? For example, if I have a dock area split in half with two widgets, how can I query the framework to find out which one is on top and which one is on bottom? The user can, of course, be dragging and dropping QDockWidgets dynamically.
You can determine which dock is above the other by checking the value of both of their geometries.
if you have two docks,
QDockWidget dock1;
QDockWidget dock2;
then the one on top will have the lesser y() value (since the top left corner of the screen is (0,0) and the y value increases as you move towards the bottom)
if(dock1.geometry().y() < dock2.geometry().y())
{
qDebug() << "Dock 1 is above Dock 2";
}
Left and right can be calculated using the x() value. If the geometries are equivalent, the docks are tabbed, and the one on top can be determined by the value of isVisible().
If you want to recalculate which dock is on top in relation to user input, I would recommend connecting to the QDockWidget::dockLocationChanged signal, since it will be emitted whenever a dock changes dock location, or is moved within its current location, which should cover all the necessary cases.

Reimplement resize event

My widget is frameless because of the sleek looks and so I need to reimplement the resize behaviour.
If I drag the bottom right corner it already works with this code:
void mouseMoveEvent(QMouseEvent *event) {
resize(event->pos().x(), event->pos().y());
}
But what about all the other corners? For example the bottom left corner.
As one expects, it should behave as if the top right corner of the widget would be fixed.
But in the resize function, the upper left corner is fixed, because there is the event.pos()==0.
My idea was to resize the window and then move it, so that it looks as if it would not move, but only change size around the upper left corner. As this leads to flickering and even not the perfect result, is there a better way?
EDIT: Solution:
a) You can define in mousePressEvent:
offset = event->pos();
initialFixedCornerPosX = this->width()+this->pos().x();
initialFixedCornerPosY = this->pos().y();
and in mouseMoveEvent
int x = event->globalX();
int y = event->globalY();
int x_w = offset.x();
int y_w = offset.y();
setGeometry(x-x_w,initialFixedCornerPosY,initialFixedCornerPosX-x+x_w,y-initialFixedCornerPosY);
or
b) in mouseMoveEvent
QRect rect = geometry();
rect.setBottomLeft(event->globalPos());
setGeometry(rect);
To both resize and move the window in one step without flickering, you should use QWidget::setGeometry(QRect) by providing a modified rectangle previously fetched using the corresponding getter function QWidget::geometry().
QRect rect = geometry();
// (then modify...)
setGeometry(rect);
To modify a corner of a QRect, you could either modify each edge or the corners directly. Depending on the rest of your logic, one makes more sense than the other. I'd prefer the second of the following two options:
Example using corners:
If you detect that the user drags the bottom left corner, use
rect.setBottomLeft(event->pos());
However, you of course need to consider edges too, and if you consider corners as separate cases this results in eight cases to be considered in the mouse event.
Example using only edges:
If you detect that the mouse is on the left edge (it might be as well on the top or bottom corner, which are only special cases, so for now we ignore that):
rect.setLeft(event->pos().x());
and if you detect it is on the bottom edge, then
rect.setBottom(event->pos().y());
so if both cases are true, this effectively moves the corner of the rect. So you only need to consider four cases to drag all edges and corners of your window! This assumes that you have a single widget which handles the resize (i.e. your top level widget, which has a margin on the layout to have the children not touch the window edge), and do not add a widget for each corner / edge.

Stop Qt QGraphicsView from scrolling on re-size

I have two QGraphicsView that are of equal width, one ontop the other in a Vertical Layout.
When I re-size my application window, the QGraphicsView on the bottom does what I expect, it remains at the exact position it started at, however the top view begins to move the scene to the right exposing coordinates that are below x=0(essentially blank padding on the left edge of the View), which I do not want, I need both to behave the same because they correspond to each other.
I must have missed something, because should these views behave exactly the same? I need them to align, as the top view has hidden scroll bars and scrolls horizontally by however much the bottom view is scrolled.
Make sure your resizeAnchor is set to NoAnchor and alignment is Qt::AlignLeft | Qt::AlignTop. You may need to try some other combination to work with your situation.

Resize By Mouse

I have a rectangle class that has 2 points, the center axes point and the size of the rectangle. Lets say I want to drag the bottom of the rectangle with the mouse but keep the top of it in the same position. What is the algorithm to find the center position and the new rectangle size based on the mouse? Thanks in advance :)
Move the center in the same direction and half the distance (in either or both X and Y) as the bottom (right-hand corner) was dragged.
I would assume a graphics API is at hand here, what is it? I also assume that you have worked out how to detect that mouse clicking onto the edge of your box, have you decided exactly how that works though? do they just need to click near it, and they then drag the exact corner or what?
I can tell you that you are going to need to log the position of the mouse when they first click and get the differance to where they are now. Half that distance, and then add it to the original centre.
EDIT
oh, for the new size, its the difference of the mouse position added onto the original size. so if the mouse has moved down (increasing y) and left (decreasing x) say 10 units each way, you make the box ten units taller and ten narrower, the centre will be 5 down and 5 left.
It would be easier to tell the difference in the mouse movement, and create a new rectangle that size. Then use the rectangle class to check for the center point. Far easier than offsetting the old center.