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)文章
spyder 在控制臺(tái)(console)執(zhí)行python文件,debug python程序方式
這篇文章主要介紹了spyder 在控制臺(tái)(console)執(zhí)行python文件,debug python程序方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python對多屬性的重復(fù)數(shù)據(jù)去重實(shí)例
下面小編就為大家分享一篇Python對多屬性的重復(fù)數(shù)據(jù)去重實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04對Python 檢查文件名是否規(guī)范的實(shí)例詳解
今天小編就為大家分享一篇對Python 檢查文件名是否規(guī)范的實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python實(shí)現(xiàn)音樂下載的統(tǒng)計(jì)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)音樂下載的統(tǒng)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06python判斷列表的連續(xù)數(shù)字范圍并分塊的方法
今天小編就為大家分享一篇python判斷列表的連續(xù)數(shù)字范圍并分塊的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python如何爬取微信公眾號文章和評論(基于 Fiddler 抓包分析)
這篇文章主要介紹了Python如何爬取微信公眾號文章和評論(基于 Fiddler 抓包分析),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06windows系統(tǒng)Tensorflow2.x簡單安裝記錄(圖文)
這篇文章主要介紹了windows系統(tǒng)Tensorflow2.x簡單安裝記錄(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python中NumPy的ufuncs函數(shù)實(shí)例
這篇文章主要介紹了Python中NumPy的ufuncs函數(shù)實(shí)例,NumPy是一個(gè)開源的Python科學(xué)計(jì)算庫,使用NumPy,就可以很自然地使用數(shù)組和矩陣,本文主要介紹Python Numpy ufuncs通用函數(shù),需要的朋友可以參考下2023-07-07Python json格式化打印實(shí)現(xiàn)過程解析
這篇文章主要介紹了Python json格式化打印實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07