Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
4//! [0]
5#ifndef CARD_H
6#define CARD_H
7
8#include <QtWidgets>
9#include <QList>
10
11class CardLayout : public QLayout
12{
13public:
14 CardLayout(int spacing): QLayout()
15 { setSpacing(spacing); }
16 CardLayout(int spacing, QWidget *parent): QLayout(parent)
17 { setSpacing(spacing); }
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
32//! [0]
33
34
35//! [1]
36//#include "card.h"
37//! [1]
38
39//! [2]
40int CardLayout::count() const
41{
42 // QList::size() returns the number of QLayoutItems in m_items
43 return m_items.size();
44}
45//! [2]
46
47//! [3]
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}
60//! [3]
61
62
63//! [4]
64void CardLayout::addItem(QLayoutItem *item)
65{
66 m_items.append(item);
67}
68//! [4]
69
70
71//! [5]
73{
74 QLayoutItem *item;
75 while ((item = takeAt(0)))
76 delete item;
77}
78//! [5]
79
80
81//! [6]
82void CardLayout::setGeometry(const QRect &r)
83{
84 QLayout::setGeometry(r);
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}
99//! [6]
100
101
102//! [7]
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}
130//! [7]
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)