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_())
相关推荐
进击的明明1 天前
当AI开始“边想边干”:深入聊聊ReAct,以及如何用Python手搓一个简单Agent
python·面试·agent
全栈弄潮儿1 天前
Python实战第8期:函数
后端·python·编程语言
卷无止境1 天前
暗流与破局:AI时代的代码安全审查工具全景指南
后端·python
卷无止境1 天前
一人公司的全栈选型指南:让 AI Agent 写得快,也让生意跑得稳
后端·python
滴啦嘟啦哒1 天前
【具身笔记】【π0.5 微调与仿真】五、在MuJoCo中实现可视化仿真
笔记·python·vla·具身
全栈练习生1 天前
AI安全与红队测试
python·ai
费曼学习法1 天前
把「发送成功」当「送达」?我排查 IM 长消息被静默吞掉的完整过程
python·ai编程·自动化运维
鸽鸽程序猿1 天前
【项目】【在线判题系统】b端题目增删改
开发语言·python
NetX行者1 天前
Python 工程化入门:从脚本到规范的 Python 项目结构
网络·python
MasonYyp1 天前
asyncio并发协程
python