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_())
相关推荐
学c真好玩11 分钟前
4.3python操作ppt
开发语言·python·powerpoint
巷北夜未央1 小时前
数据结构之二叉树Python版
开发语言·数据结构·python
wapicn991 小时前
手机归属地查询Api接口,数据准确可靠
java·python·智能手机·php
郝YH是人间理想2 小时前
OpenCV基础——傅里叶变换、角点检测
开发语言·图像处理·人工智能·python·opencv·计算机视觉
白白糖2 小时前
二叉树 递归
python·算法·力扣
G皮T2 小时前
【Python Cookbook】字符串和文本(五):递归下降分析器
数据结构·python·正则表达式·字符串·编译原理·词法分析·语法解析
独好紫罗兰3 小时前
洛谷题单3-P1420 最长连号-python-流程图重构
开发语言·python·算法
kingmax542120083 小时前
深入解析:使用Python爬取Bilibili视频
开发语言·python·音视频
程序员一诺3 小时前
【Flask开发】嘿马文学web完整flask项目第1篇:简介【附代码文档】
后端·python·flask·框架
Aerkui3 小时前
Python列表(List)深度解析
开发语言·python