首页 > 解决方案 > 如何将 QModelIndex 设置为 QListView

问题描述

我真的是 QT 的菜鸟,所以对我很轻松所以我有 2 个 QListviews 1. TimeList 2. DateList 我想做的是,当我在 Timelist 中选择一个项目时,将选择 Datelist 中的相同索引,然后我将发送数据到日期时间编辑

我尝试使用 QAbstractItemmodel model->index(row,column); 和 createindex 但我不太了解语法所以我把它搞砸了

QModelIndex i = ui->TimeList->currentIndex(); //it gets the index when an item is clicked
ui->DateList->setcurrentIndex(i); //it sets the same index in the other QListView
ui->DateList->clearSelection();
ui->DateList->selectionModel()->select(i, QItemSelectionModel::Select); //This highlights the same index in other QListView and it works fine


QTime t = i.data(Qt::DisplayRole).toTime(); // It converts to QTime fine
i = ui->DateList->currentindex(); // i try to change the value of index to get the date
QDate d = i.data(Qt::DisplayRole).toDate(); 

ui->TimeEdit->setTime(t);
ui->DateEdit->setDate(d);

我收集到的是 i.data 输出对 QDate 无效,因为它在 qDebug 中这样说。所以我想索引不仅包含行和列值,而且如何将它分配给 Datelist 超出了我的范围。(: 提前致谢

标签: qt

解决方案


我只需要学习语法来创建一个新的 QIndexModel

QModelIndex i = ui->TimeList->currentIndex();
QModelIndex i2 = ui->DateList->model()->index(i.row(), 0);

所以我使用 index1 中的相同行值,但它分配给第二个 QListview


推荐阅读