Search code examples
c++qtqlist

How to create a list or an object when starting a program in Qt?


I just started using Qt and had this doubt after reading some examples and trying stuff on my own. I want to create a list as soon as my program starts. It will be empty, waiting for the user to add items (or objects) to this list. My main example of doing this list thing was this example.

Now, when starting Qt, I get an .ui file in the "Forms" folder and as my mainwindow.cpp I get this:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Checking back the example, it doesn't have an .ui file associated with it. Even more important, and is what is making me wonder, this is what is used as the start of the Main Window:

MainWindow::MainWindow()
    : textEdit(new QTextEdit)
{
    setCentralWidget(textEdit);

    createActions();
    createStatusBar();
    createDockWindows();

    setWindowTitle(tr("Dock Widgets"));

    newLetter();
    setUnifiedTitleAndToolBarOnMac(true);
}

The "createDockWindows()" does the trick of creating the list and stuff in this example. I just wanna go with a simple:

QList <QString> GroceryShoppingList

Now, where do I put it? Like this (inside the method that creates the UI):

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QList<QString> GroceryShoppingList;
}

MainWindow::~MainWindow()
{
    delete ui;
}

If yes, everything I initialize with the program should also go inside this? Or only in this specific example? I've also based my question is this question. I wanted to know what this function does, since it seemed to be the one that started the MainWindow. Thanks in advance.

Bonus: How do I link the list I created with the program with the QListView that will display it in the ui?


Solution

  • Your list of objects is part of your application state, so this data cannot just be defined in a local variable in the MainWindow constructor.

    In your simple example it can be a member of your MainWindow class though:

    in mainwindow.h:

    class MainWindow :.....
    {
        ....
    private:
        QList<QString> items;
        ....
    }
    

    But, since you want to show it in a view in the UI, the most common case would be that the data is kept in the model of the view . Please see the documentation of how Qt's model and view classes work. Short version is that responsibilities are split between the visual presentation (view) and the data (model). You can start with a simple QStringListModel for your data.

    So instead of QList<QString> items you'd have

    in mainwindow.h:

    class MainWindow :....
    {
        ....
    private:
        QStringListModel model;
        ....
    }
    

    Add a ListView to the UI file in Designer or the Design mode in Qt Creator (i.e. open the mainwindow.ui file), and then in mainwindow.cpp:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        // "ui" contains members for each UI element that you added in
        // design mode. Look what the name of the list view is that you
        // added there, and adapt accordingly
        ui->listView->setModel(&model);
    
        // if you want, you can initialize your model with some items:
        model.setStringList({"Bananas", "DVD Player"});
    }