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 QListIterator - 2020





Bookmark and Share





bogotobogo.com site search:




QListIterator

In this tutorial, we will learn about QListIterator.

QList has both Java-style iterators and STL-style iterators. The Java-style iterators are more high-level and easier to use than the STL-style iterators; on the other hand, they are slightly less efficient.

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



QListIterator<T> allows us to iterate over a QList<T>. If we want to modify the list as we iterate over it, use QMutableListIterator<T> instead.

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

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

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

    // The QListIterator constructor takes a QList
    // as argument. After construction, the iterator
    // is located at the very beginning of the list
    // (before the first item)
    QListIterator<int> iter(List);

    qDebug() << "Forward...";
    while(iter.hasNext())
    {
        // The next() function returns the next item
        // in the list and advances the iterator.
        qDebug() << iter.next();
    }

    qDebug() << "Backward...";
    while(iter.hasPrevious())
    {
        // The next() function returns the next item
        // in the list and advances the iterator.
        qDebug() << iter.previous();
    }

    return a.exec();
}

Output:

Forward...
0
1
2
3
4
5
6
7
8
9
Backward...
9
8
7
6
5
4
3
2
1
0

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

The hasNext() returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns false.

The next() function returns the next item in the list and advances the iterator. Unlike STL-style iterators, Java-style iterators point between items rather than directly at items. The first call to next() advances the iterator to the position between the first and second item, and returns the first item; the second call to next() advances the iterator to the position between the second and third item, and returns the second item; and so on.

The hasPrevious() returns true if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns false.

The previous() returns the previous item and moves the iterator back by one position. Calling this function on an iterator located at the front of the container leads to undefined results.

We can peek before we iterate:

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

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

    // The QListIterator constructor takes a QList
    // as argument. After construction, the iterator
    // is located at the very beginning of the list
    // (before the first item)
    QListIterator<int> iter(List);

    qDebug() << "Forward...";
    while(iter.hasNext())
    {
        // The next() function returns the next item
        // in the list and advances the iterator.
        qDebug() << iter.next();
    }

    qDebug() << "Backward...";
    while(iter.hasPrevious())
    {
        // The next() function returns the next item
        // in the list and advances the iterator.
        qDebug() << iter.previous();
    }

    qDebug() << "Forward while peeking...";
    while(iter.hasNext())
    {
        qDebug() << "peek next ..." << iter.peekNext();
        qDebug() << iter.next();
    }

    return a.exec();
}

Output:

...
Forware while peeking...
peek next ... 0
0
peek next ... 1
1
peek next ... 2
2
peek next ... 3
3
peek next ... 4
4
peek next ... 5
5
peek next ... 6
6
peek next ... 7
7
peek next ... 8
8
peek next ... 9
9

The peekNext() Returns the next item without moving the iterator. Calling this function on an iterator located at the back of the container leads to undefined results.












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