PYQT QTreeView绘制拖放指示器
时间: 2023-08-27 21:50:12 浏览: 72
您好!对于在PYQT中使用QTreeView绘制拖放指示器,您可以通过重写QAbstractItemView的drawDropIndicator方法来实现。下面是一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QTreeView, QMainWindow
from PyQt5.QtCore import Qt
class CustomTreeView(QTreeView):
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self.setDragEnabled(True)
def drawDropIndicator(self, painter):
option = self.viewOptions()
rect = self.viewport().rect()
if self.dropIndicatorPosition() == QAbstractItemView.OnItem:
index = self.indexAt(self.dropIndicatorPosition())
if index.isValid():
rect = self.visualRect(index)
painter.setPen(Qt.red)
painter.drawRect(rect.adjusted(0, 0, -1, -1))
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.tree_view = CustomTreeView(self)
self.setCentralWidget(self.tree_view)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在上面的示例中,我们创建了一个自定义的QTreeView类CustomTreeView,并重写了drawDropIndicator方法。在该方法中,我们首先获取当前的绘制选项和视口的矩形区域。然后根据当前的dropIndicatorPosition来确定绘制指示器的位置,如果是在项上,则获取当前项的视觉矩形。最后,我们使用红色的画笔绘制一个矩形作为指示器。
您可以根据自己的需求修改指示器的绘制样式和颜色。希望对您有所帮助!如果您有任何问题,请随时提问。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)