Making our List Widget functional

Writing code, however, is still required in order for the widgets to be fully functional in your application. Let's learn how to add items to our item view widgets using C++ code!

First, open up mainwindow.cpp and write the following code to the class constructor, right after ui->setupui(this):

ui->listWidget->addItem("My Test Item"); 

As simple as that, you have successfully added an item to the List Widget!

There is another way to add an item to the List Widget. But before that, we must add the following headers to mainwindow.h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 
 
#include <QMainWindow> 
#include <QDebug> ...

Get Hands-On GUI Programming with C++ and Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.