【问题标题】:error: conversion from 'QModelIndex' to non-scalar type 'QString'错误:从“QModelIndex”转换为非标量类型“QString”
【发布时间】:2015-07-02 13:11:00
【问题描述】:

这里是菜鸟 :) 我需要一些关于我的新项目的帮助。 运行我的项目时出现此错误:

错误:请求从“QModelIndex”转换为非标量类型“QString” QString str = ui->okullar->selectionModel()->currentIndex().parent().parent();

  qDebug() << ui->okullar->selectionModel()->currentIndex().parent().parent();
  QString str = ui->okullar->selectionModel()->currentIndex().parent().parent();

  QRegExp rx("[, ]");// match a comma or a space
  QStringList list = str.split(rx, QString::SkipEmptyParts);

实际上 qDebug(在第一行)给了我:

QModelIndex(0,0,0x137cb798,QStandardItemModel(0x28fdb4))

我只需要解析它...而 ui->okullar 是一个 qviewtree...

【问题讨论】:

  • 解析完这种字符串后你真正想得到什么?为什么?
  • 我需要知道点击的项目是组还是子...
  • 在 QModelIndex() 中你有行和列你能从他们那里知道你需要什么吗?
  • 我的模型没有列。

标签: c++ qt qt4 qt5


【解决方案1】:

您可以使用QModelIndex::data从模型中获取数据

ui->okullar->selectionModel()->currentIndex().parent().parent().data().toString()

【讨论】:

  • QList::operator[] 中的 ASSERT 失败:“索引超出范围”,文件 ../../../../Qt/5.4/mingw491_32/include/QtCore /qlist.h,第 486 行 此应用程序已请求运行时以不寻常的方式终止它。请联系应用程序的支持团队以获取更多信息。程序意外结束。
  • 在检索数据之前检查您的索引是否有效。 QModelIndex::isValid.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 2015-12-01
  • 2012-10-27
  • 1970-01-01
相关资源
最近更新 更多