note
- 个人理解, QListView 用于 显示数据,而数据的逻辑维护则由 QStandardItemModel 完成.
QStandardItemModel 创建
if (nullptr == pchannel_list_view_model_)
pchannel_list_view_model_ = new(std::nothrow) QStandardItemModel(ui->channel_list_view);
QListView添加QStandardItemModel
/// 绑定数据源
ui->channel_list_view->setModel(pchannel_list_view_model_);
QListView设置为不可编辑
/// 设置为不可编辑
ui->channel_list_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
添加行
standard_item_ex* item1 = new(std::nothrow) standard_item_ex(tr("new_channel"));
pchannel_list_view_model_->appendRow(item1);
note
- standard_item_ex 的原型如下, 我对其做了扩展
class standard_item_ex : public QStandardItem
{
public:
explicit standard_item_ex(const QString& str_txt);
standard_item_ex(const QIcon &icon, const QString &text);
~standard_item_ex();
....
}
QListView 显示添加数据
ui->channel_list_view->update();
QListView滚动的最下面
ui->channel_list_view->scrollToBottom();
QListView获取选中的行
QItemSelectionModel *selmodel = ui->channel_list_view->selectionModel();
if (selmodel)
{
/// 解析: indexlist 即可
QModelIndexList indexlist = selmodel->selectedIndexes();
}
获取选择的行和列
- 这里演示了 绑定的doublelciked的信号 和 下文的槽函数
void channel_widget::slot_list_view_item_double_clicked_(const QModelIndex & model_index)
{
/// 得到行列索引
int row_index = model_index.row();
int column_index = model_index.column();
}
根据行和列获取item
const int channel_row_index = 0;
const int column_index = 0;
QModelIndex& model_index = pchannel_list_view_model_->index(channel_row_index, column_index);
pchannel_list_view_model_->setData(model_index, str_new_name);