如何在 QListView 中选择与底层 QSqlTableModel 最后插入的记录相对应的项目?

发布于 2024-10-04 05:00:07 字数 1033 浏览 4 评论 0原文

我正在编写某种地址簿。在我的应用程序的左侧,我有一个 QListView 来显示我的联系人姓名。在我的应用程序的右侧有一个表格,用于输入我的联系人信息(例如姓名、地址、电话号码)。我将联系人的数据存储在 QSqlTableModel 中。我使用 QListView 显示 QSqlTableModel 的一列。

我的问题是:如何自动选择 QListView 中与 QSqlTableModel 中最后插入的联系人相对应的项目?

这就是我设置模型的方式:

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

model = new QSqlTableModel(this, db);
model->setTable("demodaten");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setSort(0, Qt::AscendingOrder);
model->select();

view->setModel(model);
view->setModelColumn(0);

这就是我向模型添加新记录的方式:

QSqlRecord record = model->record();
for(int i = 0; i<record.count(); i++){
    record.setValue(i, "");
}

record.setValue("codenummer", p.getCodeNummer());
record.setValue("vorname", p.getVorname());
record.setValue("nachname", p.getNachname());
record.setValue("geburtsdatum", p.getGeburtsdatum());

model->insertRecord(-1, record);
model->submitAll();

I am programming some kind of address book. On the left side of my application i have a QListView to display the names of my contacts. On the right side of my application i have a form to type in the information (e.g. name, address, phone number) of my contacts. I am storing the data of my contacts in a QSqlTableModel. I use my QListView to display one column of my QSqlTableModel.

My question is: How do I automatically select the item in my QListView that corresponds to the last inserted contact in my QSqlTableModel?

This is how I set up my model:

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

model = new QSqlTableModel(this, db);
model->setTable("demodaten");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setSort(0, Qt::AscendingOrder);
model->select();

view->setModel(model);
view->setModelColumn(0);

This is how I add a new record to my model:

QSqlRecord record = model->record();
for(int i = 0; i<record.count(); i++){
    record.setValue(i, "");
}

record.setValue("codenummer", p.getCodeNummer());
record.setValue("vorname", p.getVorname());
record.setValue("nachname", p.getNachname());
record.setValue("geburtsdatum", p.getGeburtsdatum());

model->insertRecord(-1, record);
model->submitAll();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍孤女 2024-10-11 05:00:07

您可以连接到 QSqlTableModel 的 rowsInserted 信号;每次将新行插入模型时都应该触发它。在相应的槽中,使用列表视图的 selectionModel 方法选择插入的行。

在您的父小部件标头中定义模型和插槽:

private:
    QSqlTableModel *_model;

private slots:
    void on_rowsInserted(const QModelIndex &source_parent, int start, int end);

连接到小部件构造函数中模型的 rowsInserted 信号:

connect(_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(on_rowsInserted(QModelIndex,int,int)));

可能的 on_rowsInserted 实现:

void YourParentWidget::on_rowsInserted(const QModelIndex &source_parent, int start, int end)
{
    QModelIndex index = _model->index(start, 0);
    if (index.isValid())
    {
        ui->listView->selectionModel()->clear();
        ui->listView->selectionModel()->select(index, QItemSelectionModel::Select);
    }
}

希望这有帮助,问候

you can connect to the rowsInserted signal of your QSqlTableModel; it should be fired every time new rows are inserted into the model. In the corresponding slot select inserted rows using the listview's selectionModel method.

in your parent widget header define model and slot:

private:
    QSqlTableModel *_model;

private slots:
    void on_rowsInserted(const QModelIndex &source_parent, int start, int end);

connect to the model's rowsInserted signal in your widget constructor:

connect(_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(on_rowsInserted(QModelIndex,int,int)));

possible on_rowsInserted implementation:

void YourParentWidget::on_rowsInserted(const QModelIndex &source_parent, int start, int end)
{
    QModelIndex index = _model->index(start, 0);
    if (index.isValid())
    {
        ui->listView->selectionModel()->clear();
        ui->listView->selectionModel()->select(index, QItemSelectionModel::Select);
    }
}

hope this helps, regards

我的影子我的梦 2024-10-11 05:00:07

来自 Qt Doc:

bool QSqlTableModel::insertRecord ( int row, const QSqlRecord & record )
Inserts the record after row. If row is negative, the record will be appended to
the end. 

您要订购桌子吗?最后插入的项不是表的最后一项吗?

获取预先存在的索引:(

QModelIndex QAbstractItemModel::index ( int row, int column, const QModelIndex & parent = QModelIndex() ) 
int QAbstractItemModel::rowCount ( const QModelIndex & parent = QModelIndex() ) const

如果您有选择模式行,则列并不重要)

QPersistentModelIndex ( const QModelIndex & index )

在插入之前但在submitAll()之前;

并选择该行(如果您不介意更改当前索引,则不必为此使用选择模型)

void QAbstractItemView::setCurrentIndex ( const QModelIndex & index )

它也会选择该项目。

您还可以滚动以使新项目可见:

void QAbstractItemView::scrollTo ( const QModelIndex & index, ScrollHint hint = EnsureVisible )

编辑:
不知道为什么我之前没有看到订单......

From Qt Doc :

bool QSqlTableModel::insertRecord ( int row, const QSqlRecord & record )
Inserts the record after row. If row is negative, the record will be appended to
the end. 

Are you ordering the table? The last inserted item, is not the last item of the table?

Get a presisten index with:

QModelIndex QAbstractItemModel::index ( int row, int column, const QModelIndex & parent = QModelIndex() ) 
int QAbstractItemModel::rowCount ( const QModelIndex & parent = QModelIndex() ) const

(column is not important if you have selectionmode Row )

QPersistentModelIndex ( const QModelIndex & index )

anter the insert but before the submitAll();

And select that row with (you dont have to use a selection model for this if you dont mind to change the current index)

void QAbstractItemView::setCurrentIndex ( const QModelIndex & index )

It selects the item, too.

You can also scroll to make the new item visible:

void QAbstractItemView::scrollTo ( const QModelIndex & index, ScrollHint hint = EnsureVisible )

Edited:
Don't know how i didsnt see the order before ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文