Solved QLabel snap to ScrollBar
-
@Mikeeeeee
well its constructor is
ImageViewer();
so it doesn't take a parent. you change that so it can be imbedded into what you like. -
How to set picture? Need a method to move from private to public?
-
@Mikeeeeee
hi
yes you can make setImage public -
I did so:
QImage mapImage(":/Images/Images/mapMain.png"); ImageViewer* myImageViewer = new ImageViewer(); myImageViewer->setImage(mapImage);
I have a label: ui->labelMap
How do I set to myImageViewer to ui->labelMap? -
@Mikeeeeee
You cannot put whole viewer into a Label.
However, you can use myImageViewer instead of
ui->labelMap -
But how to use? How to do this?
-
@Mikeeeeee
well do you have ui->labelMap in layout ?
then just insert myImageViewer into that. -
How to do this? I created ui->label Map in the designer.
-
@Mikeeeeee
but is it a layout? -
I appent ui->label in the ui->gridLayout_2
-
Also I have an empty ui->verticalLayout
-
@Mikeeeeee said in QLabel snap to ScrollBar:
ui->gridLayout_2
ok then do
ui->gridLayout_2->addWidget(myImageViewer);
You can delete the label if u wish. -
Cool, works. And how to reconfigure the mouse wheel to zoom in?
-
@Mikeeeeee
Its not a configure option. Its new code :)
You can override (add) wheelEvent ( QWheelEvent * event ) to the
ImageViewer class and change the size of the inner QLabel *imageLabel;
to create a zoom in effect.
Or create your on drawing function like here
https://stackoverflow.com/questions/6650219/zooming-function-on-a-qwidget -
I tried to do so, but it does not work. Please tell me how to set up the signal?
connect(imageLabel, &QWidget:: wheelEvent(), this, &ImageViewer::zoomIn);
-
@Mikeeeeee
Hi
Its not a signal but a virtual function.
You have to override it.Right clik on the class name
Then select
and you get the functionprotected: virtual void wheelEvent(QWheelEvent *event) override { }
Then do same right click again and select
to move the body of function to the cpp file.
Now you can try to implement your zoom.
-
Thanks, did so and zoom works:
void ImageViewer::wheelEvent(QWheelEvent *event) { zoomIn(); }
But how to add a slot to the reverse rotation of the mouse wheel?
And when I rotate the mouse wheel verticalScrollBar also receives a signal and moves. How to disable verticalScrollBar from (QWheelEvent *event)? -
@Mikeeeeee said in QLabel snap to ScrollBar:
wheelEvent
Hi
Both up and down comes to wheelEvent
check out.
event->delta()
it changes based on direction.void ImageViewer::wheelEvent(QWheelEvent *event) { qDebug() << " delta" << event->delta(); qDebug() << " delta" << event->angleDelta(); int nDeg = event->delta(); if (nDeg > 0) zoomOut(); else if (nDeg < 0) zoomIn(); event->accept(); }
- How to disable verticalScrollBar from (QWheelEvent *event)?
You can try to hide them.
scrollArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
Else you need to use an event filter on the scrollarea's viewport to eat the
QWheelEvent events so it dont see them.You do need event filer as not to have it scroll funny.
Add this to the class
bool ImageViewer::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::Wheel) { // Don't propagate return true; } // Other events should propagate return false; } and in its constructor, set the filter scrollArea->viewport()->installEventFilter(this);
Then it zooms pretty fine :)
-
Thanks so much. It works. Is it possible to split a QLabel on the coordinates 100*100 and add a small button on top of the QLabel ?
-
@Mikeeeeee said in QLabel snap to ScrollBar:
split a QLabel
What does this mean?
Yes, you can put a button on top of a label.