InsertRow
-
wrote on 17 May 2021, 18:43 last edited by
How to insert a new row and new data to every cell of new row ? And how beginInsertRow() work ?
-
Hi,
What kind of model are you using ?
-
wrote on 18 May 2021, 02:34 last edited by Duy Khang
i'm using tableView
i used ui->tableView->model()->insertRow(0), but it does not work ! -
wrote on 18 May 2021, 07:33 last edited by JonB
@Duy-Khang
@SGaist asked you what model you are using. Not theQTableView
. What is yourui->tableView->model()
? BecauseinsertRow(0)
inserts a row into the model, and if you say "but it does not work !" then you have an issue in your model. Perhaps because you have your own model and have not implemented inserting rows correctly/at all? -
wrote on 18 May 2021, 07:49 last edited by
i don't know what is model and you can show me how to insert a new row and add data to every cell of new row ? I just want this but i don't know how to do it.
-
i don't know what is model and you can show me how to insert a new row and add data to every cell of new row ? I just want this but i don't know how to do it.
wrote on 18 May 2021, 07:52 last edited by JonB@Duy-Khang
Nobody can without knowing what model you are using. Only you know that, and you really should know what your model is. Have you tried reading the Model/View Tutorial? -
wrote on 18 May 2021, 08:01 last edited by
-
We all understood what you want to do.
What we are asking you is what type of model is set on that QTableView ?
QSqlTableModel ?
QSqlQueryModel ?
QStandardItemModel ?
Custom QAbstractTableModel based model ?
Other ? (to be defined) -
wrote on 18 May 2021, 08:11 last edited by
it is QSqlQueryModel.
-
wrote on 18 May 2021, 08:13 last edited by JonB
@Duy-Khang
At last! AQSqlQueryModel
is for querying SQL, it does not allow insertions, because it does not know where to insert any rows. It sounds like you should switch to aQSqlTableModel
for inserts etc.? I assume the rows you show are all in one table, which is where you want to insert/update/delete rows. -
wrote on 18 May 2021, 08:22 last edited by
oh,I don't know that before and how to insert new row if i use QSqlTableModel ?
-
wrote on 18 May 2021, 08:27 last edited by JonB
@Duy-Khang
The code you showed would work if the model is aQSqlTableModel
instead of aQSqlQueryModel
, and you have specified the table. You can search https://doc.qt.io/qt-5/qsqltablemodel.html and https://doc.qt.io/qt-5/qsqltablemodel-members.html forinsertRow
and see what you come across. There is alsoQSqlTableModel::insertRecord()
etc. if you prefer a record-based interface to a row-based interface, that is a matter of preference.BTW you don't need to worry about/implement the
beginInsertRows()
etc. you asked about if you use aQSqlTableModel
, its implementation handles that for you. -
wrote on 18 May 2021, 08:32 last edited by
and QSqlTableModel can not setQuery() how to do this ?
-
wrote on 18 May 2021, 08:41 last edited by
@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. -
wrote on 18 May 2021, 08:43 last edited by
Thank you very much
-
@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.
-
wrote on 18 May 2021, 17:58 last edited by JonB
@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.
wrote on 18 May 2021, 18:09 last edited by@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.
1/32