Solved QLabel snap to ScrollBar
-
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. -
I need to bind the button to a specific area on the image. Please tell me how to do it?
-
@Mikeeeeee Do you actually read documentation?
https://doc.qt.io/qt-5/qwidget.html#move-1 -
@mrjj said in QLabel snap to ScrollBar:
Right clik on the class name
OMG, this is an option in QtC ?
I feel so stupid now....
-
Do so. But tied to a Qlabel , and not to the Image. Is it possible to link to an Image?
QImage mapImage(":/Images/Images/mapMain.png"); ImageViewer* myImageViewer = new ImageViewer(); myImageViewer->setImage(mapImage); ui->verticalLayout->addWidget(myImageViewer); QPushButton* btn1 = new QPushButton(myImageViewer); btn1->setFixedSize(20,20); btn1->setText("*"); btn1->move(20,20);
-
@Mikeeeeee said in QLabel snap to ScrollBar:
QPushButton* btn1 = new QPushButton(myImageViewer);
btn1->setFixedSize(20,20);
btn1->setText("*");
btn1->move(20,20);You forgot
btn1->show();
-
How do I know the visible coordinates of an Image?
-
@Mikeeeeee said in QLabel snap to ScrollBar:
How do I know the visible coordinates of an Image?
What do you mean?
What are "visible coordinates" and relative to what do you want to have them?
Do you mean you want to know which part of displayed image is visible? -
Yes. For example, I see the Central 50% of the image? How do I know I'm seeing an image from 25% to 75%?
-
Partially solve. But regardless of the scrollbar state, pageStep() returns a single value
qDebug()<<scrollArea->horizontalScrollBar()->value() <<scrollArea->horizontalScrollBar()->maximum() <<scrollArea->horizontalScrollBar()->minimum() <<scrollArea->horizontalScrollBar()->pageStep();
-
@Mikeeeeee said in QLabel snap to ScrollBar:
pageStep
But it is a single step value. how much to scroll for a page.
What do you need it for ? -
I wanted to know the width of the scroll and scrollbar and calculate the visible part.
-
Need to know the size of the scroll and its position and determine.
Scroll size = (widget size/picture size)/zoom -
How can I remove the menu bar? If I remove createActions(); from the constructor, the program does not start.
-
Hi
If i do
// createActions();
it starts fine.
Please provide more details.