当前位置: 代码迷 >> QT开发 >> 初学者这个qstringlist为什么会越界呢
  详细解决方案

初学者这个qstringlist为什么会越界呢

热度:57   发布时间:2016-04-25 03:51:01.0
菜鸟这个qstringlist为什么会越界呢?
QStringList test;
    test[0] = "ab";
    test[1] = "cd";
    qDebug()<<test[0];

------解决方案--------------------
QStringList 是个QList,你没有往list里添加对象,就调用[]操作,当然会越界。应该这样:

test << "ab" << "cd";
qDebug() << test[0];


T & QList::operator[] ( int i )
Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).

This function is very fast (constant time).

See also at() and value().
------解决方案--------------------
楼主还不太熟悉容器等泛型。要这样写:
QStringList test;
test<<"ab"<<"cd";

------解决方案--------------------
也可以用append函数添加