• 6173阅读
  • 2回复

【提问】实现headerData()后为什么程序老崩溃? [复制链接]

上一主题 下一主题
离线kytexzy
 

只看楼主 倒序阅读 楼主  发表于: 2006-02-06
我自定义了一个类:
class MaterialInputModel : public QAbstractTableModel;
我没有实现headerData()函数时程序到可以运行,但我加入headerData()函数后:
QVariant MaterialInputModel::headerData(int section,
                          Qt::Orientation orientation,
                          int role) const
{
  if (role != Qt::DisplayRole)
    return QVariant();
  if (orientation == Qt::Horizontal) {
    return QString(tr("Column %1")).arg(section);
  } else {
    // Qt::Vertical
  }
}

每当我调用insertRow()向 MaterialInputModel中插入数据时程序就马上崩溃了,终端显示:
*** glibc detected *** free(): invalid pointer: 0xbfffee68 ***
问题在哪里呢?
[ 此贴被XChinux在2006-02-06 23:35重新编辑 ]
try to find forgiveness for yourself and forgive others
离线kytexzy

只看该作者 1楼 发表于: 2006-02-07
问题找到了,因为这个函数需要一个返回值:
QVariant MaterialInputModel::headerData(int section,
                Qt::Orientation orientation,
                int role) const
{
if (role != Qt::DisplayRole)
  return QVariant();
if (orientation == Qt::Horizontal) {
  return QString(tr("Column %1")).arg(section);
} else {
  // Qt::Vertical
}
return QVariant();

}
在函数末尾加入一个返回语句
return QVariant();

就可以了。
try to find forgiveness for yourself and forgive others
离线aesoplord

只看该作者 2楼 发表于: 2011-02-15
这个问题编译的时候应该有警告吧
快速回复
限100 字节
 
上一个 下一个