QTreeView怎么绘制一整行内容

重写QTreeView的drawRow方法。
 

void MyTreeView::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{

    painter->save();
    painter->setPen(QColor("gray"));
    painter->drawRect(QRect(option.rect.topLeft(), QSize(option.rect.width(), option.rect.height())));
    painter->restore();

    QTreeView::drawRow(painter, option, index);
}

实现效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于在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来确定绘制指示器的位置,如果是在项上,则获取当前项的视觉矩形。最后,我们使用红色的画笔绘制一个矩形作为指示器。 您可以根据自己的需求修改指示器的绘制样式和颜色。希望对您有所帮助!如果您有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值