BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Qt5 Tutorial QMutableListIterator - 2020





Bookmark and Share





bogotobogo.com site search:




QMutableListIterator

In this tutorial, we will learn about QMutableListIterator.

The QMutableListIterator class provides a Java-style non-const iterator for QList.



QMutableListIterator<T> allows us to iterate over a QList<<T>> and modify the list. If we don't want to modify the list (or have a const QList), use the slightly faster QListIterator<T> instead.

#include <QCoreApplication>
#include <QList>
#include <QDebug>
#include <QListIterator>
#include <QMutableListIterator>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    QList<int> List;

    for(int i = 0; i < 10; i++) List.append(i);

    // The QMutableListIterator constructor takes a QList
    // as argument. After construction, the iterator
    // is located at the very beginning of the list
    // (before the first item)

    QMutableListIterator<int> mIter(List);

    qDebug() << "Modifying element while moving forward...";
    while(mIter.hasNext())
    {
        // The next() function returns the next item
        // in the list and advances the iterator.
        // To remove items as we iterate over the list,
        // we use remove().
        // To modify the value of an item,
        // we use setValue().

        int val = mIter.next();

        // odd: remove, even: negate
        if (val % 2 ==  1) mIter.remove();
        else if (val % 2 ==  0) mIter.setValue(-val);

    }

    qDebug() << "Start again from front...";

    // toFront() voves the iterator to the front
    // of the container (before the first item).
    mIter.toFront();

    qDebug() << "Forward...";
    while(mIter.hasNext()) qDebug() << mIter.next();

    return a.exec();
}

Output:

Modifying element while moving forward...
Start again from front...
Forward...
0
-2
-4
-6
-8

Note that the QMutableListIterator constructor takes a QList as argument. After construction, the iterator is located at the very beginning of the list (before the first item).

We used remove() to remove items as we iterate over the list. To modify the value of an item, we used setValue().

The toFront() moves the iterator to the front of the container (before the first item).












Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Qt 5 Tutorial



Hello World

Signals and Slots

Q_OBJECT Macro

MainWindow and Action

MainWindow and ImageViewer using Designer A

MainWindow and ImageViewer using Designer B

Layouts

Layouts without Designer

Grid Layouts

Splitter

QDir

QFile (Basic)

Resource Files (.qrc)

QComboBox

QListWidget

QTreeWidget

QAction and Icon Resources

QStatusBar

QMessageBox

QTimer

QList

QListIterator

QMutableListIterator

QLinkedList

QMap

QHash

QStringList

QTextStream

QMimeType and QMimeDatabase

QFile (Serialization I)

QFile (Serialization II - Class)

Tool Tips in HTML Style and with Resource Images

QPainter

QBrush and QRect

QPainterPath and QPolygon

QPen and Cap Style

QBrush and QGradient

QPainter and Transformations

QGraphicsView and QGraphicsScene

Customizing Items by inheriting QGraphicsItem

QGraphicsView Animation

FFmpeg Converter using QProcess

QProgress Dialog - Modal and Modeless

QVariant and QMetaType

QtXML - Writing to a file

QtXML - QtXML DOM Reading

QThreads - Introduction

QThreads - Creating Threads

Creating QThreads using QtConcurrent

QThreads - Priority

QThreads - QMutex

QThreads - GuiThread

QtConcurrent QProgressDialog with QFutureWatcher

QSemaphores - Producer and Consumer

QThreads - wait()

MVC - ModelView with QListView and QStringListModel

MVC - ModelView with QTreeView and QDirModel

MVC - ModelView with QTreeView and QFileSystemModel

MVC - ModelView with QTableView and QItemDelegate

QHttp - Downloading Files

QNetworkAccessManager and QNetworkRequest - Downloading Files

Qt's Network Download Example - Reconstructed

QNetworkAccessManager - Downloading Files with UI and QProgressDialog

QUdpSocket

QTcpSocket

QTcpSocket with Signals and Slots

QTcpServer - Client and Server

QTcpServer - Loopback Dialog

QTcpServer - Client and Server using MultiThreading

QTcpServer - Client and Server using QThreadPool

Asynchronous QTcpServer - Client and Server using QThreadPool

Qt Quick2 QML Animation - A

Qt Quick2 QML Animation - B

Short note on Ubuntu Install

OpenGL with QT5

Qt5 Webkit : Web Browser with QtCreator using QWebView Part A

Qt5 Webkit : Web Browser with QtCreator using QWebView Part B

Video Player with HTML5 QWebView and FFmpeg Converter

Qt5 Add-in and Visual Studio 2012

Qt5.3 Installation on Ubuntu 14.04

Qt5.5 Installation on Ubuntu 14.04

Short note on deploying to Windows




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong













Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master