2011-10-02 38 views
0

我很新的Qt,并试图创建一个用户界面,其中用户将有信息行,每一行代表一个管道中的阶段。 Iam试图实现一个用户可以拖放不同的行,这将改变步骤的发生顺序。pyQt v4 rowsMo​​ved信号

我已经实现了阻力和使用行的下降。 self.tableView.verticalHeader()setMovable(真)

荫现在试图让信号“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 
+0

我不认为这里有足够的信息可以帮助任何人解决问题。我建议你或者添加一些简化代码,或者关于任何错误消息的信息,或者别人的任何信息...... –

+0

你可以发布一些代码来展示你当前如何使用信号? –

+0

增加了一些代码。它基本上归结为如何使用QAbstractTableModel上的信号行移动,它是我所遗漏的一件事,如何跟踪行的移动,以便您可以看到它在哪里以及它现在在哪里,一旦它出现移动 –

回答

0

你想要的是继承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信号

+0

嘿,移动的行是在AbstractItemModel,我使用QAbstractTableModel http://doc.qt.nokia.com/4.7-snapshot/qabstractitemmodel.html#rowsMo​​ved,如果你知道如何让signla工作owuld是真棒,其中一个缺少的难题。 –

+0

我认为它不适合你的原因是因为你没有以你连接的旧方式使用信号的完整C signture。我相信如果你使用新的连接形式,那么你不需要指定整个签名。旧的方式必须如下所示:SIGNAL(“rowsMo​​ved(QModelIndex&,int,int,QModelIndex&,int)”)。检查我更新的答案。 btw即时通讯在我的手机上这样做。没有测试过。 – jdi

+0

Ahoy男人,是的,我试图做一个超载,它似乎没有工作,我想也许我做错了什么*面对手掌*,需要做更多的阅读。 –

0

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

+0

嘿,是的,我看了看到这个评论,你有什么想法,然后我会如何检测这个事件。 –

+0

它真的不工作,只是超载rowMoved()SLOT像我建议的?那个SLOT没有被叫? – jdi