mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-04 08:15:30 +08:00
qt 6.5.1 original
This commit is contained in:
39
examples/widgets/itemviews/stardelegate/CMakeLists.txt
Normal file
39
examples/widgets/itemviews/stardelegate/CMakeLists.txt
Normal file
@ -0,0 +1,39 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(stardelegate LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/stardelegate")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(stardelegate
|
||||
main.cpp
|
||||
stardelegate.cpp stardelegate.h
|
||||
stareditor.cpp stareditor.h
|
||||
starrating.cpp starrating.h
|
||||
)
|
||||
|
||||
set_target_properties(stardelegate PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(stardelegate PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS stardelegate
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
68
examples/widgets/itemviews/stardelegate/main.cpp
Normal file
68
examples/widgets/itemviews/stardelegate/main.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include <QTableWidget>
|
||||
|
||||
#include "stardelegate.h"
|
||||
#include "stareditor.h"
|
||||
#include "starrating.h"
|
||||
|
||||
//! [0]
|
||||
void populateTableWidget(QTableWidget *tableWidget)
|
||||
{
|
||||
static constexpr struct {
|
||||
const char *title;
|
||||
const char *genre;
|
||||
const char *artist;
|
||||
int rating;
|
||||
} staticData[] = {
|
||||
//! [0] //! [1]
|
||||
{ "Mass in B-Minor", "Baroque", "J.S. Bach", 5 },
|
||||
//! [1]
|
||||
{ "Three More Foxes", "Jazz", "Maynard Ferguson", 4 },
|
||||
{ "Sex Bomb", "Pop", "Tom Jones", 3 },
|
||||
{ "Barbie Girl", "Pop", "Aqua", 5 },
|
||||
//! [2]
|
||||
{ nullptr, nullptr, nullptr, 0 }
|
||||
//! [2] //! [3]
|
||||
};
|
||||
//! [3] //! [4]
|
||||
|
||||
for (int row = 0; staticData[row].title != nullptr; ++row) {
|
||||
QTableWidgetItem *item0 = new QTableWidgetItem(staticData[row].title);
|
||||
QTableWidgetItem *item1 = new QTableWidgetItem(staticData[row].genre);
|
||||
QTableWidgetItem *item2 = new QTableWidgetItem(staticData[row].artist);
|
||||
QTableWidgetItem *item3 = new QTableWidgetItem;
|
||||
item3->setData(0,
|
||||
QVariant::fromValue(StarRating(staticData[row].rating)));
|
||||
|
||||
tableWidget->setItem(row, 0, item0);
|
||||
tableWidget->setItem(row, 1, item1);
|
||||
tableWidget->setItem(row, 2, item2);
|
||||
tableWidget->setItem(row, 3, item3);
|
||||
}
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QTableWidget tableWidget(4, 4);
|
||||
tableWidget.setItemDelegate(new StarDelegate);
|
||||
tableWidget.setEditTriggers(QAbstractItemView::DoubleClicked
|
||||
| QAbstractItemView::SelectedClicked);
|
||||
tableWidget.setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
tableWidget.setHorizontalHeaderLabels({"Title", "Genre", "Artist", "Rating"});
|
||||
|
||||
populateTableWidget(&tableWidget);
|
||||
|
||||
tableWidget.resizeColumnsToContents();
|
||||
tableWidget.resize(500, 300);
|
||||
tableWidget.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
//! [5]
|
90
examples/widgets/itemviews/stardelegate/stardelegate.cpp
Normal file
90
examples/widgets/itemviews/stardelegate/stardelegate.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "stardelegate.h"
|
||||
#include "stareditor.h"
|
||||
#include "starrating.h"
|
||||
|
||||
//! [0]
|
||||
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.data().canConvert<StarRating>()) {
|
||||
StarRating starRating = qvariant_cast<StarRating>(index.data());
|
||||
|
||||
if (option.state & QStyle::State_Selected)
|
||||
painter->fillRect(option.rect, option.palette.highlight());
|
||||
|
||||
starRating.paint(painter, option.rect, option.palette,
|
||||
StarRating::EditMode::ReadOnly);
|
||||
} else {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
//! [0]
|
||||
}
|
||||
|
||||
//! [1]
|
||||
QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.data().canConvert<StarRating>()) {
|
||||
StarRating starRating = qvariant_cast<StarRating>(index.data());
|
||||
return starRating.sizeHint();
|
||||
}
|
||||
return QStyledItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
QWidget *StarDelegate::createEditor(QWidget *parent,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
|
||||
{
|
||||
if (index.data().canConvert<StarRating>()) {
|
||||
StarEditor *editor = new StarEditor(parent);
|
||||
connect(editor, &StarEditor::editingFinished,
|
||||
this, &StarDelegate::commitAndCloseEditor);
|
||||
return editor;
|
||||
}
|
||||
return QStyledItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
void StarDelegate::setEditorData(QWidget *editor,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.data().canConvert<StarRating>()) {
|
||||
StarRating starRating = qvariant_cast<StarRating>(index.data());
|
||||
StarEditor *starEditor = qobject_cast<StarEditor *>(editor);
|
||||
starEditor->setStarRating(starRating);
|
||||
} else {
|
||||
QStyledItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
void StarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.data().canConvert<StarRating>()) {
|
||||
StarEditor *starEditor = qobject_cast<StarEditor *>(editor);
|
||||
model->setData(index, QVariant::fromValue(starEditor->starRating()));
|
||||
} else {
|
||||
QStyledItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
void StarDelegate::commitAndCloseEditor()
|
||||
{
|
||||
StarEditor *editor = qobject_cast<StarEditor *>(sender());
|
||||
emit commitData(editor);
|
||||
emit closeEditor(editor);
|
||||
}
|
||||
//! [5]
|
31
examples/widgets/itemviews/stardelegate/stardelegate.h
Normal file
31
examples/widgets/itemviews/stardelegate/stardelegate.h
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef STARDELEGATE_H
|
||||
#define STARDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
//! [0]
|
||||
class StarDelegate : public QStyledItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
using QStyledItemDelegate::QStyledItemDelegate;
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const override;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const override;
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const override;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const override;
|
||||
|
||||
private slots:
|
||||
void commitAndCloseEditor();
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif
|
14
examples/widgets/itemviews/stardelegate/stardelegate.pro
Normal file
14
examples/widgets/itemviews/stardelegate/stardelegate.pro
Normal file
@ -0,0 +1,14 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(tablewidget))
|
||||
|
||||
HEADERS = stardelegate.h \
|
||||
stareditor.h \
|
||||
starrating.h
|
||||
SOURCES = main.cpp \
|
||||
stardelegate.cpp \
|
||||
stareditor.cpp \
|
||||
starrating.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/stardelegate
|
||||
INSTALLS += target
|
63
examples/widgets/itemviews/stardelegate/stareditor.cpp
Normal file
63
examples/widgets/itemviews/stardelegate/stareditor.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "stareditor.h"
|
||||
#include "starrating.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
//! [0]
|
||||
StarEditor::StarEditor(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setMouseTracking(true);
|
||||
setAutoFillBackground(true);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
QSize StarEditor::sizeHint() const
|
||||
{
|
||||
return myStarRating.sizeHint();
|
||||
}
|
||||
|
||||
//! [1]
|
||||
void StarEditor::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter painter(this);
|
||||
myStarRating.paint(&painter, rect(), palette(),
|
||||
StarRating::EditMode::Editable);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void StarEditor::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
const int star = starAtPosition(event->position().toPoint().x());
|
||||
|
||||
if (star != myStarRating.starCount() && star != -1) {
|
||||
myStarRating.setStarCount(star);
|
||||
update();
|
||||
}
|
||||
QWidget::mouseMoveEvent(event);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
void StarEditor::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
emit editingFinished();
|
||||
QWidget::mouseReleaseEvent(event);
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
int StarEditor::starAtPosition(int x) const
|
||||
{
|
||||
const int star = (x / (myStarRating.sizeHint().width()
|
||||
/ myStarRating.maxStarCount())) + 1;
|
||||
if (star <= 0 || star > myStarRating.maxStarCount())
|
||||
return -1;
|
||||
|
||||
return star;
|
||||
}
|
||||
//! [4]
|
39
examples/widgets/itemviews/stardelegate/stareditor.h
Normal file
39
examples/widgets/itemviews/stardelegate/stareditor.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 STAREDITOR_H
|
||||
#define STAREDITOR_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "starrating.h"
|
||||
|
||||
//! [0]
|
||||
class StarEditor : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
StarEditor(QWidget *parent = nullptr);
|
||||
|
||||
QSize sizeHint() const override;
|
||||
void setStarRating(const StarRating &starRating) {
|
||||
myStarRating = starRating;
|
||||
}
|
||||
StarRating starRating() { return myStarRating; }
|
||||
|
||||
signals:
|
||||
void editingFinished();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void mouseMoveEvent(QMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
int starAtPosition(int x) const;
|
||||
|
||||
StarRating myStarRating;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif
|
60
examples/widgets/itemviews/stardelegate/starrating.cpp
Normal file
60
examples/widgets/itemviews/stardelegate/starrating.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 "starrating.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <cmath>
|
||||
|
||||
constexpr int PaintingScaleFactor = 20;
|
||||
|
||||
//! [0]
|
||||
StarRating::StarRating(int starCount, int maxStarCount)
|
||||
: myStarCount(starCount),
|
||||
myMaxStarCount(maxStarCount)
|
||||
{
|
||||
starPolygon << QPointF(1.0, 0.5);
|
||||
for (int i = 1; i < 5; ++i)
|
||||
starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14),
|
||||
0.5 + 0.5 * std::sin(0.8 * i * 3.14));
|
||||
|
||||
diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4)
|
||||
<< QPointF(0.6, 0.5) << QPointF(0.5, 0.6)
|
||||
<< QPointF(0.4, 0.5);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
QSize StarRating::sizeHint() const
|
||||
{
|
||||
return PaintingScaleFactor * QSize(myMaxStarCount, 1);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void StarRating::paint(QPainter *painter, const QRect &rect,
|
||||
const QPalette &palette, EditMode mode) const
|
||||
{
|
||||
painter->save();
|
||||
|
||||
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(mode == EditMode::Editable ?
|
||||
palette.highlight() :
|
||||
palette.windowText());
|
||||
|
||||
const int yOffset = (rect.height() - PaintingScaleFactor) / 2;
|
||||
painter->translate(rect.x(), rect.y() + yOffset);
|
||||
painter->scale(PaintingScaleFactor, PaintingScaleFactor);
|
||||
|
||||
for (int i = 0; i < myMaxStarCount; ++i) {
|
||||
if (i < myStarCount)
|
||||
painter->drawPolygon(starPolygon, Qt::WindingFill);
|
||||
else if (mode == EditMode::Editable)
|
||||
painter->drawPolygon(diamondPolygon, Qt::WindingFill);
|
||||
painter->translate(1.0, 0.0);
|
||||
}
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
//! [2]
|
39
examples/widgets/itemviews/stardelegate/starrating.h
Normal file
39
examples/widgets/itemviews/stardelegate/starrating.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 STARRATING_H
|
||||
#define STARRATING_H
|
||||
|
||||
#include <QPainter>
|
||||
#include <QPolygonF>
|
||||
#include <QSize>
|
||||
|
||||
//! [0]
|
||||
class StarRating
|
||||
{
|
||||
public:
|
||||
enum class EditMode { Editable, ReadOnly };
|
||||
|
||||
explicit StarRating(int starCount = 1, int maxStarCount = 5);
|
||||
|
||||
void paint(QPainter *painter, const QRect &rect,
|
||||
const QPalette &palette, EditMode mode) const;
|
||||
QSize sizeHint() const;
|
||||
int starCount() const { return myStarCount; }
|
||||
int maxStarCount() const { return myMaxStarCount; }
|
||||
void setStarCount(int starCount) { myStarCount = starCount; }
|
||||
void setMaxStarCount(int maxStarCount) { myMaxStarCount = maxStarCount; }
|
||||
|
||||
private:
|
||||
QPolygonF starPolygon;
|
||||
QPolygonF diamondPolygon;
|
||||
int myStarCount;
|
||||
int myMaxStarCount;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
Q_DECLARE_METATYPE(StarRating)
|
||||
//! [1]
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user