Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4/*
5 main.cpp
6
7 A simple example of how to access items from an existing model.
8*/
9
10#include <QApplication>
11#include <QLabel>
12#include <QVBoxLayout>
13#include <QWidget>
14
15#include <QFileSystemModel>
16#include <QPalette>
17
18#include <QDir>
19#include <QModelIndex>
20
21/*!
22 Create a default directory model and, using the index-based interface to
23 the model and some QLabel widgets, populate the window's layout with the
24 names of objects in the directory.
25*/
26
27int main(int argc, char *argv[])
28{
29 QApplication app(argc, argv);
30
31 QWidget window;
32 auto *layout = new QVBoxLayout(&window);
33 auto *title = new QLabel("Some items from the directory model", &window);
34 title->setBackgroundRole(QPalette::Base);
35 title->setMargin(8);
36 layout->addWidget(title);
37
38 //! [0]
39 auto *model = new QFileSystemModel;
40
41 auto onDirectoryLoaded = [model, layout, &window](const QString &directory) {
42 QModelIndex parentIndex = model->index(directory);
43 const int numRows = model->rowCount(parentIndex);
44 //! [1]
45 for (int row = 0; row < numRows; ++row) {
46 QModelIndex index = model->index(row, 0, parentIndex);
47 //! [1]
48
49 //! [2]
50 QString text = model->data(index, Qt::DisplayRole).toString();
51 //! [2]
52
53 // Display the text in a widget.
54 auto *label = new QLabel(text, &window);
55 layout->addWidget(label);
56 //! [3]
57 }
58 //! [3]
59 };
60
61 QObject::connect(model, &QFileSystemModel::directoryLoaded, onDirectoryLoaded);
62 model->setRootPath(QDir::currentPath());
63//! [0]
64
65 window.setWindowTitle("A simple model example");
66 window.show();
67 return app.exec();
68}
int main(int argc, char *argv[])
[ctor_close]