mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-08 02:17:43 +08:00
qt 6.5.1 original
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
TARGET = mv_selections
|
||||
TEMPLATE = app
|
||||
QT += widgets
|
||||
requires(qtConfig(treeview))
|
||||
SOURCES += main.cpp \
|
||||
mainwindow.cpp
|
||||
HEADERS += mainwindow.h
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tutorials/modelview/7_selections
|
||||
INSTALLS += target
|
@ -0,0 +1,37 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(mv_selections LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tutorials/modelview/7_selections")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(mv_selections
|
||||
main.cpp
|
||||
mainwindow.cpp mainwindow.h
|
||||
)
|
||||
|
||||
set_target_properties(mv_selections PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(mv_selections PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS mv_selections
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
13
examples/widgets/tutorials/modelview/7_selections/main.cpp
Normal file
13
examples/widgets/tutorials/modelview/7_selections/main.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
//! [quoting modelview_a]
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QTreeView>
|
||||
#include <QStandardItemModel>
|
||||
#include <QItemSelectionModel>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, treeView(new QTreeView(this))
|
||||
, standardModel(new QStandardItemModel(this))
|
||||
{
|
||||
setCentralWidget(treeView);
|
||||
auto *rootNode = standardModel->invisibleRootItem();
|
||||
|
||||
|
||||
// defining a couple of items
|
||||
auto *americaItem = new QStandardItem("America");
|
||||
auto *mexicoItem = new QStandardItem("Canada");
|
||||
auto *usaItem = new QStandardItem("USA");
|
||||
auto *bostonItem = new QStandardItem("Boston");
|
||||
auto *europeItem = new QStandardItem("Europe");
|
||||
auto *italyItem = new QStandardItem("Italy");
|
||||
auto *romeItem = new QStandardItem("Rome");
|
||||
auto *veronaItem = new QStandardItem("Verona");
|
||||
|
||||
// building up the hierarchy
|
||||
rootNode-> appendRow(americaItem);
|
||||
rootNode-> appendRow(europeItem);
|
||||
americaItem-> appendRow(mexicoItem);
|
||||
americaItem-> appendRow(usaItem);
|
||||
usaItem-> appendRow(bostonItem);
|
||||
europeItem-> appendRow(italyItem);
|
||||
italyItem-> appendRow(romeItem);
|
||||
italyItem-> appendRow(veronaItem);
|
||||
|
||||
// register the model
|
||||
treeView->setModel(standardModel);
|
||||
treeView->expandAll();
|
||||
|
||||
// selection changes shall trigger a slot
|
||||
QItemSelectionModel *selectionModel = treeView->selectionModel();
|
||||
connect(selectionModel, &QItemSelectionModel::selectionChanged,
|
||||
this, &MainWindow::selectionChangedSlot);
|
||||
}
|
||||
//! [quoting modelview_a]
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
//! [quoting modelview_b]
|
||||
void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection & /*oldSelection*/)
|
||||
{
|
||||
// get the text of the selected item
|
||||
const QModelIndex index = treeView->selectionModel()->currentIndex();
|
||||
QString selectedText = index.data(Qt::DisplayRole).toString();
|
||||
// find out the hierarchy level of the selected item
|
||||
int hierarchyLevel = 1;
|
||||
QModelIndex seekRoot = index;
|
||||
while (seekRoot.parent().isValid()) {
|
||||
seekRoot = seekRoot.parent();
|
||||
hierarchyLevel++;
|
||||
}
|
||||
QString showString = QString("%1, Level %2").arg(selectedText)
|
||||
.arg(hierarchyLevel);
|
||||
setWindowTitle(showString);
|
||||
}
|
||||
//! [quoting modelview_b]
|
||||
|
||||
|
@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTreeView; // forward declarations
|
||||
class QStandardItemModel;
|
||||
class QItemSelection;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void selectionChangedSlot(const QItemSelection &newSelection, const QItemSelection &oldSelection);
|
||||
|
||||
private:
|
||||
QTreeView *treeView;
|
||||
QStandardItemModel *standardModel;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
Reference in New Issue
Block a user