mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 15:55:27 +08:00
6.5.3 clean
This commit is contained in:
@ -0,0 +1,50 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(embeddeddialogs LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/graphicsview/embeddeddialogs")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(embeddeddialogs
|
||||
customproxy.cpp customproxy.h
|
||||
embeddeddialog.cpp embeddeddialog.h embeddeddialog.ui
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(embeddeddialogs PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(embeddeddialogs PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(embeddeddialogs_resource_files
|
||||
"No-Ones-Laughing-3.jpg"
|
||||
)
|
||||
|
||||
qt_add_resources(embeddeddialogs "embeddeddialogs"
|
||||
PREFIX
|
||||
"/"
|
||||
FILES
|
||||
${embeddeddialogs_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS embeddeddialogs
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
@ -0,0 +1,133 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "customproxy.h"
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QPainter>
|
||||
#include <QStyleOptionGraphicsItem>
|
||||
|
||||
CustomProxy::CustomProxy(QGraphicsItem *parent, Qt::WindowFlags wFlags)
|
||||
: QGraphicsProxyWidget(parent, wFlags), timeLine(new QTimeLine(250, this))
|
||||
{
|
||||
connect(timeLine, &QTimeLine::valueChanged,
|
||||
this, &CustomProxy::updateStep);
|
||||
connect(timeLine, &QTimeLine::stateChanged,
|
||||
this, &CustomProxy::stateChanged);
|
||||
}
|
||||
|
||||
QRectF CustomProxy::boundingRect() const
|
||||
{
|
||||
return QGraphicsProxyWidget::boundingRect().adjusted(0, 0, 10, 10);
|
||||
}
|
||||
|
||||
void CustomProxy::paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
||||
QWidget *widget)
|
||||
{
|
||||
const QColor color(0, 0, 0, 64);
|
||||
|
||||
QRectF r = windowFrameRect();
|
||||
QRectF right(r.right(), r.top() + 10, 10, r.height() - 10);
|
||||
QRectF bottom(r.left() + 10, r.bottom(), r.width(), 10);
|
||||
bool intersectsRight = right.intersects(option->exposedRect);
|
||||
bool intersectsBottom = bottom.intersects(option->exposedRect);
|
||||
if (intersectsRight && intersectsBottom) {
|
||||
QPainterPath path;
|
||||
path.addRect(right);
|
||||
path.addRect(bottom);
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(color);
|
||||
painter->drawPath(path);
|
||||
} else if (intersectsBottom) {
|
||||
painter->fillRect(bottom, color);
|
||||
} else if (intersectsRight) {
|
||||
painter->fillRect(right, color);
|
||||
}
|
||||
|
||||
QGraphicsProxyWidget::paintWindowFrame(painter, option, widget);
|
||||
}
|
||||
|
||||
void CustomProxy::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
||||
{
|
||||
QGraphicsProxyWidget::hoverEnterEvent(event);
|
||||
scene()->setActiveWindow(this);
|
||||
if (qFuzzyCompare(timeLine->currentValue(), 1))
|
||||
zoomIn();
|
||||
}
|
||||
|
||||
void CustomProxy::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
||||
{
|
||||
QGraphicsProxyWidget::hoverLeaveEvent(event);
|
||||
if (!popupShown
|
||||
&& (timeLine->direction() != QTimeLine::Backward || qFuzzyIsNull(timeLine->currentValue()))) {
|
||||
zoomOut();
|
||||
}
|
||||
}
|
||||
|
||||
bool CustomProxy::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
||||
{
|
||||
if (watched->isWindow()
|
||||
&& (event->type() == QEvent::UngrabMouse || event->type() == QEvent::GrabMouse)) {
|
||||
popupShown = watched->isVisible();
|
||||
if (!popupShown && !isUnderMouse())
|
||||
zoomOut();
|
||||
}
|
||||
return QGraphicsProxyWidget::sceneEventFilter(watched, event);
|
||||
}
|
||||
|
||||
QVariant CustomProxy::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
if (change == ItemChildAddedChange || change == ItemChildRemovedChange) {
|
||||
if (change == ItemChildAddedChange) {
|
||||
currentPopup = qvariant_cast<QGraphicsItem *>(value);
|
||||
currentPopup->setCacheMode(ItemCoordinateCache);
|
||||
if (scene())
|
||||
currentPopup->installSceneEventFilter(this);
|
||||
} else if (scene()) {
|
||||
currentPopup->removeSceneEventFilter(this);
|
||||
currentPopup = nullptr;
|
||||
}
|
||||
} else if (currentPopup && change == ItemSceneHasChanged) {
|
||||
currentPopup->installSceneEventFilter(this);
|
||||
}
|
||||
return QGraphicsProxyWidget::itemChange(change, value);
|
||||
}
|
||||
|
||||
void CustomProxy::updateStep(qreal step)
|
||||
{
|
||||
QRectF r = boundingRect();
|
||||
setTransform(QTransform()
|
||||
.translate(r.width() / 2, r.height() / 2)
|
||||
.rotate(step * 30, Qt::XAxis)
|
||||
.rotate(step * 10, Qt::YAxis)
|
||||
.rotate(step * 5, Qt::ZAxis)
|
||||
.scale(1 + 1.5 * step, 1 + 1.5 * step)
|
||||
.translate(-r.width() / 2, -r.height() / 2));
|
||||
}
|
||||
|
||||
void CustomProxy::stateChanged(QTimeLine::State state)
|
||||
{
|
||||
if (state == QTimeLine::Running) {
|
||||
if (timeLine->direction() == QTimeLine::Forward)
|
||||
setCacheMode(ItemCoordinateCache);
|
||||
} else if (state == QTimeLine::NotRunning) {
|
||||
if (timeLine->direction() == QTimeLine::Backward)
|
||||
setCacheMode(DeviceCoordinateCache);
|
||||
}
|
||||
}
|
||||
|
||||
void CustomProxy::zoomIn()
|
||||
{
|
||||
if (timeLine->direction() != QTimeLine::Forward)
|
||||
timeLine->setDirection(QTimeLine::Forward);
|
||||
if (timeLine->state() == QTimeLine::NotRunning)
|
||||
timeLine->start();
|
||||
}
|
||||
|
||||
void CustomProxy::zoomOut()
|
||||
{
|
||||
if (timeLine->direction() != QTimeLine::Backward)
|
||||
timeLine->setDirection(QTimeLine::Backward);
|
||||
if (timeLine->state() == QTimeLine::NotRunning)
|
||||
timeLine->start();
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef CUSTOMPROXY_H
|
||||
#define CUSTOMPROXY_H
|
||||
|
||||
#include <QTimeLine>
|
||||
#include <QGraphicsProxyWidget>
|
||||
|
||||
class CustomProxy : public QGraphicsProxyWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CustomProxy(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = { });
|
||||
|
||||
QRectF boundingRect() const override;
|
||||
void paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
||||
QWidget *widget) override;
|
||||
|
||||
protected:
|
||||
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
|
||||
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
|
||||
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
|
||||
|
||||
private slots:
|
||||
void updateStep(qreal step);
|
||||
void stateChanged(QTimeLine::State);
|
||||
void zoomIn();
|
||||
void zoomOut();
|
||||
|
||||
private:
|
||||
QTimeLine *timeLine;
|
||||
QGraphicsItem *currentPopup = nullptr;
|
||||
bool popupShown = false;
|
||||
};
|
||||
|
||||
#endif // CUSTOMPROXY_H
|
@ -0,0 +1,70 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "embeddeddialog.h"
|
||||
#include "ui_embeddeddialog.h"
|
||||
|
||||
#include <QStyleFactory>
|
||||
|
||||
EmbeddedDialog::EmbeddedDialog(QWidget *parent)
|
||||
: QDialog(parent)
|
||||
, ui(new Ui::EmbeddedDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->layoutDirection->setCurrentIndex(layoutDirection() != Qt::LeftToRight);
|
||||
|
||||
const QStringList styleKeys = QStyleFactory::keys();
|
||||
for (const QString &styleName : styleKeys) {
|
||||
ui->style->addItem(styleName);
|
||||
if (style()->objectName().toLower() == styleName.toLower())
|
||||
ui->style->setCurrentIndex(ui->style->count() - 1);
|
||||
}
|
||||
|
||||
connect(ui->layoutDirection, &QComboBox::activated,
|
||||
this, &EmbeddedDialog::layoutDirectionChanged);
|
||||
connect(ui->spacing, &QSlider::valueChanged,
|
||||
this, &EmbeddedDialog::spacingChanged);
|
||||
connect(ui->fontComboBox, &QFontComboBox::currentFontChanged,
|
||||
this, &EmbeddedDialog::fontChanged);
|
||||
connect(ui->style, &QComboBox::textActivated,
|
||||
this, &EmbeddedDialog::styleChanged);
|
||||
}
|
||||
|
||||
EmbeddedDialog::~EmbeddedDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void EmbeddedDialog::layoutDirectionChanged(int index)
|
||||
{
|
||||
setLayoutDirection(index == 0 ? Qt::LeftToRight : Qt::RightToLeft);
|
||||
}
|
||||
|
||||
void EmbeddedDialog::spacingChanged(int spacing)
|
||||
{
|
||||
layout()->setSpacing(spacing);
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
void EmbeddedDialog::fontChanged(const QFont &font)
|
||||
{
|
||||
setFont(font);
|
||||
}
|
||||
|
||||
static void setStyleHelper(QWidget *widget, QStyle *style)
|
||||
{
|
||||
widget->setStyle(style);
|
||||
widget->setPalette(style->standardPalette());
|
||||
const QObjectList children = widget->children();
|
||||
for (QObject *child : children) {
|
||||
if (QWidget *childWidget = qobject_cast<QWidget *>(child))
|
||||
setStyleHelper(childWidget, style);
|
||||
}
|
||||
}
|
||||
|
||||
void EmbeddedDialog::styleChanged(const QString &styleName)
|
||||
{
|
||||
QStyle *style = QStyleFactory::create(styleName);
|
||||
if (style)
|
||||
setStyleHelper(this, style);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef EMBEDDEDDIALOG_H
|
||||
#define EMBEDDEDDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
class EmbeddedDialog;
|
||||
}
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class EmbeddedDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
EmbeddedDialog(QWidget *parent = nullptr);
|
||||
~EmbeddedDialog();
|
||||
|
||||
private slots:
|
||||
void layoutDirectionChanged(int index);
|
||||
void spacingChanged(int spacing);
|
||||
void fontChanged(const QFont &font);
|
||||
void styleChanged(const QString &styleName);
|
||||
|
||||
private:
|
||||
Ui::EmbeddedDialog *ui;
|
||||
};
|
||||
|
||||
#endif // EMBEDDEDDIALOG_H
|
@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>EmbeddedDialog</class>
|
||||
<widget class="QDialog" name="EmbeddedDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>407</width>
|
||||
<height>134</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Embedded Dialog</string>
|
||||
</property>
|
||||
<layout class="QFormLayout" name="formLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Layout Direction:</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>layoutDirection</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="layoutDirection">
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Left to Right</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Right to Left</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Select Font:</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>fontComboBox</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QFontComboBox" name="fontComboBox"/>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>Style:</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>style</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QComboBox" name="style"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_4">
|
||||
<property name="text">
|
||||
<string>Layout spacing:</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>spacing</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QSlider" name="spacing">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
@ -0,0 +1,18 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(fontcombobox))
|
||||
|
||||
SOURCES += main.cpp
|
||||
SOURCES += customproxy.cpp embeddeddialog.cpp
|
||||
HEADERS += customproxy.h embeddeddialog.h
|
||||
|
||||
FORMS += embeddeddialog.ui
|
||||
RESOURCES += embeddeddialogs.qrc
|
||||
|
||||
build_all:!build_pass {
|
||||
CONFIG -= build_all
|
||||
CONFIG += release
|
||||
}
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/graphicsview/embeddeddialogs
|
||||
INSTALLS += target
|
@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource>
|
||||
<file>No-Ones-Laughing-3.jpg</file>
|
||||
</qresource>
|
||||
</RCC>
|
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "customproxy.h"
|
||||
#include "embeddeddialog.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsView>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QGraphicsScene scene;
|
||||
scene.setStickyFocus(true);
|
||||
const int gridSize = 10;
|
||||
|
||||
for (int y = 0; y < gridSize; ++y) {
|
||||
for (int x = 0; x < gridSize; ++x) {
|
||||
CustomProxy *proxy = new CustomProxy(nullptr, Qt::Window);
|
||||
proxy->setWidget(new EmbeddedDialog);
|
||||
|
||||
QRectF rect = proxy->boundingRect();
|
||||
|
||||
proxy->setPos(x * rect.width() * 1.05, y * rect.height() * 1.05);
|
||||
proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
|
||||
|
||||
scene.addItem(proxy);
|
||||
}
|
||||
}
|
||||
scene.setSceneRect(scene.itemsBoundingRect());
|
||||
|
||||
QGraphicsView view(&scene);
|
||||
view.scale(0.5, 0.5);
|
||||
view.setRenderHints(view.renderHints() | QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
|
||||
view.setBackgroundBrush(QPixmap(":/No-Ones-Laughing-3.jpg"));
|
||||
view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
|
||||
view.show();
|
||||
view.setWindowTitle("Embedded Dialogs Example");
|
||||
return app.exec();
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(flowlayout LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/graphicsview_flowlayout")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(graphicsview_flowlayout
|
||||
flowlayout.cpp flowlayout.h
|
||||
main.cpp
|
||||
window.cpp window.h
|
||||
)
|
||||
|
||||
set_target_properties(graphicsview_flowlayout PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(graphicsview_flowlayout PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS graphicsview_flowlayout
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
@ -0,0 +1,170 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "flowlayout.h"
|
||||
|
||||
#include <QtMath>
|
||||
|
||||
FlowLayout::FlowLayout(QGraphicsLayoutItem *parent) : QGraphicsLayout(parent)
|
||||
{
|
||||
QSizePolicy sp = sizePolicy();
|
||||
sp.setHeightForWidth(true);
|
||||
setSizePolicy(sp);
|
||||
}
|
||||
|
||||
void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item)
|
||||
{
|
||||
item->setParentLayoutItem(this);
|
||||
if (index > m_items.count() || index < 0)
|
||||
index = m_items.count();
|
||||
m_items.insert(index, item);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
int FlowLayout::count() const
|
||||
{
|
||||
return m_items.count();
|
||||
}
|
||||
|
||||
QGraphicsLayoutItem *FlowLayout::itemAt(int index) const
|
||||
{
|
||||
return m_items.value(index);
|
||||
}
|
||||
|
||||
void FlowLayout::removeAt(int index)
|
||||
{
|
||||
m_items.removeAt(index);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
qreal FlowLayout::spacing(Qt::Orientation o) const
|
||||
{
|
||||
return m_spacing[int(o) - 1];
|
||||
}
|
||||
|
||||
void FlowLayout::setSpacing(Qt::Orientations o, qreal spacing)
|
||||
{
|
||||
if (o & Qt::Horizontal)
|
||||
m_spacing[0] = spacing;
|
||||
if (o & Qt::Vertical)
|
||||
m_spacing[1] = spacing;
|
||||
}
|
||||
|
||||
void FlowLayout::setGeometry(const QRectF &geom)
|
||||
{
|
||||
QGraphicsLayout::setGeometry(geom);
|
||||
doLayout(geom, true);
|
||||
}
|
||||
|
||||
qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const
|
||||
{
|
||||
qreal left, top, right, bottom;
|
||||
getContentsMargins(&left, &top, &right, &bottom);
|
||||
const qreal maxw = geom.width() - left - right;
|
||||
|
||||
qreal x = 0;
|
||||
qreal y = 0;
|
||||
qreal maxRowHeight = 0;
|
||||
QSizeF pref;
|
||||
for (QGraphicsLayoutItem *item : m_items) {
|
||||
pref = item->effectiveSizeHint(Qt::PreferredSize);
|
||||
maxRowHeight = qMax(maxRowHeight, pref.height());
|
||||
|
||||
qreal next_x;
|
||||
next_x = x + pref.width();
|
||||
if (next_x > maxw) {
|
||||
if (qFuzzyIsNull(x)) {
|
||||
pref.setWidth(maxw);
|
||||
} else {
|
||||
x = 0;
|
||||
next_x = pref.width();
|
||||
}
|
||||
y += maxRowHeight + spacing(Qt::Vertical);
|
||||
maxRowHeight = 0;
|
||||
}
|
||||
|
||||
if (applyNewGeometry)
|
||||
item->setGeometry(QRectF(QPointF(left + x, top + y), pref));
|
||||
x = next_x + spacing(Qt::Horizontal);
|
||||
}
|
||||
maxRowHeight = qMax(maxRowHeight, pref.height());
|
||||
return top + y + maxRowHeight + bottom;
|
||||
}
|
||||
|
||||
QSizeF FlowLayout::minSize(const QSizeF &constraint) const
|
||||
{
|
||||
QSizeF size(0, 0);
|
||||
qreal left, top, right, bottom;
|
||||
getContentsMargins(&left, &top, &right, &bottom);
|
||||
if (constraint.width() >= 0) { // height for width
|
||||
const qreal height = doLayout(QRectF(QPointF(0,0), constraint), false);
|
||||
size = QSizeF(constraint.width(), height);
|
||||
} else if (constraint.height() >= 0) { // width for height?
|
||||
// not supported
|
||||
} else {
|
||||
for (const QGraphicsLayoutItem *item : std::as_const(m_items))
|
||||
size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize));
|
||||
size += QSizeF(left + right, top + bottom);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
QSizeF FlowLayout::prefSize() const
|
||||
{
|
||||
qreal left, right;
|
||||
getContentsMargins(&left, nullptr, &right, nullptr);
|
||||
|
||||
qreal maxh = 0;
|
||||
qreal totalWidth = 0;
|
||||
for (const QGraphicsLayoutItem *item : std::as_const(m_items)) {
|
||||
if (totalWidth > 0)
|
||||
totalWidth += spacing(Qt::Horizontal);
|
||||
QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize);
|
||||
totalWidth += pref.width();
|
||||
maxh = qMax(maxh, pref.height());
|
||||
}
|
||||
maxh += spacing(Qt::Vertical);
|
||||
|
||||
const qreal goldenAspectRatio = 1.61803399;
|
||||
qreal w = qSqrt(totalWidth * maxh * goldenAspectRatio) + left + right;
|
||||
|
||||
return minSize(QSizeF(w, -1));
|
||||
}
|
||||
|
||||
QSizeF FlowLayout::maxSize() const
|
||||
{
|
||||
qreal totalWidth = 0;
|
||||
qreal totalHeight = 0;
|
||||
for (const QGraphicsLayoutItem *item : std::as_const(m_items)) {
|
||||
if (totalWidth > 0)
|
||||
totalWidth += spacing(Qt::Horizontal);
|
||||
if (totalHeight > 0)
|
||||
totalHeight += spacing(Qt::Vertical);
|
||||
QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize);
|
||||
totalWidth += pref.width();
|
||||
totalHeight += pref.height();
|
||||
}
|
||||
|
||||
qreal left, top, right, bottom;
|
||||
getContentsMargins(&left, &top, &right, &bottom);
|
||||
return QSizeF(left + totalWidth + right, top + totalHeight + bottom);
|
||||
}
|
||||
|
||||
QSizeF FlowLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
|
||||
{
|
||||
QSizeF sh = constraint;
|
||||
switch (which) {
|
||||
case Qt::PreferredSize:
|
||||
sh = prefSize();
|
||||
break;
|
||||
case Qt::MinimumSize:
|
||||
sh = minSize(constraint);
|
||||
break;
|
||||
case Qt::MaximumSize:
|
||||
sh = maxSize();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return sh;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef FLOWLAYOUT_H
|
||||
#define FLOWLAYOUT_H
|
||||
|
||||
#include <QGraphicsLayout>
|
||||
|
||||
class FlowLayout : public QGraphicsLayout
|
||||
{
|
||||
public:
|
||||
FlowLayout(QGraphicsLayoutItem *parent = nullptr);
|
||||
inline void addItem(QGraphicsLayoutItem *item);
|
||||
void insertItem(int index, QGraphicsLayoutItem *item);
|
||||
void setSpacing(Qt::Orientations o, qreal spacing);
|
||||
qreal spacing(Qt::Orientation o) const;
|
||||
|
||||
// inherited functions
|
||||
void setGeometry(const QRectF &geom) override;
|
||||
|
||||
int count() const override;
|
||||
QGraphicsLayoutItem *itemAt(int index) const override;
|
||||
void removeAt(int index) override;
|
||||
|
||||
protected:
|
||||
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override;
|
||||
|
||||
private:
|
||||
qreal doLayout(const QRectF &geom, bool applyNewGeometry) const;
|
||||
QSizeF minSize(const QSizeF &constraint) const;
|
||||
QSizeF prefSize() const;
|
||||
QSizeF maxSize() const;
|
||||
|
||||
QList<QGraphicsLayoutItem *> m_items;
|
||||
qreal m_spacing[2] = {6, 6};
|
||||
};
|
||||
|
||||
|
||||
inline void FlowLayout::addItem(QGraphicsLayoutItem *item)
|
||||
{
|
||||
insertItem(-1, item);
|
||||
}
|
||||
|
||||
#endif // FLOWLAYOUT_H
|
@ -0,0 +1,10 @@
|
||||
QT += widgets
|
||||
|
||||
QMAKE_PROJECT_NAME = flowlayout_graphicsview
|
||||
|
||||
HEADERS += flowlayout.h window.h
|
||||
SOURCES += flowlayout.cpp main.cpp window.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/graphicsview/flowlayout
|
||||
INSTALLS += target
|
@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
//! [1]
|
||||
#include "window.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QGraphicsView>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QGraphicsScene scene;
|
||||
QGraphicsView view(&scene);
|
||||
Window *w = new Window;
|
||||
scene.addItem(w);
|
||||
|
||||
view.resize(400, 300);
|
||||
view.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
//! [1]
|
@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "window.h"
|
||||
#include "flowlayout.h"
|
||||
|
||||
#include <QGraphicsProxyWidget>
|
||||
#include <QLabel>
|
||||
|
||||
Window::Window(QGraphicsItem *parent) : QGraphicsWidget(parent, Qt::Window)
|
||||
{
|
||||
FlowLayout *lay = new FlowLayout;
|
||||
const QString sentence(QLatin1String("I am not bothered by the fact that I am unknown."
|
||||
" I am bothered when I do not know others. (Confucius)"));
|
||||
const QList<QStringView> words = QStringView{ sentence }.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
||||
for (const QStringView &word : words) {
|
||||
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this);
|
||||
QLabel *label = new QLabel(word.toString());
|
||||
label->setFrameStyle(QFrame::Box | QFrame::Plain);
|
||||
proxy->setWidget(label);
|
||||
lay->addItem(proxy);
|
||||
}
|
||||
setLayout(lay);
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include <QGraphicsWidget>
|
||||
|
||||
class Window : public QGraphicsWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Window(QGraphicsItem *parent = nullptr);
|
||||
};
|
||||
|
||||
#endif // WINDOW_H
|
Reference in New Issue
Block a user