我需要从SQL表中分页数据。
我在PyQT或Pyside中找不到任何有关分页的信息。你能帮我一下吗?
发布于 2022-07-22 07:50:07
处理分页的一种方法是使用QStackedWidget
来模拟页面本身,使用常规的QLabel
作为页面链接。然后,您可以覆盖每个标签( mousePressEvent
),向堆叠小部件发出一个信号,将当前索引设置为单击的标签中的页码。
下面是使用PySide6演示所建议的策略的最小可重现性示例。
import sys
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
class PageLink(QLabel):
clicked = Signal([str]) # Signal emited when label is clicked
def __init__(self, text, parent=None):
super().__init__(text, parent=parent)
self.setTextInteractionFlags(Qt.LinksAccessibleByMouse)
self.setStyleSheet("color: blue;") # set text color to blue to emulate a link
self.setCursor(Qt.PointingHandCursor) # set the cursor to link pointer
def mousePressEvent(self, event):
self.clicked.emit(self.text()) # emit the clicked signal when pressed
return super().mousePressEvent(event)
class MainWindow(QMainWindow):
def __init__(self, parent=None) -> None:
"""Main Window Setup"""
super().__init__(parent=parent)
self.setWindowTitle("Pagination Demonstration")
self.resize(600,400)
self.central = QWidget(parent=self)
self.layout = QVBoxLayout(self.central)
self.setCentralWidget(self.central)
# create the stacked widget that will contain each page...
self.stackWidget = QStackedWidget(parent=self)
self.layout.addWidget(self.stackWidget)
# setup the layout for the page numbers below the stacked widget
self.pagination_layout = QHBoxLayout()
self.pagination_layout.addStretch(0)
self.pagination_layout.addWidget(QLabel("<"))
# create pages and corresponding labels
for i in range(1, 6):
page_link = PageLink(str(i), parent=self)
self.pagination_layout.addWidget(page_link)
page = QWidget()
layout = QVBoxLayout(page)
layout.addWidget(QLabel(f"This is page number {i} of 5"))
self.stackWidget.addWidget(page)
page_link.clicked.connect(self.switch_page)
self.pagination_layout.addWidget(QLabel(">"))
self.layout.addLayout(self.pagination_layout)
def switch_page(self, page):
self.stackWidget.setCurrentIndex(int(page) - 1)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
https://stackoverflow.com/questions/73076015
复制相似问题