PyQt newbie再次将当前记录从tableview传递到其他类中的Qdialog / Mapper表单

时间:2014-12-30 10:23:06

标签: python qt python-3.x pyqt pyqt5

我在一个类(KL_browse)的tableview中有一个Qsqltable QsqlTable有一个选择栏并编辑所选记录我在另一个类中调用一个表单(Test_win)

如何将Tableview中的选定记录以???

的形式传递给映射器

参见代码示例(测试和播放代码!)

class Test_win(QDialog,ui_test_win.Ui_test_win):

def init (自我,状态,索引):         super(Test_win,self)。 init ()         self.setupUi(个体经营)         self.state = state         self.index = index

#对生成的ui进行一些本地修改         self.naam_edt.setAlignment(Qt.AlignRight)

#Set model

    self.model = QSqlTableModel(self)
    self.model.setTable("KLANTEN")
    self.model.select()

#设置要查看的映射字段         self.mapper = QDataWidgetMapper(self)         self.mapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)         self.mapper.setModel(self.model)

    self.mapper.addMapping(self.klnr_edt, 0)
    self.mapper.addMapping(self.naam_edt, 1)
    self.mapper.addMapping(self.straat_edt, 2)
    self.mapper.addMapping(self.huisnr_edt, 3)
    self.mapper.addMapping(self.gemeente_edt, 4)       

#connections&插槽
        self.ok_button.clicked.connect(self.save_Record)         self.pushButton.clicked.connect(self.prev_Record)    #在添加模式下设置
        如果state == 0:             query = QSqlQuery(""" SELECT MAX(KLANTNR)as KLANTNR                              来自KLANTEN""")             query.next()             如果query.isValid():                 Hklantnr = query.value(0)
            其他:                 返回无
            self.klnr_edt.setText(str(Hklantnr + 1))             row = self.model.rowCount()             self.model.insertRow(行)             self.mapper.setCurrentIndex(行)             self.naam_edt.setFocus()    #在编辑模式下设置             query = QSqlQuery(""" SELECT * FROM KLANTEN WHERE KLANTNR = self.index""")             query.next()

        self.naam_edt.setFocus()

def prev_Record(self):         self.mapper.toNext()

def save_Record(self):

    self.mapper.submit()
    QDialog.done(self, True)

#Voor beeld van浏览门klantenbestand

类KL_browse(QDialog,ui_kl_browse.Ui_kl_browse):

def __init__(self):
    super(KL_browse, self).__init__()
   # query = QSqlQuery()

   # query.exec_("""SELECT * FROM klanten ORDER BY klantnaam""")

    self.setupUi(self)

    self.model = QSqlTableModel(self)
    self.model.setTable("KLANTEN")
    self.model.select()
    self.tableView.setModel(self.model)
    self.tableView.setSelectionMode(QTableView.SingleSelection)
    self.tableView.setSelectionBehavior(QTableView.SelectRows)
 #   self.view.setColumnHidden(ID, True)
 #   index = self.tableView.model().index(0, 1)
 #   self.tableView.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate)      

    self.tableView.horizontalHeader().setSectionsMovable(True)
    self.tableView.horizontalHeader().setDragEnabled(True)
    self.tableView.horizontalHeader().setDragDropMode(QAbstractItemView.InternalMove)
    self.tableView.horizontalHeader().setSortIndicator(1,0)
    self.tableView.selectRow(0)
    self.tableView.selectColumn(1) 

    index = self.tableView.model().index(0, 1)
    self.tableView.setCurrentIndex(index)

#Volgende代码是om de actie op knoppen en dergelijke op te vangen         self.add_button.clicked.connect(self.add_record)         self.Del_button.clicked.connect(self.del_record)         self.ed_button.clicked.connect(self.edt​​_record)

def del_record(self):
     index = self.tableView.currentIndex()
     self.model.removeRow(index.row())
     self.model.submitAll()
     self.model.select()
     index = self.tableView.model().index(0, 1)
     self.tableView.setCurrentIndex(index)


def add_record(self):
     self.test_win = Test_win(0) # 0 = add mode
     self.test_win.exec_() 
     self.model.select()
     index = self.tableView.model().index(0, 1)
     self.tableView.setCurrentIndex(index)

def  edt_record(self):
     index = self.tableView.currentIndex()   
     self.test_win = Test_win(1, index)  # 1 = Edit mode
     self.test_win.exec_() 
   #  self.model.select() 
   #  self.tableView.activateWindow()

1 个答案:

答案 0 :(得分:1)

这是一个既定的控制:

self.tableview.connect(self.tableview,SIGNAL('clicked(QModelIndex)'),
        lambda x:self.mapper.setCurrentModelIndex(self.tableview.selectedIndexes()[0]))

    def connects(self):
        self.connect(self.controlButtons.topPushButton, SIGNAL("clicked()"),lambda:self.arrow('firstRecord'))
        self.connect(self.controlButtons.lastPushButton, SIGNAL("clicked()"),lambda:self.arrow('lastRecord'))
        self.connect(self.controlButtons.beforePushButton, SIGNAL("clicked()"),lambda:self.arrow('prevRecord'))
        self.connect(self.controlButtons.nextPushButton, SIGNAL("clicked()"),lambda:self.arrow('nextRecord'))
        self.connect(self.controlButtons.newPushButton, SIGNAL("clicked()"),self.newRecord)
        self.connect(self.controlButtons.modifyPushButton, SIGNAL("clicked()"),self.modifyRecord)
        self.connect(self.controlButtons.deletePushButton, SIGNAL("clicked()"),self.deleteRecord)
        self.connect(self.controlButtons.savePushButton, SIGNAL("clicked()"),self.saveRecord)
        self.connect(self.controlButtons.cancelPushButton, SIGNAL("clicked()"),self.cancelRecord)


    def arrow(self, arrow):
        if arrow=='firstRecord':self.mapper.toFirst()
        elif arrow=='lastRecord':self.mapper.toLast()
        elif arrow=='prevRecord':self.mapper.toPrevious()
        elif arrow=='nextRecord':self.mapper.toNext()
        self.tableSelectRow()

    def tableSelectRow(self):
        row = self.mapper.currentIndex()
        if self.tableview:
            self.tableview.selectRow(row)

    def newRecord(self):
        row = self.model.rowCount()
        self.model.insertRow(row)
        self.arrow('lastRecord')

    def modifyRecord(self):pass

    def deleteRecord(self):
        if (QMessageBox.question(None,"Delete","Really Delete?",QMessageBox.Yes, QMessageBox.No)==QMessageBox.No):
            return
        row = self.mapper.currentIndex()
        self.model.removeRow(row)
        self.model.submitAll()
        if row + 1 >= self.model.rowCount(): row-=1
        self.mapper.setCurrentIndex(row)
        self.tableSelectRow()

    def saveRecord(self):
        row = self.mapper.currentIndex()
        self.mapper.submit()
        self.mapper.setCurrentIndex(row)
        self.tableSelectRow()

    def cancelRecord(self): 
        self.mapper.revert()
        self.tableSelectRow()