pyqt5 tableView实现excel拖曳填充

和tableWidget实现excel拖曳填充大同小异,见代码中MyTableView类,其他类只用于演示,无大作用。

python 复制代码
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QPoint, Qt, QCoreApplication, pyqtSlot
from PyQt5.QtGui import  QBrush, QPixmap, QColor, QPainter,QIcon,QPolygon
from PyQt5 import QtWidgets,QtCore
from PyQt5.QtCore import pyqtSignal
import logging
class MyTableView(QtWidgets.QTableView):
    dragbottom = pyqtSignal(object)
    def __init__(self,parent):
        super().__init__(parent)
        self.cellBottomRight=False
        self.leftbuttonDown=False
    def show_select(self):
        painter=QPainter(self.viewport())
        pen = painter.pen();
        pen.setWidth(1);
        selections = self.selectionModel()
        if selections!=None:
            # logging.info("not None")
            list1 = selections.selectedIndexes()
            for i in range(len(list1)):
                modelIndex = list1[i]
                #QRect类提供各种矩形坐标,绘制线跟点的时候需要用到坐标
                rect = self.visualRect(modelIndex);
                tmpRect=QtCore.QRect(QtCore.QPoint(rect.x()+1,rect.y()+1),QtCore.QSize(rect.width()-2,rect.height()-2))
                #如果是选中状态 并且在选择公式状态
                #给选中单元格进行画线画点
                dashes=[]
                penXu = painter.pen();
                #设置画笔宽度
                penXu.setWidth(2);
                color=QColor();
                #设置画笔颜色
                color.setRgb(31,187,125);
                penXu.setColor(color);
                painter.setPen(penXu);
                #绘制单元格四周的线
                # print(dir(tmpRect))
                painter.drawRect(tmpRect);
                #绘制单元格右下角点
                penXu.setWidth(6);
                painter.setPen(penXu);
                painter.drawPoint(tmpRect.x()+tmpRect.width() -3,tmpRect.y()+tmpRect.height()-3);

    def paintEvent(self,e):
        super().paintEvent(e)
        self.show_select()
    #鼠标移动事件
    def mouseMoveEvent(self,event):
        #获取鼠标位置信息
        # logging.info(event)
        if not self.cellBottomRight:
            # logging.info("not bottom")
            mousePos =event.pos();
            #获取所有选中单元格
            # logging.info(self.cellBottomRight)
            selections = self.selectionModel()
            if selections==None:
                return;
            list1 = selections.selectedIndexes()
            if len(list1)>0:
                modelIndex = list1[-1]
                #获取最后一个选中的单元格的QRect,用来判断是否鼠标位置是否在右下角区域
                rect = self.visualRect(modelIndex);
                # logging.info([mousePos,rect])
                #判断是否在我们规定的区域,或者是按下模式,isClick是按下模式标志
                # print(dir(mousePos))
                if((mousePos.x() >= (rect.x()+rect.width() -7) and mousePos.x() <= (rect.x()+rect.width())
                    and mousePos.y() >= (rect.y()+rect.height()-7) and mousePos.y() <= (rect.y()+rect.height()))):
                    logging.info("right bottom")
                    #设置鼠标在右下角区域样式
                    self.setCursor(Qt.CrossCursor);
                    #在右下角区域
                    self.cellBottomRight = True;
        super().mouseMoveEvent(event);
        #鼠标点击事件
    def mousePressEvent(self,event):
        if(event.button()==Qt.LeftButton):
            self.leftbuttonDown = True;
            self.setCursor(Qt.SizeAllCursor);
        else:
            self.leftbuttonDown = False;
            self.setCursor(Qt.ArrowCursor);
        super().mousePressEvent(event);#提交鼠标给控件
    def mouseReleaseEvent(self,ev):
        if self.leftbuttonDown and self.cellBottomRight:
            selections = self.selectionModel()
            if selections!=None:
                list1 = selections.selectedIndexes()
                if len(list1)>0:
                    logging.info("dragbottom")
                    self.dragbottom.emit(list1)
        self.leftbuttonDown = False;
        self.cellBottomRight = False;
        self.setCursor(Qt.ArrowCursor);
        super().mouseReleaseEvent(ev)
class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super().__init__()
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data)

    def columnCount(self, parent=None):
        return len(self._data[0])

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole:
            return str(self._data[index.row()][index.column()])

        return None

    def flags(self, index):
        return QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled | super().flags(index)

    def moveRow(self, fromIndex, toIndex):
        row = self._data.pop(fromIndex)
        self.beginInsertRows(QtCore.QModelIndex(), toIndex, toIndex)
        self._data.insert(toIndex, row)
        self.endInsertRows()

    def mimeTypes(self):
        return [QtCore.Qt.MimeType]

    def mimeData(self, indexes):
        mimedata = QtCore.QMimeData()
        encodedData = QtCore.QByteArray()

        stream = QtCore.QDataStream(encodedData, QtCore.QIODevice.WriteOnly)

        for index in indexes:
            if index.isValid():
                text = self._data[index.row()][index.column()]
                stream.writeString(text.encode("utf-8"))

        mimedata.setData("QtCore.Qt.MimeType", encodedData)
        return mimedata

    def dropMimeData(self, data, action, row, column, parent):
        if action == QtCore.Qt.IgnoreAction:
            return True

        if not data.hasFormat(QtCore.Qt.MimeType):
            return False

        if column > 0:
            return False

        if row != -1:
            self.moveRow(data.text(), row)
        else:
            self.moveRow(data.text())

        return True
def dragbottom(items):
    print(items)  
    global data
    first=items[0]
    col=first.column()
    row=first.row()
    first_value=data[row][col]
    print(dir(first))
    for one in items[1:]:
        col=one.column()
        row=one.row()
        data[row][col]=first_value
        # self.table.setItem(one.row(),one.column(),QTableWidgetItem(v))
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    data = [
        ["1", "John"],
        ["2", "Alice"],
        ["3", "Bob"]
    ]

    model = TableModel(data)

    view = MyTableView(None)
    view.dragbottom.connect(dragbottom)
    view.setModel(model)
    view.setWindowTitle("Drag and Drop")
    view.show()

    sys.exit(app.exec_())
相关推荐
jufeng130710 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 11 篇】
python·ai agent·mcp
En^_^Joy10 小时前
Django项目配置全攻略:settings配置文件
后端·python·django
for_ever_love__11 小时前
python基础语法学习: 异常的传递性
开发语言·python·学习
AC赳赳老秦11 小时前
公开图片 OCR 数据提取:OpenClaw 合规采集公开信息图,识别文字与表格并转化为结构化数据
java·大数据·前端·数据库·python·php·openclaw
byxdaz12 小时前
QT中服务使用
开发语言·qt
麻雀飞吧13 小时前
零基础选量化工具,先把问题说清楚
人工智能·python
段一凡-华北理工大学13 小时前
AI推动工业智能化转型~系列文章20:工业 AI 平台架构:云-边-端协同的技术体系
人工智能·python·架构·工业平台·云-边协同
CodeBlog-star13 小时前
Harness Engineering:Pi Agent 架构深度解析
人工智能·python·架构·harness工程
简不变13 小时前
将components下的文件夹复制到别一个文件夹下,并重新生成CMakelist.txt文件
python