Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
27int main(int argc, char *argv[])
28{
29 QApplication app(argc, argv);
30
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);
37
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);
45 for (int row = 0; row < numRows; ++row) {
46 QModelIndex index = model->index(row, 0, parentIndex);
48
52 // Display the text in a widget.
53 auto *label = new QLabel(text, &window);
56 }
58 };
59
61 model->setRootPath(QDir::currentPath());
63
64 window.setWindowTitle("A simple model example");
65 window.show();
66 return app.exec();
67}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment.
static QString currentPath()
Returns the absolute path of the application's current directory.
Definition qdir.cpp:2054
The QFileSystemModel class provides a data model for the local filesystem.
void directoryLoaded(const QString &path)
The QLabel widget provides a text or image display.
Definition qlabel.h:20
\inmodule QtCore
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QVariant data(const QModelIndex &item, int role=Qt::DisplayRole) const override
Returns the value for the specified item and role.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
If the database supports returning the size of a query (see QSqlDriver::hasFeature()),...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QVBoxLayout class lines up widgets vertically.
Definition qboxlayout.h:91
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
int main()
[0]
QString text
@ DisplayRole
GLuint index
[2]
GLuint GLsizei const GLchar * label
[43]
GLenum GLenum GLsizei void * row
QSqlQueryModel * model
[16]
QVBoxLayout * layout
QString title
[35]
QApplication app(argc, argv)
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]