Adding only one row until the user fills in the information and then that information is submitted to the database. [SOLVED]



  • Is this the best method to prevent the user from adding multiple rows before they finish editing the row and that row is submitted to the database? I only want to submit to the database when there is a value in each cell. There has to be a better method then this.

    class DataGap(QtGui.QDialog, ui_data_gaps.Ui_DataGapDialog):
        trigger = QtCore.pyqtSignal()
        def __init__(self, database = None, table = None, parent = None):
            super(DataGap, self).__init__(parent)
            self.setupUi(self)
    
            header = self.data_gap_tableView.horizontalHeader()
            header.setStretchLastSection(True)
            self.data_gap_tableView.setAlternatingRowColors(True)
    
            self.model = DataGapModel(table, parent = self)
            self.data_gap_tableView.setModel(self.model)
            self.model.select()
            self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
    
            # Signals
            self.add_row_pushButton.clicked.connect(self.add_row)
    
            self.model.dataChanged.connect(self.submit)
            self.temp = False
    
    
        def submit(self, index):
            self.model.submitAll()
            if self.model.lastError().isValid():
                print self.model.lastError().text()
                self.temp = True
            else:
                self.temp = False
    
        def add_row(self):
            if self.temp == True:
                msg = "Please complete previous row before adding a new row"
                QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
                return
    
            row = self.model.rowCount()
            self.model.insertRow(row)
            index = self.model.index(row, DATANAME)
            if index.isValid():
                self.data_gap_tableView.edit(index)
            self.resize_row()

  • Lifetime Qt Champion

    Hi,

    If you only want to add one row at a time then disable anything that can add a new row in your GUI until your user is done editing the current row



  • How do you do that if onmanualsubmit() is being used? I only want the user to be able to add a row if the previous row was submitted to the database correctly.


  • Lifetime Qt Champion

    You can e.g. disable add_row_pushButton until the user committed the data



  • How do you test if the row was submitted to the database correctly then? I am using dataChange signal, but I am not sure that is correct. Do you use isDirty()?


  • Lifetime Qt Champion

    Don't you already do it in your submit function ?



  • Thank you SGaist for all your help. You're a true pyqt ninja . No that is where I am confused. This handles if both of the values are dirty which makes sense if one is inserting a new row, but what if the user is updating only one cell from a row that has already been inserted? Then both cells are not dirty and this logic does not work. dataChanged also gets called whenever one cell is changed and gets called twice. Is dataChanged the right signal? I only want call submit when both cells have been changed on a newly inserted row. If the row has already been set then only one cell would need to be dirty.

       self.add_row_pushButton.clicked.connect(self.add_row)
       self.model.dataChanged.connect(self.submit)
    
    
    def submit(self, index):
        if self.model.isDirty(self.model.index(index.row(), 1)):
            if self.model.isDirty(self.model.index(index.row(), 2)):
                self.model.submitAll()
                if self.model.lastError().isValid():
                    print self.model.lastError().text()
                    self.add_row_pushButton.setEnabled(False)
                else:
                    self.add_row_pushButton.setEnabled(True)

  • Lifetime Qt Champion

    Then you should have something like a "validate" button that the user must push in order to submit the data to the database. Enable that button once all fields contain something


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.