【问题标题】:pyQt v4 rowsMoved SignalpyQt v4 rowsMo​​ved 信号
【发布时间】:2011-10-02 18:46:21
【问题描述】:

我对 Qt 很陌生,并且一直在尝试创建一个 UI,其中用户将拥有多行信息,每一行代表管道中的一个阶段。我试图实现用户可以拖放不同的行,这将改变步骤发生的顺序。

我已经使用以下方法实现了行的拖放: self.tableView.verticalHeader().setMovable(True)

我现在正试图让信号“rowsMo​​ved”工作,但似乎无法让它在我的自定义模型和委托中工作。如果有人知道如何让这个工作或不使用这个 Signla 并使用另一个信号来跟踪哪一行已经移动以及它现在移动到哪里。这将是一个很大的帮助! :)

谢谢大家

代码如下

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

【问题讨论】:

  • 我认为这里没有足够的信息让任何人都能够帮助您。我建议您添加一些缩减代码,或有关任何错误消息的信息,或任何人继续...
  • 你能发布一些代码来展示你目前是如何使用信号的吗?
  • 添加了一些代码。它基本上归结为您如何在 QAbstractTableModel 上使用信号 rowsMo​​ved,这是我遗漏的一件事,如何跟踪一行的移动,以便您可以看到它的位置和现在的位置,一旦它已经移动

标签: python drag-and-drop pyqt signals


【解决方案1】:

注意:连接到此信号的组件使用它来适应模型尺寸的变化。 只能由 QAbstractItemModel 实现发出,不能在子类代码中显式发出

【讨论】:

  • 嘿,是的,我读到了该评论,您知道我将如何检测此事件。
  • 真的不能像我建议的那样重载 rowMoved() SLOT 吗?那个 SLOT 不会被调用吗?
【解决方案2】:

你想要的是将 QTableView 子类化,并重载 rowMoved() SLOT

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 插槽和使用 rowsMo​​ved 信号

【讨论】:

  • 嘿,移动的行位于使用 QAbstractTableModel doc.qt.nokia.com/4.7-snapshot/qabstractitemmodel.html#rowsMoved 的 AbstractItemModel 上,如果您知道如何让信号正常工作,那就太棒了,它是拼图中缺少的一块。
  • 我认为它对您不起作用的原因是您没有以旧的连接方式使用信号的完整 C 签名。我相信如果您使用新的连接形式,那么您不需要指定整个签名。旧方法必须是: SIGNAL("rowsMo​​ved(QModelIndex&,int,int,QModelIndex&,int)") 。检查我更新的答案。顺便说一句,我在我的手机上这样做。没有测试。
  • 啊,伙计,是的,我试图做一个超载,但它似乎没有工作,我想我可能做错了什么掌心,需要做更多的阅读.
  • 按照我的示例连接到信号是否也不起作用?
猜你喜欢
  • 1970-01-01
  • 2016-06-21
  • 2011-08-08
  • 1970-01-01
  • 2016-11-13
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多