2011-05-10 2 views
2

사용자 지정 대리자를 사용하여 QTableView에 comboBoxes 열을 표시합니다. 값은 모든 comboBox에 대해 동일하므로 실제로 문제가되는 부분이 아닙니다.QTableView에서 사용자 지정 대리인의 comboBox 항목을 선택했습니다.

내가 데이터베이스에서 검색 할 수있는 값인 선택된 항목으로 표시하려고합니다. 델리게이트에서 데이터베이스에 액세스 할 수 있지만 요청을 보내려면 comboBox 행이 필요합니다.

그래서 내 질문에 : 테이블의 모든 행을 반복하고 사용자 지정 대리자 내부에서 몇 가지 작업을 수행 할 수 있습니까?

는 여기에 도움이 될 수 있다면 내 사용자 지정 위임 클래스입니다 :

class ComboBoxDelegate(QtGui.QItemDelegate): 

def __init__(self, parent, itemslist): 
    QtGui.QItemDelegate.__init__(self, parent) 
    self.itemslist = itemslist 
    self.parent = parent 

def paint(self, painter, option, index):   
    # Get Item Data 
    value = index.data(QtCore.Qt.DisplayRole).toInt()[0] 
    # value = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]] 
    # fill style options with item data 
    style = QtGui.QApplication.style() 
    opt = QtGui.QStyleOptionComboBox() 
    opt.currentText = str(self.itemslist[value]) 
    opt.rect = option.rect 


    # draw item data as ComboBox 
    style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter) 
    self.parent.openPersistentEditor(index) 

def createEditor(self, parent, option, index): 

    ##get the "check" value of the row 
    # for row in range(self.parent.model.rowCount(self.parent)): 
     # print row 

    self.editor = QtGui.QComboBox(parent) 
    self.editor.addItems(self.itemslist) 
    self.editor.setCurrentIndex(0) 
    self.editor.installEventFilter(self)  
    self.connect(self.editor, QtCore.SIGNAL("currentIndexChanged(int)"), self.editorChanged) 

    return self.editor 

# def setEditorData(self, editor, index): 
    # value = index.data(QtCore.Qt.DisplayRole).toInt()[0] 
    # editor.setCurrentIndex(value) 

def setEditorData(self, editor, index): 
    text = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]] 
    pos = self.editor.findText(text) 
    if pos == -1: 
     pos = 0 
    self.editor.setCurrentIndex(pos) 


def setModelData(self,editor,model,index): 
    value = self.editor.currentIndex() 
    model.setData(index, QtCore.QVariant(value)) 


def updateEditorGeometry(self, editor, option, index): 
    self.editor.setGeometry(option.rect) 

def editorChanged(self, index): 
    check = self.editor.itemText(index) 
    id_seq = self.parent.selectedIndexes[0][0] 
    update.updateCheckSeq(self.parent.db, id_seq, check) 

그리고는이 같은 QTableView을 폴더 만 전화 : 귀하의 관심을 내가 충분히 분명했다

self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged) 
self.viewport().installEventFilter(self) 
self.setItemDelegateForColumn(13,ComboBoxDelegate(self, self.checkValues)) 

희망, 감사

답변

1

델리게이트에서 데이터베이스에 액세스하는 것이 올바른지 확실하지 않습니다. 델리게이트는 QTableView가 참조하는 QAbstractTableModel의 인스턴스에 대한 참조를 포함 할 수 있습니다. 그런 다음 모델의 메서드를 사용하여 테이블의 행을 반복 할 수 있습니다.

+0

그럼 난 compboBoxes에 올바른 선택한 항목을 표시 관리. 그러나 당신이 진술 한대로 나는 위임자로부터 데이타베이스에 접근하고 있습니다. 그러나 당신이 제안한 것을 구현하는 방법을 정말로 보지 못합니다. 그리고 나는 여전히 문제가 있습니다 : comboBoxes는 tableView를 재정렬 할 때 "따라야"하지 않습니다 .. – Johanna

+0

모델로부터 델리게이트를 다시 칠할 수 있습니까? – Johanna

+0

하지만 모델에서 대리인에 대해 어떻게 알 수 있습니까? 모델에 대리자 참조를 전달할 수는 있습니다. 대표자는 프레젠테이션을 개선하여 모델을 만들고 상호 의존성을 위임하면 코드가 부서져 버릴 수 있습니다. – sateesh

관련 문제