InsertRow
-
@Duy-Khang
Please read the documentation rather than asking each time. It is beneficial to you to understand, not just be told. There is even an example at https://doc.qt.io/qt-5/qsqltablemodel.html#details. -
@Duy-Khang
Please read the documentation rather than asking each time. It is beneficial to you to understand, not just be told. There is even an example at https://doc.qt.io/qt-5/qsqltablemodel.html#details. -
@Duy-Khang said in InsertRow:
do you know how to disable or turn off the these indexs in this picture ?
Hide the horizontal and vertical header.
-
@Duy-Khang
If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!
In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.
-
@Duy-Khang said in InsertRow:
do you know how to disable or turn off the these indexs in this picture ?
Hide the horizontal and vertical header.
@Christian-Ehrlicher thank you.
-
@Duy-Khang
If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!
In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.
-
@Duy-Khang
If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!
In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.
-
What names do you want there ?
-
The same as usual.
As for setting the header data, it's shown in the QSqlTableModel documentation.
-
What issue do you have with using insertRow ?
-
yes, i have done it and then how can I insert a row after the current row, assuming we don't have any rows at first, I use QSqlTableModel.
-
import sys from PyQt5.QtWidgets import QWidget, QApplication,QTableView, QVBoxLayout,QPushButton from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel,QSqlRecord class demo(QWidget): def __init__(self): super().__init__() lyt = QVBoxLayout() self.setLayout(lyt) tview = QTableView() btn = QPushButton('add') lyt.addWidget(tview) lyt.addWidget(btn) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(r"xxx.db") self.model = testModel(db) tview.setModel(self.model) btn.clicked.connect(self.insertNewRecord) def insertNewRecord(self): r = QSqlRecord() r.setValue('name', 'hello') r.setValue('age', 23) if self.model.insertRecord(0, r): print(self.model.submitAll()) print(self.model.lastError().text()) else: self.model.db.rollback() print(self.model.lastError().text()) self.model.select() class testModel(QSqlTableModel): def __init__(self, db): super().__init__() self.db = db print(self.db.open()) self.setTable('test') self.setEditStrategy(QSqlTableModel.OnManualSubmit) print(self.select()) print(self.lastError().text()) print(self.tableName()) if __name__ == '__main__': app = QApplication(sys.argv) t = demo() t.show() sys.exit(app.exec())
data is displayed properly, but when i try to insertRecord, something wrong "No fields to update ". tableview not updated, let alone the database.
how to insert a record into the table?
-
import sys from PyQt5.QtWidgets import QWidget, QApplication,QTableView, QVBoxLayout,QPushButton from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel,QSqlRecord class demo(QWidget): def __init__(self): super().__init__() lyt = QVBoxLayout() self.setLayout(lyt) tview = QTableView() btn = QPushButton('add') lyt.addWidget(tview) lyt.addWidget(btn) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(r"xxx.db") self.model = testModel(db) tview.setModel(self.model) btn.clicked.connect(self.insertNewRecord) def insertNewRecord(self): r = QSqlRecord() r.setValue('name', 'hello') r.setValue('age', 23) if self.model.insertRecord(0, r): print(self.model.submitAll()) print(self.model.lastError().text()) else: self.model.db.rollback() print(self.model.lastError().text()) self.model.select() class testModel(QSqlTableModel): def __init__(self, db): super().__init__() self.db = db print(self.db.open()) self.setTable('test') self.setEditStrategy(QSqlTableModel.OnManualSubmit) print(self.select()) print(self.lastError().text()) print(self.tableName()) if __name__ == '__main__': app = QApplication(sys.argv) t = demo() t.show() sys.exit(app.exec())
data is displayed properly, but when i try to insertRecord, something wrong "No fields to update ". tableview not updated, let alone the database.
how to insert a record into the table?
@ArthurPYQT
Since you callprint(self.model.lastError().text())
both wheninsertRecord(0, r)
succeeds and when it fails, do you want to tell us (actually prove) which route your code is following...? -
@ArthurPYQT
Since you callprint(self.model.lastError().text())
both wheninsertRecord(0, r)
succeeds and when it fails, do you want to tell us (actually prove) which route your code is following...?@JonB
self.model.insertRecord(0, r)
returns True,self.model.submitAll()
returns False,self.model.lastError().text()
returns "No fields to update" -
@JonB
self.model.insertRecord(0, r)
returns True,self.model.submitAll()
returns False,self.model.lastError().text()
returns "No fields to update"@ArthurPYQT I finally found the solution. it should be
r=self.model.record()
rather thanr=QSqlRecord()
. And I 'vd come up with a new question, is QSqlRelationalTableModel a must if I want to display data from two tables and write data back? -
@ArthurPYQT I finally found the solution. it should be
r=self.model.record()
rather thanr=QSqlRecord()
. And I 'vd come up with a new question, is QSqlRelationalTableModel a must if I want to display data from two tables and write data back?@ArthurPYQT
QSqlRelationalTableModel
is only for: if you have a foreign key relationship in your database between the two tables, it allows you to display to the user the "mapped" values for viewing/editing. Typical example:- Table A has its own PK plus other columns. One column is a
country_id
number. - That number is from Table B, which has
country_id
as its PK plus acountry_name
column.
If you do this via
QSqlRelationalTableModel
and use aQSqlRelationalDelegate
, it will show the "mapped" country name from Table B for the column in Table A which is the country id. And if you allow editing it will present the user with a combobox of all the country names in Table B to pick from when editing the column in Table A.However, it is not mandatory to use QSRTM. You can do viewing/updating with or without. Note that what I have described is all it does: people sometimes expect more of it. In particular, it does not have any support for editing the foreign key table (Table B).
- Table A has its own PK plus other columns. One column is a