Is it possible to find out the position relative to the QScrollbarArea and then jump to the item? In the example code, 50 labels are generated, as soon as I press the "Test" button I want to jump to a specific label.
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import *
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.scroll = QScrollArea()
self.widget = QWidget()
self.vbox = QVBoxLayout()
for i in range(0,50):
lbl = QLabel("TextLabel_"+str(i))
self.vbox.addWidget(lbl)
btn = QPushButton("Test")
btn.clicked.connect(self.jumpToItem)
self.vbox.addWidget(btn)
self.widget.setLayout(self.vbox)
#Scroll Area Properties
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.setCentralWidget(self.scroll)
self.setGeometry(0, 0, 100, 200)
self.show()
def jumpToItem(self, value):
print("Jump to TextLabel_12")
def main():
app = QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()