[PySide, Python 3] QSqlQuery::value: not positioned on a valid record



  • Hello,

    I'm trying to learn about model/view with Sql and widget mapping, and as I experiment I'm coming across some obstacles -- I assume these problems are pretty easy to diagnose for someone who has more experience:

    I'm trying to map a lineedit and a spinbox to a text field & integer field in my sqlite database. The lineedit displays the data properly but it does not seem to submit my changes. I get the error: QSqlQuery::value: not positioned on a valid record.

    Other problems:

    1. The spinbox does not seem to reflect the model data
    2. My "next" button only seems to go through 2 of the 4 data rows
    3. I have a hangnail but that's another issue entirely

    Any suggestions on what I'm doing wrong would be much appreciated. See full code below. (In my actual script, the QTableview is not actually present, I just have it here to help me visualize the table as I experiment)

    Thanks for any advice!

    @
    from PySide.QtGui import *
    from PySide.QtCore import *
    from PySide.QtSql import *
    import sys
    import sqlite3 as lite

    class MainWindow(QMainWindow):
    def init(self):
    super(MainWindow, self).init()
    self.c = CentralWidget()
    self.setCentralWidget(self.c)
    self.setWindowTitle("Mapping Widgets?")
    self.resize(300,250)
    self.show()

    class CentralWidget(QWidget):
    def init(self):
    super(CentralWidget, self).init()
    self.grid = QGridLayout()
    self.next = QPushButton("Next")

        self.text = QLineEdit()
        self.spin = QSpinBox()
        #self.text.textChanged.connect(self.submit)
        self.setupModel()
        self.grid.addWidget(self.next, 0,0)
        self.grid.addWidget(self.text, 1,0)
        self.grid.addWidget(self.spin, 1,1)
        self.setLayout(self.grid)
    
    def setupModel(self):
        global dbfile
    
        db = QSqlDatabase.addDatabase("QSQLITE")
        db.setDatabaseName(dbfile)
        ok = db.open()
        if not ok:
            print("Invalid Database")
            return
    
        self.model = QSqlTableModel()
        self.model.setTable("TestModel")
        self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
        self.model.removeColumn(0)
        self.model.select()
    
        self.view = QTableView()
        self.view.setModel(self.model)
        self.grid.addWidget(self.view, 2,0,2,2)
    
        self.mapper = QDataWidgetMapper()
        self.mapper.setModel(self.model)
        self.mapper.addMapping(self.text, 0)
        self.mapper.addMapping(self.spin, 1)
        self.mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit)
        self.mapper.toFirst()
    
        self.next.clicked.connect(self.mapper.toNext)
    
    # def submit(self):
    #     print("text changed")
    #     self.mapper.submit()
    

    def createAndPopulate():
    global dbfile
    #con, cur = opendb(r'./data/testingPyQTSQLModel.sqlite')
    con, cur = opendb(dbfile)

    ok = cur.execute("DROP TABLE IF EXISTS TestModel")
    print(ok)
    ok = cur.execute("CREATE TABLE TestModel \
                (ID INTEGER PRIMARY KEY AUTOINCREMENT,\
                Text1 VARCHAR(255),\
                Int1 INTEGER)")
    
    ok = cur.execute("INSERT INTO TestModel VALUES( \
                null, 'ABC', 235)")
    
    cur.execute("INSERT INTO TestModel VALUES( \
                null, 'CAT', 13)")
    
    cur.execute("INSERT INTO TestModel VALUES( \
                null, 'BLUE', 62)")
    
    cur.execute("INSERT INTO TestModel VALUES( \
                null, 'FLOPPITY', 34)")
    con.commit()
    
    closedb(con)
    

    def opendb(dbfile):
    con = lite.connect(dbfile)
    with con:
    cur = con.cursor()
    return con, cur

    def closedb(con):
    con.close()

    def main():
    global dbfile

    dbfile = 'test.sqlite'
    createAndPopulate()
    app = QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())
    

    if name == 'main':
    main()
    @



  • Hmm, the problem seems to be coming from the removecolumn(0) in line 45. When I comment out this line, the text editing works as expected.

    is there a way around this?

    Also, I still can't get the spinboxes to send changes to the model/SQL data.

    Thanks


Log in to reply
 

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