Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding only one row until the user fills in the information and then that information is submitted to the database. [SOLVED]
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 873 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukeQt
    wrote on last edited by lukeQt
    #1

    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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lukeQt
        wrote on last edited by lukeQt
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lukeQt
            wrote on last edited by lukeQt
            #5

            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()?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              L 1 Reply Last reply
              0
              • SGaistS SGaist

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

                L Offline
                L Offline
                lukeQt
                wrote on last edited by lukeQt
                #7

                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)
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved