mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 23:35:28 +08:00
qt 6.5.1 original
This commit is contained in:
38
examples/widgets/itemviews/fetchmore/CMakeLists.txt
Normal file
38
examples/widgets/itemviews/fetchmore/CMakeLists.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(fetchmore LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/fetchmore")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(fetchmore
|
||||
filelistmodel.cpp filelistmodel.h
|
||||
main.cpp
|
||||
window.cpp window.h
|
||||
)
|
||||
|
||||
set_target_properties(fetchmore PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(fetchmore PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS fetchmore
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
12
examples/widgets/itemviews/fetchmore/fetchmore.pro
Normal file
12
examples/widgets/itemviews/fetchmore/fetchmore.pro
Normal file
@ -0,0 +1,12 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(listview))
|
||||
|
||||
HEADERS = filelistmodel.h \
|
||||
window.h
|
||||
SOURCES = filelistmodel.cpp \
|
||||
main.cpp \
|
||||
window.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/fetchmore
|
||||
INSTALLS += target
|
95
examples/widgets/itemviews/fetchmore/filelistmodel.cpp
Normal file
95
examples/widgets/itemviews/fetchmore/filelistmodel.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "filelistmodel.h"
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QDir>
|
||||
#include <QPalette>
|
||||
|
||||
static const int batchSize = 100;
|
||||
|
||||
FileListModel::FileListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{}
|
||||
|
||||
//![4]
|
||||
int FileListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
return parent.isValid() ? 0 : fileCount;
|
||||
}
|
||||
|
||||
QVariant FileListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return {};
|
||||
|
||||
const int row = index.row();
|
||||
if (row >= fileList.size() || row < 0)
|
||||
return {};
|
||||
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
return fileList.at(row).fileName();
|
||||
case Qt::BackgroundRole: {
|
||||
const int batch = row / batchSize;
|
||||
const QPalette &palette = QGuiApplication::palette();
|
||||
return (batch % 2) != 0 ? palette.alternateBase() : palette.base();
|
||||
}
|
||||
case Qt::DecorationRole:
|
||||
return iconProvider.icon(fileList.at(row));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
//![4]
|
||||
|
||||
QFileInfo FileListModel::fileInfoAt(const QModelIndex &index) const
|
||||
{
|
||||
return fileList.at(index.row());
|
||||
}
|
||||
|
||||
//![1]
|
||||
bool FileListModel::canFetchMore(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return false;
|
||||
return (fileCount < fileList.size());
|
||||
}
|
||||
//![1]
|
||||
|
||||
//![2]
|
||||
void FileListModel::fetchMore(const QModelIndex &parent)
|
||||
{
|
||||
if (parent.isValid())
|
||||
return;
|
||||
const int start = fileCount;
|
||||
const int remainder = int(fileList.size()) - start;
|
||||
const int itemsToFetch = qMin(batchSize, remainder);
|
||||
|
||||
if (itemsToFetch <= 0)
|
||||
return;
|
||||
|
||||
beginInsertRows(QModelIndex(), start, start + itemsToFetch - 1);
|
||||
|
||||
fileCount += itemsToFetch;
|
||||
|
||||
endInsertRows();
|
||||
|
||||
emit numberPopulated(path, start, itemsToFetch, int(fileList.size()));
|
||||
}
|
||||
//![2]
|
||||
|
||||
//![0]
|
||||
void FileListModel::setDirPath(const QString &path)
|
||||
{
|
||||
QDir dir(path);
|
||||
|
||||
beginResetModel();
|
||||
this->path = path;
|
||||
fileList = dir.entryInfoList(QDir::NoDot | QDir::AllEntries, QDir::Name);
|
||||
fileCount = 0;
|
||||
endResetModel();
|
||||
}
|
||||
//![0]
|
||||
|
42
examples/widgets/itemviews/fetchmore/filelistmodel.h
Normal file
42
examples/widgets/itemviews/fetchmore/filelistmodel.h
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef FILELISTMODEL_H
|
||||
#define FILELISTMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QFileInfoList>
|
||||
#include <QFileIconProvider>
|
||||
|
||||
//![0]
|
||||
class FileListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FileListModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
QFileInfo fileInfoAt(const QModelIndex &) const;
|
||||
|
||||
signals:
|
||||
void numberPopulated(const QString &path, int start, int number, int total);
|
||||
|
||||
public slots:
|
||||
void setDirPath(const QString &path);
|
||||
|
||||
protected:
|
||||
bool canFetchMore(const QModelIndex &parent) const override;
|
||||
void fetchMore(const QModelIndex &parent) override;
|
||||
|
||||
private:
|
||||
QFileInfoList fileList;
|
||||
QString path;
|
||||
QFileIconProvider iconProvider;
|
||||
int fileCount = 0;
|
||||
};
|
||||
//![0]
|
||||
|
||||
#endif // FILELISTMODEL_H
|
14
examples/widgets/itemviews/fetchmore/main.cpp
Normal file
14
examples/widgets/itemviews/fetchmore/main.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "window.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
Window window;
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
51
examples/widgets/itemviews/fetchmore/window.cpp
Normal file
51
examples/widgets/itemviews/fetchmore/window.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "window.h"
|
||||
#include "filelistmodel.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
Window::Window(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
model = new FileListModel(this);
|
||||
model->setDirPath(QDir::rootPath());
|
||||
|
||||
view = new QListView;
|
||||
view->setModel(model);
|
||||
|
||||
logViewer = new QPlainTextEdit(this);
|
||||
logViewer->setReadOnly(true);
|
||||
logViewer->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
|
||||
QSizePolicy::Preferred));
|
||||
|
||||
connect(model, &FileListModel::numberPopulated,
|
||||
this, &Window::updateLog);
|
||||
connect(view, &QAbstractItemView::activated,
|
||||
this, &Window::activated);
|
||||
|
||||
auto *layout = new QVBoxLayout(this);
|
||||
layout->addWidget(view);
|
||||
layout->addWidget(logViewer);
|
||||
|
||||
setWindowTitle(tr("Fetch More Example"));
|
||||
}
|
||||
|
||||
void Window::updateLog(const QString &path, int start, int number, int total)
|
||||
{
|
||||
const int last = start + number - 1;
|
||||
const QString nativePath = QDir::toNativeSeparators(path);
|
||||
const QString message = tr("%1..%2/%3 items from \"%4\" added.")
|
||||
.arg(start).arg(last).arg(total).arg(nativePath);
|
||||
logViewer->appendPlainText(message);
|
||||
}
|
||||
|
||||
void Window::activated(const QModelIndex &index)
|
||||
{
|
||||
const QFileInfo fi = model->fileInfoAt(index);
|
||||
if (fi.isDir()) {
|
||||
logViewer->clear();
|
||||
model->setDirPath(fi.absoluteFilePath());
|
||||
}
|
||||
}
|
34
examples/widgets/itemviews/fetchmore/window.h
Normal file
34
examples/widgets/itemviews/fetchmore/window.h
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QModelIndex;
|
||||
class QListView;
|
||||
class QPlainTextEdit;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class FileListModel;
|
||||
|
||||
class Window : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Window(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void updateLog(const QString &path, int start, int number, int total);
|
||||
void activated(const QModelIndex &);
|
||||
|
||||
private:
|
||||
QPlainTextEdit *logViewer;
|
||||
FileListModel *model;
|
||||
QListView *view;
|
||||
};
|
||||
|
||||
#endif // WINDOW_H
|
Reference in New Issue
Block a user