pandas使用QGraphicsView自動(dòng)排列項(xiàng)目的實(shí)現(xiàn)
我想使用 QGraphicsView 來編寫一個(gè)資源瀏覽器。它與使用 QGraphicsView 和 QGraphicsItems 的例子略有不同,因?yàn)槲抑幌M幸粋€(gè)滾動(dòng)條,并且我希望項(xiàng)目在視區(qū)大小更改時(shí)能夠自動(dòng)移動(dòng)。例如,當(dāng)視區(qū)寬度足夠大以顯示 4 個(gè)資產(chǎn)時(shí),它們應(yīng)該像這樣顯示:
aaaa
aaaa
aa
但是當(dāng)視區(qū)縮小并且只能在一行中容納 3 個(gè)時(shí),它應(yīng)該像這樣顯示:
aaa
aaa
aaa
a
我不想自己移動(dòng)這些資產(chǎn),而是讓圖形視圖管理它們。這可能嗎?
我曾經(jīng)寫過這樣一件事,但使用 QWidget 和 paintEvent,自己繪制所有資產(chǎn)并跟蹤一行中可以顯示多少資產(chǎn)??梢杂?QGraphicsView 更簡單地完成嗎?
解決方案
方法一:使用 QGraphicsFlowLayout
QGraphicsView 支持布局。您需要做的是實(shí)現(xiàn)您自己的布局管理器,繼承自 QGraphicsLayout。
對于您需要的布局,請查看 Qt 的流布局示例。轉(zhuǎn)換該示例將為您提供一個(gè) QGraphicsFlowLayout。將您的 QGraphicsItems 添加到此布局并將您的 QGraphicsView 的布局設(shè)置為該布局,這將完成這項(xiàng)工作。
方法二:使用 QListWidget
聽起來您想要一個(gè)列表,而不是一個(gè)圖形視圖??梢詫⒘斜碓O(shè)置為顯示像您希望的那樣換行的內(nèi)容。請參閱拼圖示例,注意左側(cè)的拼圖塊列表。對于所提出的情況,設(shè)置起來非常簡單。
當(dāng)然,如果您真的想在圖形視圖中實(shí)現(xiàn)它,我想您可以將一個(gè)列表添加到視圖中并在那里使用它。
代碼示例
from PyQt5.QtCore import QRectF
from PyQt5.QtGui import QBrush, QColor, QPen
from PyQt5.QtWidgets import QApplication, QGraphicsItem, QGraphicsRectItem, QGraphicsScene, QGraphicsView, QVBoxLayout, QWidget
class QGraphicsFlowLayout(QGraphicsLayout):
def __init__(self):
super().__init__()
self.item_list = []
def addItem(self, item):
self.item_list.append(item)
def count(self):
return len(self.item_list)
def itemAt(self, index):
return self.item_list[index]
def takeAt(self, index):
item = self.item_list.pop(index)
return item
def boundingRect(self):
rect = QRectF()
for item in self.item_list:
rect |= item.rect()
return rect
def sizeHint(self, which, constraint):
return self.boundingRect().size()
def minimumSize(self):
return self.sizeHint(QGraphicsLayout.MinimumSize, QSizeF())
def preferredSize(self):
return self.sizeHint(QGraphicsLayout.PreferredSize, QSizeF())
def get_row_width(self):
row_width = 0
for item in self.item_list:
row_width += item.rect().width()
return row_width
def get_row_height(self):
row_height = 0
for item in self.item_list:
row_height = max(row_height, item.rect().height())
return row_height
def get_num_rows(self, view_width):
row_width = self.get_row_width()
num_rows = 1
if row_width > view_width:
num_rows = row_width // view_width + 1
return num_rows
def get_row_spacing(self):
return 10
def get_column_spacing(self):
return 10
def get_item_position(self, item, row, column):
x = column * (item.rect().width() + self.get_column_spacing())
y = row * (item.rect().height() + self.get_row_spacing())
return QPointF(x, y)
def setGeometry(self, rect):
view_width = rect.width()
num_rows = self.get_num_rows(view_width)
row_height = self.get_row_height()
for i, item in enumerate(self.item_list):
row = i // num_rows
column = i % num_rows
pos = self.get_item_position(item, row, column)
item.setPos(pos)
class QGraphicsFlowView(QGraphicsView):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene(self)
self.layout = QGraphicsFlowLayout()
self.scene.setLayout(self.layout)
self.setRenderHints(QGraphicsView.Antialiasing | QGraphicsView.SmoothPixmapTransform)
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
def add_item(self, item):
self.layout.addItem(item)
def resizeEvent(self, event):
super().resizeEvent(event)
self.layout.setGeometry(self.rect())
class QGraphicsFlowItem(QGraphicsRectItem):
def __init__(self, color):
super().__init__()
self.setRect(0, 0, 100, 100)
self.setBrush(QBrush(color))
self.setPen(QPen(QColor(0, 0, 0), 1))
if __name__ == "__main__":
app = QApplication([])
view = QGraphicsFlowView()
view.add_item(QGraphicsFlowItem(QColor(255, 0, 0)))
view.add_item(QGraphicsFlowItem(QColor(0, 255, 0)))
view.add_item(QGraphicsFlowItem(QColor(0, 0, 255)))
view.add_item(QGraphicsFlowItem(QColor(255, 255, 0)))
view.add_item(QGraphicsFlowItem(QColor(255, 0, 255)))
view.add_item(QGraphicsFlowItem(QColor(0, 255, 255)))
view.show()
app.exec_()到此這篇關(guān)于pandas使用QGraphicsView自動(dòng)排列項(xiàng)目的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pandas QGraphicsView自動(dòng)排列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Python的EasyGUI學(xué)習(xí)實(shí)踐
這篇文章主要介紹了基于Python的EasyGUI學(xué)習(xí)實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Python多種場景下實(shí)現(xiàn)CSV轉(zhuǎn)換為Excel文件
在數(shù)據(jù)處理和分析工作流程中,將 CSV 文件轉(zhuǎn)換為 Excel 格式是一項(xiàng)常見且實(shí)用的需求,本文將深入探討如何使用 Python 實(shí)現(xiàn)多種場景下的 CSV 到 Excel 轉(zhuǎn)換功能,感興趣的可以了解下2026-04-04
基于python實(shí)現(xiàn)PDF分頁和管理工具開發(fā)詳解
本文將詳細(xì)分析一個(gè)使用wxPython開發(fā)的PDF分離和管理工具,該工具能夠?qū)DF文件按頁分離,提供預(yù)覽功能,并支持別名管理系統(tǒng),感興趣的小伙伴可以了解下2025-09-09
基于Python打造一個(gè)PDF合并器(支持批量拖拽合并)
不知道你們有沒有遇到過這種情況,多個(gè) PDF想合成一份復(fù)習(xí)資料,或者下載了很多發(fā)票憑證和行程單,想合并成一份報(bào)銷文件,本文就來使用Python為大家搭建一個(gè)PDF合并器吧2025-07-07
Python?RawString與open文件的newline換行符遇坑解決
這篇文章主要為大家介紹了Python?RawString與open文件的newline換行符遇坑解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
PyTorch中的方法torch.randperm()示例介紹
在 PyTorch 中,torch.randperm(n) 函數(shù)用于生成一個(gè)從 0 到 n-1 的隨機(jī)排列的整數(shù)序列,這篇文章主要介紹了PyTorch中的方法torch.randperm()介紹,需要的朋友可以參考下2024-05-05

