조회 수 7752 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

QList나 QVector등과 같은 컨테이너에 new로 생성된 객체가 적재되어 있을때 어느 시점에는 삭제(delete)를 해줘야 한다.

 

그럴때 qDeleteAll함수를 사용하면 매우 유용한데 이 함수는 C++의 delete 연산자를 사용하여 컨테이너의 모든 항목을 삭제해준다.

 

void qDeleteAll(ForwardIterator begin, ForwardIterator end)

void qDeleteAll(const Container & c)

 

간단한 예제 코드이다.

#include <QtAlgorithms> // qDeleteAll함수 관련 헤더 포함

QList<Employee *> list;
list.append(new Employee());
list.append(new Employee());

qDebug() << "count: " << list.count();

qDeleteAll(list.begin(), list.end());

qDebug() << "count: " << list.count(); // 컨테이너의 항목을 제거하지 않는다는 것에 유의하자.

list.clear(); // 컨테이너의 항목을 모두 지운다.

qDebug() << "count: " << list.count(); 

 

다른 예를 들면 QVBoxLayout에 자식 위젯들이 있다고 가정했을때 다음과 같이 사용할 수 있다.

QGroupBox * box = new QGroupBox;
box->setObjectName("PushButtonBox");

QPushButton *button1 = new QPushButton();
QPushButton *button2 = new QPushButton();
QPushButton *button3 = new QPushButton();

QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->addWidget(button1);
vlayout->addWidget(button2);
vlayout->addWidget(button3);

box->setLayout(vlayout);

box->show();

qDebug() << "before: " << box->children();
qDebug() << "count: " << box->children().count();

qDeleteAll(box->children());

qDebug() << "after: " << box->children();
qDebug() << "count: " << box->children().count(); // 여기선 안심하자.

 

TAG •

  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views437265
    read more
  2. No Image

    컨테이너에 적재된 객체를 편리하게 삭제하기

    Date2019.09.18 CategoryGeneral and Desktop Bymakersweb Views7752
    Read More
  3. C++로 구현된 모델을 QML의 ListView에서 참조

    Date2019.09.07 CategoryQML and Qt Quick Bymakersweb Views11272
    Read More
  4. No Image

    QSocketNotifier로 파일 디스크립터의 활동감지

    Date2019.08.28 CategoryQML and Qt Quick Bymakersweb Views10224
    Read More
  5. No Image

    MCU용 Qt에 대해서

    Date2019.08.22 CategoryMobile and Embedded Bymakersweb Views8415
    Read More
  6. [Qt News] Qt for Python을 위한 기술 비전

    Date2019.08.20 CategoryGeneral and Desktop Byj2doll Views21676
    Read More
  7. No Image

    열거형(enum)을 QML에서 사용하는 방법과 문자열(QString)로 얻기

    Date2019.08.20 CategoryQML and Qt Quick Bymakersweb Views11724
    Read More
  8. No Image

    [Qt News] Qt 6 기술 비전 (Technical vision for Qt 6)

    Date2019.08.08 CategoryGeneral and Desktop Byj2doll Views14086
    Read More
  9. No Image

    [Qt News] Qt6 Git 개발 초기 단계 시작하기

    Date2019.08.02 CategoryInstallation and Deployment Byj2doll Views14534
    Read More
  10. No Image

    [Qt] Google Play의 향후 요구 사항을 준수하는 방법

    Date2019.07.29 CategoryQML and Qt Quick Byj2doll Views13962
    Read More
  11. No Image

    Qt기반의 오픈소스 프로젝트들 - 2

    Date2019.07.21 CategoryGeneral and Desktop By운영자 Views15431
    Read More
  12. No Image

    QML, 이미지, 폰트등을 바이너리 리소스로 만들기

    Date2019.06.24 CategoryInstallation and Deployment Bymakersweb Views9302
    Read More
  13. Qt Creator에서 임베디드 장치로 deploy설정(Custom Process Step)

    Date2019.06.15 CategoryInstallation and Deployment Bymakersweb Views13116
    Read More
  14. Qt Quick Controls 2사용 및 스타일 설정

    Date2019.06.07 CategoryQML and Qt Quick Bymakersweb Views13806
    Read More
  15. No Image

    QML 강좌 - 동적 Listing (ListView)

    Date2019.06.01 CategoryQML and Qt Quick Bymakersweb Views16048
    Read More
  16. QtInstallerFramework로 온라인 설치프로그램(Online Installer)만드는 방법

    Date2019.05.28 CategoryInstallation and Deployment Bymakersweb Views14245
    Read More
  17. QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2)

    Date2019.05.26 CategoryQML and Qt Quick Bymakersweb Views31993
    Read More
  18. No Image

    QML에서 멀티 스레드(multithreading) 프로그래밍

    Date2019.05.25 CategoryQML and Qt Quick Bymakersweb Views9153
    Read More
  19. No Image

    QtSerialPort를 사용한 시리얼(Serial)통신

    Date2019.05.21 CategoryGeneral and Desktop Bymakersweb Views22875
    Read More
  20. No Image

    Qt기반의 오픈소스 프로젝트들

    Date2019.05.15 CategoryGeneral and Desktop Bymakersweb Views14909
    Read More
  21. No Image

    Q_D매크로와 d-pointer

    Date2019.05.07 CategoryGeneral and Desktop Bymakersweb Views8857
    Read More
Board Pagination Prev 1 4 5 6 7 8 9 Next
/ 9