首页 > 解决方案 > 在 PyQt5 中上下移动行

问题描述

考虑一个 QTableWidget 和两个按钮“上移”和“下移”。点击上移,当前行应该上移一行,类似于“下移”。

实现相应的上移和下移功能最简单的方法是什么?或者是否可以将此代码段更新为 pyqt5,它在 pyqt4 中执行相同的任务!

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui

class mtable(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)


        self.move_up = QtGui.QAction("Move_Up", self)
        self.connect(self.move_up, QtCore.SIGNAL('triggered()'), self.moveUp)

        self.move_down = QtGui.QAction("Move_Down",self)
        self.connect(self.move_down, QtCore.SIGNAL('triggered()'), self.moveDown)

        self.toolbar = self.addToolBar('Toolbar')
        self.toolbar.addAction(self.move_up)
        self.toolbar.addAction(self.move_down)


        ##Init Table
        self.table = QtGui.QTableWidget(4,3)
        for i in range(0,4):
            for j in range(0,4):
                self.table.setItem(i,j,QtGui.QTableWidgetItem("a_"+str(i)+str(j)))

        self.setCentralWidget(self.table)

    def moveDown(self):
        row = self.table.currentRow()
        column = self.table.currentColumn();
        if row < self.table.rowCount()-1:
            self.table.insertRow(row+2)
            for i in range(self.table.columnCount()):
               self.table.setItem(row+2,i,self.table.takeItem(row,i))
               self.table.setCurrentCell(row+2,column)
            self.table.removeRow(row)        


    def moveUp(self):    
        row = self.table.currentRow()
        column = self.table.currentColumn();
        if row > 0:
            self.table.insertRow(row-1)
            for i in range(self.table.columnCount()):
               self.table.setItem(row-1,i,self.table.takeItem(row+1,i))
               self.table.setCurrentCell(row-1,column)
            self.table.removeRow(row+1)        


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    tb = mtable()
    tb.show()
    sys.exit(app.exec_())

标签: pythonpyqtpyqt5

解决方案


转换为 PyQt5 所需要做的就是将导入和模块从 QtGui 更改为 QtWidgets 以及信号槽连接的语法。

import sys
from PyQt5 import QtWidgets

class mtable(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        
        self.move_up = QtWidgets.QAction("Move_Up", self)
        self.move_up.triggered.connect(self.moveUp)

        self.move_down = QtWidgets.QAction("Move_Down",self)
        self.move_down.triggered.connect(self.moveDown)

        self.toolbar = self.addToolBar('Toolbar')
        self.toolbar.addAction(self.move_up)
        self.toolbar.addAction(self.move_down)

        ##Init Table
        self.table = QtWidgets.QTableWidget(4,3)
        for i in range(0,4):
            for j in range(0,4):
                self.table.setItem(i,j,QtWidgets.QTableWidgetItem("a_"+str(i)+str(j)))

        self.setCentralWidget(self.table)

该类的其余部分应该是相同的,只是对 QApplication 进行了一次编辑。

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    tb = mtable()
    tb.show()
    sys.exit(app.exec_())

推荐阅读