English 中文(简体)
为什么INSERT和DELETE会搞砸QTableView(Qt、C++、sqlite)?
原标题:Why do INSERT and DELETE screw up QTableView (Qt, C++, sqlite)?

QSqlQueryModel上设置INSERT或DELETE查询后,我的QTableView就被搞砸了。例如,我通过调用view->;隐藏了ID列;hideColumn(ID),但在INSERT或DELETE之后,ID列变为可见。

在这些情况下,我如何自动将视图重置为以前的设置?

最佳回答

我想问题出在QSqlQueryModel::setQuery每次重新加载内容和插入行时,您最终都会调用它。看看setQuery的实现,我建议:根据查询,您的模型可以重置,包括列设置更改,这应该会触发视图列更新。

正如Qt文件所示:

The QSqlQueryModel class provides a read-only data model for SQL result sets.

所以我会直接使用QSqlQuery调用数据更新,然后使用相同的查询重新加载模型。或者考虑切换到QSQLTableModel,它对于单表内容操作非常方便,并支持插入、更新和删除。看看下面的示例是否适用:

设置数据库、视图和模型:

QSqlTableModel *_model;
QTableView *_view;

。。。

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

QSqlQuery query;
query.prepare("CREATE TABLE IF NOT EXISTS person (id INTEGER UNIQUE PRIMARY KEY, name VARCHAR(30))");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ( test1 )");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ( test2 )");
query.exec();

_model = new QSqlTableModel(this, db);
_model->setTable("person");
_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
_model->select();
_model->setHeaderData(1, Qt::Horizontal, tr("name"));
_model->setSort(1, Qt::AscendingOrder);

_view = new QTableView(this);
_view->setModel(_model);
_view->hideColumn(0);

添加新行:

QSqlRecord record;
_model->insertRecord(-1, record);

删除所选行:

QModelIndexList selected = _view->selectionModel()->selectedIndexes();
for (int i = 0; i < selected.size(); ++i)
    _model->removeRows(selected.at(i).row(), 1);

提交更改:

_model->submitAll();

希望这有帮助,问候

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签