2011-10-02 46 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