pyQt v4 行移动信号

0 投票
2 回答
1364 浏览
提问于 2025-04-17 03:32

我刚接触Qt,正在尝试创建一个用户界面,用户可以在这个界面上看到一行行的信息,每一行代表一个流程中的阶段。我想让用户能够拖动这些行,这样就可以改变步骤的顺序。

我已经实现了行的拖放功能,使用的代码是:self.tableView.verticalHeader().setMovable(True)

现在我想让“rowsMoved”这个信号工作,但在我的自定义模型和委托中似乎无法实现。如果有人知道如何让这个信号正常工作,或者有没有其他方法可以跟踪哪一行被移动了,以及它现在移动到了哪里,那将会非常有帮助!:)

谢谢大家

下面是代码

class pipelineModel( QAbstractTableModel ):

    def __init_( self ):
        super( pipelineModel, self ).__init__()
        self.stages = []

    # Sets up the population of information in the Model    
    def data( self, index, role=Qt.DisplayRole ):

        if (not index.isValid() or not (0 <= index.row() < len(self.stages) ) ):
            return QVariant()

        column = index.column()
        stage = self.stages[ index.row() ]          # Retrieves the object from the list using the row count.

        if role == Qt.DisplayRole:                      # If the role is a display role, setup the display information in each cell for the stage that has just been retrieved
            if column == NAME:
                return QVariant(stage.name)
            if column == ID:
                return QVariant(stage.id)
            if column == PREV:
                return QVariant(stage.prev)
            if column == NEXT:
                return QVariant(stage.next)
            if column == TYPE:
                return QVariant(stage.assetType)
            if column == DEPARTMENT:
                return QVariant(stage.depID)
            if column == EXPORT:
                return QVariant(stage.export)
            if column == PREFIX:
                return QVariant(stage.prefix)
            if column == DELETE:
                return QVariant(stage.delete)

        elif role == Qt.TextAlignmentRole:
            pass

        elif role == Qt.TextColorRole:
            pass

        elif role == Qt.BackgroundColorRole:
            pass

        return QVariant()

    # Sets up the header information for the table
    def headerData( self, section, orientation, role = Qt.DisplayRole ):

        if role == Qt.TextAlignmentRole:

            if orientation == Qt.Horizontal:
                return QVariant( int(Qt.AlignLeft|Qt.AlignVCenter))
            return QVariant( int(Qt.AlignRight|Qt.AlignVCenter))

        if role != Qt.DisplayRole:
            return QVariant()

        if orientation == Qt.Horizontal:        # If Orientation is horizontal then we populate the headings
            if section == ID:
                return QVariant("ID")
            elif section == PREV:
                return QVariant("Previouse")
            elif section == NEXT: 
                return QVariant("Next")
            elif section == NAME:
                return QVariant("Name")
            elif section == TYPE:
                return QVariant("Type")
            elif section == DEPARTMENT:
                return QVariant("Department")
            elif section == EXPORT:
                return QVariant("Export Model")
            elif section == PREFIX:
                return QVariant("Prefix")
            elif section == DELETE:
                return QVariant("Delete")
        return QVariant( int( section + 1 ) )           # Creates the Numbers Down the Vertical Side

    # Sets up the amount of Rows they are
    def rowCount( self, index = QModelIndex() ):
        count = 0
        try:
            count = len( self.stages )
        except:
            pass

        return count

    # Sets up the amount of columns they are
    def columnCount( self, index = QModelIndex() ):
        return 9

    def rowsMoved( self, row, oldIndex, newIndex ):
        print 'ASDASDSA'

# ---------MAIN AREA---------
class pipeline( QDialog ):


    def __init__(self, parent = None):
        super(pipeline, self).__init__(parent)
        self.stages = self.getDBinfo()                      # gets the stages from the DB and return them as a list of objects

        tableLabel      = QLabel("Testing Table - Custom Model + Custom Delegate")
        self.tableView  = QTableView()              # Creates a Table View (for now we are using the default one and not creating our own)

        self.tableDelegate  = pipelineDelegate()
        self.tableModel     = pipelineModel()

        tableLabel.setBuddy( self.tableView )
        self.tableView.setModel( self.tableModel )
#       self.tableView.setItemDelegate( self.tableDelegate )

        layout = QVBoxLayout()
        layout.addWidget(self.tableView)
        self.setLayout(layout)

        self.tableView.verticalHeader().setMovable(True) 

        self.connect(self.tableModel, SIGNAL("rowsMoved( )"), self.MovedRow) # trying to setup an on moved signal, need to check threw the available slots
#       self.connect(self.tableModel, SIGNAL("  rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex ,int)"), self.MovedRow) # trying to setup an on moved signal, need to check threw the available slots

2 个回答

0

注意:与这个信号连接的组件会用它来适应模型尺寸的变化。这个信号只能由QAbstractItemModel的实现发出,子类代码不能直接发出这个信号

0

你需要做的是创建一个QTableView的子类,并重写rowMoved()这个槽函数。

class MyTableView(QtGui.QTableView):
    def __init__(self, parent=None):
        super(MyTableView, self).__init__(parent)

        self.rowsMoved.connect(self.movedRowsSlot)  

    def rowMoved(self, row, oldIndex, newIndex):
        # do something with these or

        super(MyTableView, self).rowMoved(row, oldIndex, newIndex)

    def movedRowsSlot(self, *args):
        print "Moved rows!", args

编辑过 这里展示了如何重写rowMoved槽函数,以及如何使用rowsMoved信号。

撰写回答