【问题标题】:Move row up and down in PyQt5 [duplicate]在PyQt5中上下移动行[重复]
【发布时间】:2020-08-12 00:41:36
【问题描述】:

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

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

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_())

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    要转换为 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_())
    

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多