Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
doc_src_layout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5#ifndef CARD_H
6#define CARD_H
7
8#include <QtWidgets>
9#include <QList>
10
11class CardLayout : public QLayout
12{
13public:
19
20 void addItem(QLayoutItem *item) override;
21 QSize sizeHint() const override;
22 QSize minimumSize() const override;
23 int count() const override;
24 QLayoutItem *itemAt(int) const override;
25 QLayoutItem *takeAt(int) override;
26 void setGeometry(const QRect &rect) override;
27
28private:
29 QList<QLayoutItem *> m_items;
30};
31#endif
33
34
36//#include "card.h"
38
41{
42 // QList::size() returns the number of QLayoutItems in m_items
43 return m_items.size();
44}
46
49{
50 // QList::value() performs index checking, and returns nullptr if we are
51 // outside the valid range
52 return m_items.value(idx);
53}
54
56{
57 // QList::take does not do index checking
58 return idx >= 0 && idx < m_items.size() ? m_items.takeAt(idx) : 0;
59}
61
62
65{
66 m_items.append(item);
67}
69
70
73{
75 while ((item = takeAt(0)))
76 delete item;
77}
79
80
83{
85
86 if (m_items.size() == 0)
87 return;
88
89 int w = r.width() - (m_items.count() - 1) * spacing();
90 int h = r.height() - (m_items.count() - 1) * spacing();
91 int i = 0;
92 while (i < m_items.size()) {
93 QLayoutItem *o = m_items.at(i);
94 QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h);
95 o->setGeometry(geom);
96 ++i;
97 }
98}
100
101
104{
105 QSize s(0, 0);
106 int n = m_items.count();
107 if (n > 0)
108 s = QSize(100, 70); //start with a nice default size
109 int i = 0;
110 while (i < n) {
111 QLayoutItem *o = m_items.at(i);
112 s = s.expandedTo(o->sizeHint());
113 ++i;
114 }
115 return s + n * QSize(spacing(), spacing());
116}
117
119{
120 QSize s(0, 0);
121 int n = m_items.count();
122 int i = 0;
123 while (i < n) {
124 QLayoutItem *o = m_items.at(i);
125 s = s.expandedTo(o->minimumSize());
126 ++i;
127 }
128 return s + n * QSize(spacing(), spacing());
129}
void setGeometry(const QRect &rect) override
[5]
QLayoutItem * itemAt(int) const override
[2]
void addItem(QLayoutItem *item) override
[3]
QSize sizeHint() const override
[6]
CardLayout(int spacing)
int count() const override
[0]
QLayoutItem * takeAt(int) override
Must be implemented in subclasses to remove the layout item at index from the layout,...
QSize minimumSize() const override
Implemented in subclasses to return the minimum size of this item.
CardLayout(int spacing, QWidget *parent)
The QLayoutItem class provides an abstract item that a QLayout manipulates.
Definition qlayoutitem.h:25
The QLayout class is the base class of geometry managers.
Definition qlayout.h:26
int spacing
the spacing between widgets inside the layout
Definition qlayout.h:30
virtual void setSpacing(int)
Definition qlayout.cpp:266
virtual void setGeometry(const QRect &) override
\reimp
Definition qlayout.cpp:451
qsizetype size() const noexcept
Definition qlist.h:397
T takeAt(qsizetype i)
Definition qlist.h:609
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
T value(qsizetype i) const
Definition qlist.h:664
qsizetype count() const noexcept
Definition qlist.h:398
void append(parameter_type t)
Definition qlist.h:458
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:25
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
rect
[4]
GLfloat GLfloat GLfloat w
[0]
GLboolean r
[2]
GLfloat n
GLfloat GLfloat GLfloat GLfloat h
GLdouble s
[6]
Definition qopenglext.h:235
QGraphicsItem * item