QList和QVector的一些基本用法

本文详细介绍了Qt中QList与QVector的基本用法,包括数据结构体与类的定义,QList的创建、添加、修改、删除操作,以及如何使用迭代器和排序函数。特别关注了指针类型在QList中的处理方式,如内存管理、遍历和排序技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

QList和QVector基本用法一样

QList<T> list 和QList<T*>list

T可以是数据类型或者指针(类指针)

定义一个数据结构体

typedef struct student

{

    int id;

    QString name;

    QString sex;

    int age;

    QString des;

 

}student;

 

或者定义一个类

class Facility : public QObject

{

    Q_OBJECT

public:

    explicit Facility(QObject *parent = nullptr);

    QString name;

signals:

public slots:

}

 

1、创建QList

(1)T为非指针的数据结构

QList<student> SList;
    //非指针
    for (int i=0;i<10;i++) {
       student s1;
       s1.id=i;
       s1.name = "哇哈哈"+tr("%1").arg(i);
       s1.age=10+i;
       SList.append(s1);

 }

(2) T为指针类型

QList<Facility *> flist;
  Facility *P = new Facility();
  P->name = "哈哈哈";
  flist.push_back(P);
  Facility *P1 = new Facility();
  P1->name = "大神把";
flist.push_back(P1);

2、添加、插入

void append(const T&value)     在QList尾部插入value

 void append(const TQList<T>&value)       在QList尾部插入QList<T>&value

void insert ( int i, const T & value )      在QList其中某个位置插入value,假如没 声明i ,i 默认size()及在最后插入value

 iterator insert ( iterator before, const T & value )    在迭代器的前个位置插入value并返回当前迭代器的位置

3、修改值

QList.at(i) 不能修改值

(1)、可以用下标修改值,然后replace

for (int i=0;i<SList.size();i++) {
        if (SList[i].id%2) {
            SList[i].id=SList[i].id*-1;
            SList.replace(i,SList[i]);
        }

    }

(2)、用迭代器修改值

QList<student *>::iterator i;
   for (i = SList.begin();i!=SList.end();) {
       if ((*i)->id%2) {
           (*i)->id=(*i)->id*(-1);
       }
       ++i;
   }

4、删除item (一般使用迭代器)

注意内存泄露

不要在foreach里面进行元素的remove/add操作,remove请使用Iterator方式

(1)T非指针,直接removeOne或erase 全部删除用clear

(2)指针类型的,必须先delete *i 然后removeOne或erase

全部删除QList先删除指针内存再删除item 可以使用qDeleteAll(flist);flist.clear();
使用迭代器删除item时注意下标
QList<Facility *>::iterator i=flist.begin();
    for (;i!=flist.end();++i) {
      if ((*i)->name.contains("大")) {
         (*i)->name = (*i)->name.replace("大","小");         
          delete *i//指针需要删除内存
          flist.removeOne(*i);
        //  flist.erase(i);
          i--;  // 注意i的位置要回退一位
      }
  }

5、排序

使用qSort函数

bool compareData(const student &infoA, const student &infoB)
{
    return infoA.id < infoB.id;

}

 

qSort(SList.begin(),SList.end(),compareData);//按id排序

 

或者冒泡排序

//冒泡排序

    for (int i=0;i<SList.size();i++) {

        for (int j=i+1;j<SList.size();j++) {

            if (SList[j].id>SList[i].id)//互换

            {

                student temp;

                temp = SList[i];

                SList[i]=SList[j];

                SList[j]=temp;

            }

        }

    }

 

D:\QTproject\QtDxf\dxfhelper.cpp:352: error: C2665: “QList<QVector3D>::append”: 没有重载函数可以转换所有参数类型 D:\QTproject\QtDxf\dxfhelper.cpp(352): error C2665: “QList<QVector3D>::append”: 没有重载函数可以转换所有参数类型 D:\QT\6.5.3\msvc2019_64\include\QtCore/qlist.h(447): note: 可能是“void QList<QVector3D>::append(QList<QVector3D> &&)” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: “void QList<QVector3D>::append(QList<QVector3D> &&)”: 无法将参数 1 从“QVector2D”转换为“QList<QVector3D> &&” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 原因如下: 无法从“QVector2D”转换为“QList<QVector3D>” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符 D:\QT\6.5.3\msvc2019_64\include\QtCore/qlist.h(443): note: 或 “void QList<QVector3D>::append(const QList<QVector3D> &)” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: “void QList<QVector3D>::append(const QList<QVector3D> &)”: 无法将参数 1 从“QVector2D”转换为“const QList<QVector3D> &” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 原因如下: 无法从“QVector2D”转换为“const QList<QVector3D>” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符 D:\QT\6.5.3\msvc2019_64\include\QtCore/qlist.h(435): note: 或 “void QList<QVector3D>::append(QVector3D &&)” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: “void QList<QVector3D>::append(QVector3D &&)”: 无法将参数 1 从“QVector2D”转换为“QVector3D &&” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 原因如下: 无法从“QVector2D”转换为“T” with [ T=QVector3D ] D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符 D:\QT\6.5.3\msvc2019_64\include\QtCore/qlist.h(433): note: 或 “void QList<QVector3D>::append(const QVector3D &)” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: “void QList<QVector3D>::append(const QVector3D &)”: 无法将参数 1 从“QVector2D”转换为“const QVector3D &” D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 原因如下: 无法从“QVector2D”转换为“const T” with [ T=QVector3D ] D:\QTproject\QtDxf\dxfhelper.cpp(352): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符 D:\QTproject\QtDxf\d
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值