mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-04 16:25:27 +08:00
qt 6.5.1 original
This commit is contained in:
17
examples/widgets/tutorials/modelview/5_edit/5_edit.pro
Normal file
17
examples/widgets/tutorials/modelview/5_edit/5_edit.pro
Normal file
@ -0,0 +1,17 @@
|
||||
TARGET = mv_edit
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
QT += widgets
|
||||
requires(qtConfig(tableview))
|
||||
|
||||
SOURCES += main.cpp \
|
||||
mainwindow.cpp \
|
||||
mymodel.cpp
|
||||
|
||||
HEADERS += mainwindow.h \
|
||||
mymodel.h
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tutorials/modelview/5_edit
|
||||
INSTALLS += target
|
38
examples/widgets/tutorials/modelview/5_edit/CMakeLists.txt
Normal file
38
examples/widgets/tutorials/modelview/5_edit/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(mv_edit LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tutorials/modelview/5_edit")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(mv_edit
|
||||
main.cpp
|
||||
mainwindow.cpp mainwindow.h
|
||||
mymodel.cpp mymodel.h
|
||||
)
|
||||
|
||||
set_target_properties(mv_edit PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(mv_edit PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS mv_edit
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
13
examples/widgets/tutorials/modelview/5_edit/main.cpp
Normal file
13
examples/widgets/tutorials/modelview/5_edit/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();
|
||||
}
|
20
examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp
Normal file
20
examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "mymodel.h"
|
||||
|
||||
#include <QTableView>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, tableView(new QTableView(this))
|
||||
{
|
||||
setCentralWidget(tableView);
|
||||
auto *myModel = new MyModel(this);
|
||||
tableView->setModel(myModel);
|
||||
|
||||
// transfer changes to the model to the window title
|
||||
connect(myModel, &MyModel::editCompleted,
|
||||
this, &QWidget::setWindowTitle);
|
||||
}
|
24
examples/widgets/tutorials/modelview/5_edit/mainwindow.h
Normal file
24
examples/widgets/tutorials/modelview/5_edit/mainwindow.h
Normal file
@ -0,0 +1,24 @@
|
||||
// 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 QTableView; // forward declaration
|
||||
QT_END_NAMESPACE
|
||||
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
private:
|
||||
QTableView *tableView;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
60
examples/widgets/tutorials/modelview/5_edit/mymodel.cpp
Normal file
60
examples/widgets/tutorials/modelview/5_edit/mymodel.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mymodel.h"
|
||||
|
||||
MyModel::MyModel(QObject *parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
int MyModel::rowCount(const QModelIndex & /*parent*/) const
|
||||
{
|
||||
return ROWS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
int MyModel::columnCount(const QModelIndex & /*parent*/) const
|
||||
{
|
||||
return COLS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole && checkIndex(index))
|
||||
return m_gridData[index.row()][index.column()];
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
//! [quoting mymodel_e]
|
||||
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (role == Qt::EditRole) {
|
||||
if (!checkIndex(index))
|
||||
return false;
|
||||
//save value from editor to member m_gridData
|
||||
m_gridData[index.row()][index.column()] = value.toString();
|
||||
//for presentation purposes only: build and emit a joined string
|
||||
QString result;
|
||||
for (int row = 0; row < ROWS; row++) {
|
||||
for (int col= 0; col < COLS; col++)
|
||||
result += m_gridData[row][col] + ' ';
|
||||
}
|
||||
emit editCompleted(result);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//! [quoting mymodel_e]
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
//! [quoting mymodel_f]
|
||||
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||
}
|
||||
//! [quoting mymodel_f]
|
33
examples/widgets/tutorials/modelview/5_edit/mymodel.h
Normal file
33
examples/widgets/tutorials/modelview/5_edit/mymodel.h
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef MYMODEL_H
|
||||
#define MYMODEL_H
|
||||
|
||||
//! [Quoting ModelView Tutorial]
|
||||
// mymodel.h
|
||||
#include <QAbstractTableModel>
|
||||
#include <QString>
|
||||
|
||||
const int COLS= 3;
|
||||
const int ROWS= 2;
|
||||
|
||||
|
||||
class MyModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MyModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
private:
|
||||
QString m_gridData[ROWS][COLS]; //holds text entered into QTableView
|
||||
signals:
|
||||
void editCompleted(const QString &);
|
||||
};
|
||||
//! [Quoting ModelView Tutorial]
|
||||
|
||||
#endif // MYMODEL_H
|
Reference in New Issue
Block a user