Crasher in PyQt6 QSqlTableModel

ekhumoro ekhumoro at gmx.com
Wed Dec 31 12:52:02 GMT 2025


Hello

Whilst porting some code from PyQt5 to PyQt6, I found that attempting to access QSqlTableModel.query() in PyQt6 causes a 
crash. The same code in PyQt5 has never had this problem.

A minimal test is given below, but unfortunately I am not able to provide a useful back-trace. However, the same test 
does not crash when run with PySide6 using the same Qt6 library, so this does seem to be a bug in PyQt6. I also tested 
using various other pip-installed versions of PyQt6 in a venv, including 6.5.3, 6.8.0 and 6.10.1 - but they all have the 
same problem. This is on Linux, using either Python-3.13 or Python-3.9.

Here's the test-case:

     from PyQt6.QtWidgets import QApplication
     from PyQt6.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
     # from PySide6.QtWidgets import QApplication
     # from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel

     app = QApplication(['Test'])

     db = QSqlDatabase.addDatabase('QSQLITE')
     db.setDatabaseName(':memory:')
     db.open()

     q = QSqlQuery(db)

     q.exec("""CREATE TABLE Test (Name, Value);""")
     q.exec("""INSERT INTO Test VALUES('foo', 1);""")
     q.exec("""INSERT INTO Test VALUES('bar', 2);""")

     db.commit()

     q.exec("""SELECT * FROM Test;""")
     while q.next():
         print(f'QUERY: {q.value(0)}, {q.value(1)}')
     q.finish()
     print(f'QUERY: active = {q.isActive()}')

     m = QSqlTableModel()
     m.setTable('Test')
     m.select()
     for index in range(m.rowCount()):
         r = m.record(index)
         print(f'MODEL: {r.value(0)}, {r.value(1)}')
     m.query().finish()
     print(f'MODEL: active = {m.query().isActive()}')

With PySide6, the output is:

     QUERY: foo, 1
     QUERY: bar, 2
     QUERY: active = False
     MODEL: foo, 1
     MODEL: bar, 2
     MODEL: active = False

But with PyQt6, it's:

     QUERY: foo, 1
     QUERY: bar, 2
     QUERY: active = False
     MODEL: foo, 1
     MODEL: bar, 2
     free(): invalid pointer
     Aborted (core dumped)



More information about the PyQt mailing list