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:
56
examples/sql/drilldown/CMakeLists.txt
Normal file
56
examples/sql/drilldown/CMakeLists.txt
Normal file
@ -0,0 +1,56 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(drilldown LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sql/drilldown")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Sql Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(drilldown
|
||||
../connection.h
|
||||
imageitem.cpp imageitem.h
|
||||
informationwindow.cpp informationwindow.h
|
||||
main.cpp
|
||||
view.cpp view.h
|
||||
)
|
||||
|
||||
set_target_properties(drilldown PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(drilldown PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Sql
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(drilldown_resource_files
|
||||
"images/qt-creator.png"
|
||||
"images/qt-logo.png"
|
||||
"images/qt-project.png"
|
||||
"images/qt-quick.png"
|
||||
)
|
||||
|
||||
qt_add_resources(drilldown "drilldown"
|
||||
PREFIX
|
||||
"/"
|
||||
FILES
|
||||
${drilldown_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS drilldown
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
15
examples/sql/drilldown/drilldown.pro
Normal file
15
examples/sql/drilldown/drilldown.pro
Normal file
@ -0,0 +1,15 @@
|
||||
HEADERS = ../connection.h \
|
||||
imageitem.h \
|
||||
informationwindow.h \
|
||||
view.h
|
||||
RESOURCES = drilldown.qrc
|
||||
SOURCES = imageitem.cpp \
|
||||
informationwindow.cpp \
|
||||
main.cpp \
|
||||
view.cpp
|
||||
QT += sql widgets
|
||||
requires(qtConfig(combobox))
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/sql/drilldown
|
||||
INSTALLS += target
|
8
examples/sql/drilldown/drilldown.qrc
Normal file
8
examples/sql/drilldown/drilldown.qrc
Normal file
@ -0,0 +1,8 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>images/qt-logo.png</file>
|
||||
<file>images/qt-quick.png</file>
|
||||
<file>images/qt-creator.png</file>
|
||||
<file>images/qt-project.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
84
examples/sql/drilldown/imageitem.cpp
Normal file
84
examples/sql/drilldown/imageitem.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "imageitem.h"
|
||||
|
||||
//! [0]
|
||||
ImageItem::ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent)
|
||||
: QGraphicsPixmapItem(pixmap, parent)
|
||||
{
|
||||
recordId = id;
|
||||
setAcceptHoverEvents(true);
|
||||
|
||||
timeLine.setDuration(150);
|
||||
timeLine.setFrameRange(0, 150);
|
||||
|
||||
connect(&timeLine, &QTimeLine::frameChanged, this, &ImageItem::setFrame);
|
||||
connect(&timeLine, &QTimeLine::finished, this, &ImageItem::updateItemPosition);
|
||||
|
||||
adjust();
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void ImageItem::hoverEnterEvent(QGraphicsSceneHoverEvent * /*event*/)
|
||||
{
|
||||
timeLine.setDirection(QTimeLine::Forward);
|
||||
|
||||
if (z != 1.0) {
|
||||
z = 1.0;
|
||||
updateItemPosition();
|
||||
}
|
||||
|
||||
if (timeLine.state() == QTimeLine::NotRunning)
|
||||
timeLine.start();
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void ImageItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * /*event*/)
|
||||
{
|
||||
timeLine.setDirection(QTimeLine::Backward);
|
||||
if (z != 0.0)
|
||||
z = 0.0;
|
||||
|
||||
if (timeLine.state() == QTimeLine::NotRunning)
|
||||
timeLine.start();
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
void ImageItem::setFrame(int frame)
|
||||
{
|
||||
adjust();
|
||||
QPointF center = boundingRect().center();
|
||||
|
||||
setTransform(QTransform::fromTranslate(center.x(), center.y()), true);
|
||||
setTransform(QTransform::fromScale(1 + frame / 330.0, 1 + frame / 330.0), true);
|
||||
setTransform(QTransform::fromTranslate(-center.x(), -center.y()), true);
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
void ImageItem::adjust()
|
||||
{
|
||||
setTransform(QTransform::fromScale(120 / boundingRect().width(),
|
||||
120 / boundingRect().height()));
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
int ImageItem::id() const
|
||||
{
|
||||
return recordId;
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
void ImageItem::updateItemPosition()
|
||||
{
|
||||
setZValue(z);
|
||||
}
|
||||
//! [6]
|
||||
|
||||
|
39
examples/sql/drilldown/imageitem.h
Normal file
39
examples/sql/drilldown/imageitem.h
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef IMAGEITEM_H
|
||||
#define IMAGEITEM_H
|
||||
|
||||
#include <QtCore>
|
||||
#include <QtWidgets/QGraphicsPixmapItem>
|
||||
|
||||
//! [0]
|
||||
class ImageItem : public QObject, public QGraphicsPixmapItem
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum { Type = UserType + 1 };
|
||||
|
||||
ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = nullptr);
|
||||
|
||||
int type() const override { return Type; }
|
||||
void adjust();
|
||||
int id() const;
|
||||
|
||||
protected:
|
||||
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
|
||||
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void setFrame(int frame);
|
||||
void updateItemPosition();
|
||||
|
||||
private:
|
||||
QTimeLine timeLine;
|
||||
int recordId;
|
||||
double z;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif
|
BIN
examples/sql/drilldown/images/qt-creator.png
Normal file
BIN
examples/sql/drilldown/images/qt-creator.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.5 KiB |
BIN
examples/sql/drilldown/images/qt-logo.png
Normal file
BIN
examples/sql/drilldown/images/qt-logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.4 KiB |
BIN
examples/sql/drilldown/images/qt-project.png
Normal file
BIN
examples/sql/drilldown/images/qt-project.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
examples/sql/drilldown/images/qt-quick.png
Normal file
BIN
examples/sql/drilldown/images/qt-quick.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
128
examples/sql/drilldown/informationwindow.cpp
Normal file
128
examples/sql/drilldown/informationwindow.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "informationwindow.h"
|
||||
|
||||
//! [0]
|
||||
InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *items,
|
||||
QWidget *parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
//! [0] //! [1]
|
||||
QLabel *itemLabel = new QLabel(tr("Item: "));
|
||||
QLabel *descriptionLabel = new QLabel(tr("Description: "));
|
||||
QLabel *imageFileLabel = new QLabel(tr("Image file: "));
|
||||
|
||||
createButtons();
|
||||
|
||||
itemText = new QLabel;
|
||||
descriptionEditor = new QTextEdit;
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
imageFileEditor = new QComboBox;
|
||||
imageFileEditor->setModel(items->relationModel(1));
|
||||
imageFileEditor->setModelColumn(items->relationModel(1)->fieldIndex("file"));
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
mapper = new QDataWidgetMapper(this);
|
||||
mapper->setModel(items);
|
||||
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
|
||||
mapper->setItemDelegate(new QSqlRelationalDelegate(mapper));
|
||||
mapper->addMapping(imageFileEditor, 1);
|
||||
mapper->addMapping(itemText, 2, "text");
|
||||
mapper->addMapping(descriptionEditor, 3);
|
||||
mapper->setCurrentIndex(id);
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
connect(descriptionEditor, &QTextEdit::textChanged, [=]() {
|
||||
enableButtons();
|
||||
});
|
||||
connect(imageFileEditor, &QComboBox::currentIndexChanged, [=]() {
|
||||
enableButtons();
|
||||
});
|
||||
|
||||
QFormLayout *formLayout = new QFormLayout;
|
||||
formLayout->addRow(itemLabel, itemText);
|
||||
formLayout->addRow(imageFileLabel, imageFileEditor);
|
||||
formLayout->addRow(descriptionLabel, descriptionEditor);
|
||||
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
layout->addLayout(formLayout);
|
||||
layout->addWidget(buttonBox);
|
||||
setLayout(layout);
|
||||
|
||||
itemId = id;
|
||||
displayedImage = imageFileEditor->currentText();
|
||||
|
||||
setWindowFlags(Qt::Window);
|
||||
enableButtons(false);
|
||||
setWindowTitle(itemText->text());
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
int InformationWindow::id() const
|
||||
{
|
||||
return itemId;
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
void InformationWindow::revert()
|
||||
{
|
||||
mapper->revert();
|
||||
enableButtons(false);
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
void InformationWindow::submit()
|
||||
{
|
||||
QString newImage(imageFileEditor->currentText());
|
||||
|
||||
if (displayedImage != newImage) {
|
||||
displayedImage = newImage;
|
||||
emit imageChanged(itemId, newImage);
|
||||
}
|
||||
|
||||
mapper->submit();
|
||||
mapper->setCurrentIndex(itemId);
|
||||
|
||||
enableButtons(false);
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
void InformationWindow::createButtons()
|
||||
{
|
||||
closeButton = new QPushButton(tr("&Close"));
|
||||
revertButton = new QPushButton(tr("&Revert"));
|
||||
submitButton = new QPushButton(tr("&Submit"));
|
||||
|
||||
closeButton->setDefault(true);
|
||||
|
||||
connect(closeButton, &QPushButton::clicked, this, &InformationWindow::close);
|
||||
connect(revertButton, &QPushButton::clicked, this, &InformationWindow::revert);
|
||||
connect(submitButton, &QPushButton::clicked, this, &InformationWindow::submit);
|
||||
//! [8]
|
||||
|
||||
//! [9]
|
||||
buttonBox = new QDialogButtonBox(this);
|
||||
buttonBox->addButton(submitButton, QDialogButtonBox::AcceptRole);
|
||||
buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
|
||||
buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
|
||||
}
|
||||
//! [9]
|
||||
|
||||
//! [10]
|
||||
void InformationWindow::enableButtons(bool enable)
|
||||
{
|
||||
revertButton->setEnabled(enable);
|
||||
submitButton->setEnabled(enable);
|
||||
}
|
||||
//! [10]
|
||||
|
||||
|
52
examples/sql/drilldown/informationwindow.h
Normal file
52
examples/sql/drilldown/informationwindow.h
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef INFORMATIONWINDOW_H
|
||||
#define INFORMATIONWINDOW_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
|
||||
//! [0]
|
||||
class InformationWindow : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
InformationWindow(int id, QSqlRelationalTableModel *items,
|
||||
QWidget *parent = nullptr);
|
||||
|
||||
int id() const;
|
||||
|
||||
signals:
|
||||
void imageChanged(int id, const QString &fileName);
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
private slots:
|
||||
void revert();
|
||||
void submit();
|
||||
void enableButtons(bool enable = true);
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
private:
|
||||
void createButtons();
|
||||
|
||||
int itemId;
|
||||
QString displayedImage;
|
||||
|
||||
QComboBox *imageFileEditor = nullptr;
|
||||
QLabel *itemText = nullptr;
|
||||
QTextEdit *descriptionEditor = nullptr;
|
||||
|
||||
QPushButton *closeButton = nullptr;
|
||||
QPushButton *submitButton = nullptr;
|
||||
QPushButton *revertButton = nullptr;
|
||||
QDialogButtonBox *buttonBox = nullptr;
|
||||
|
||||
QDataWidgetMapper *mapper = nullptr;
|
||||
};
|
||||
//! [2]
|
||||
|
||||
#endif
|
27
examples/sql/drilldown/main.cpp
Normal file
27
examples/sql/drilldown/main.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "view.h"
|
||||
#include "../connection.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(drilldown);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
|
||||
if (!createConnection())
|
||||
return EXIT_FAILURE;
|
||||
|
||||
View view("items", "images");
|
||||
view.show();
|
||||
#ifdef QT_KEYPAD_NAVIGATION
|
||||
QApplication::setNavigationMode(Qt::NavigationModeCursorAuto);
|
||||
#endif
|
||||
return app.exec();
|
||||
}
|
135
examples/sql/drilldown/view.cpp
Normal file
135
examples/sql/drilldown/view.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "informationwindow.h"
|
||||
#include "imageitem.h"
|
||||
#include "view.h"
|
||||
|
||||
//! [0]
|
||||
View::View(const QString &items, const QString &images, QWidget *parent)
|
||||
: QGraphicsView(parent)
|
||||
{
|
||||
itemTable = new QSqlRelationalTableModel(this);
|
||||
itemTable->setTable(items);
|
||||
itemTable->setRelation(1, QSqlRelation(images, "itemid", "file"));
|
||||
itemTable->select();
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
scene = new QGraphicsScene(this);
|
||||
scene->setSceneRect(0, 0, 465, 365);
|
||||
setScene(scene);
|
||||
|
||||
addItems();
|
||||
|
||||
setMinimumSize(470, 370);
|
||||
setMaximumSize(470, 370);
|
||||
|
||||
QLinearGradient gradient(QPointF(0, 0), QPointF(0, 370));
|
||||
gradient.setColorAt(0, QColor("#868482"));
|
||||
gradient.setColorAt(1, QColor("#5d5b59"));
|
||||
setBackgroundBrush(gradient);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [3]
|
||||
void View::addItems()
|
||||
{
|
||||
int itemCount = itemTable->rowCount();
|
||||
|
||||
int imageOffset = 150;
|
||||
int leftMargin = 70;
|
||||
int topMargin = 40;
|
||||
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
QSqlRecord record = itemTable->record(i);
|
||||
|
||||
int id = record.value("id").toInt();
|
||||
QString file = record.value("file").toString();
|
||||
QString item = record.value("itemtype").toString();
|
||||
|
||||
int columnOffset = ((i % 2) * 37);
|
||||
int x = ((i % 2) * imageOffset) + leftMargin + columnOffset;
|
||||
int y = ((i / 2) * imageOffset) + topMargin;
|
||||
|
||||
ImageItem *image = new ImageItem(id, QPixmap(":/" + file));
|
||||
image->setData(0, i);
|
||||
image->setPos(x, y);
|
||||
scene->addItem(image);
|
||||
|
||||
QGraphicsTextItem *label = scene->addText(item);
|
||||
label->setDefaultTextColor(QColor("#d7d6d5"));
|
||||
QPointF labelOffset((120 - label->boundingRect().width()) / 2, 120.0);
|
||||
label->setPos(QPointF(x, y) + labelOffset);
|
||||
}
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [5]
|
||||
void View::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (QGraphicsItem *item = itemAt(event->position().toPoint())) {
|
||||
if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item))
|
||||
showInformation(image);
|
||||
}
|
||||
QGraphicsView::mouseReleaseEvent(event);
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
void View::showInformation(ImageItem *image)
|
||||
{
|
||||
int id = image->id();
|
||||
if (id < 0 || id >= itemTable->rowCount())
|
||||
return;
|
||||
|
||||
InformationWindow *window = findWindow(id);
|
||||
if (!window) {
|
||||
window = new InformationWindow(id, itemTable, this);
|
||||
|
||||
connect(window, QOverload<int,const QString &>::of(&InformationWindow::imageChanged),
|
||||
this, QOverload<int,const QString &>::of(&View::updateImage));
|
||||
|
||||
window->move(pos() + QPoint(20, 40));
|
||||
window->show();
|
||||
informationWindows.append(window);
|
||||
}
|
||||
|
||||
if (window->isVisible()) {
|
||||
window->raise();
|
||||
window->activateWindow();
|
||||
} else
|
||||
window->show();
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
void View::updateImage(int id, const QString &fileName)
|
||||
{
|
||||
QList<QGraphicsItem *> items = scene->items();
|
||||
|
||||
while(!items.empty()) {
|
||||
QGraphicsItem *item = items.takeFirst();
|
||||
|
||||
if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) {
|
||||
if (image->id() == id){
|
||||
image->setPixmap(QPixmap(":/" +fileName));
|
||||
image->adjust();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
InformationWindow *View::findWindow(int id) const
|
||||
{
|
||||
for (auto window : informationWindows) {
|
||||
if (window && (window->id() == id))
|
||||
return window;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
//! [8]
|
||||
|
43
examples/sql/drilldown/view.h
Normal file
43
examples/sql/drilldown/view.h
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef VIEW_H
|
||||
#define VIEW_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
|
||||
class ImageItem;
|
||||
class InformationWindow;
|
||||
|
||||
//! [0]
|
||||
class View : public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
View(const QString &items, const QString &images, QWidget *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
private slots:
|
||||
void updateImage(int id, const QString &fileName);
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
private:
|
||||
void addItems();
|
||||
InformationWindow *findWindow(int id) const;
|
||||
void showInformation(ImageItem *image);
|
||||
|
||||
QGraphicsScene *scene;
|
||||
QList<InformationWindow *> informationWindows;
|
||||
//! [2] //! [3]
|
||||
QSqlRelationalTableModel *itemTable;
|
||||
};
|
||||
//! [3]
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user