source: trunk/doc/src/snippets/code/doc_src_layout.qdoc@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 2.2 KB
Line 
1//! [0]
2#ifndef CARD_H
3#define CARD_H
4
5#include <QLayout>
6#include <QList>
7
8class CardLayout : public QLayout
9{
10public:
11 CardLayout(QWidget *parent, int dist)
12 : QLayout(parent, 0, dist) {}
13 CardLayout(QLayout *parent, int dist)
14 : QLayout(parent, dist) {}
15 CardLayout(int dist)
16 : QLayout(dist) {}
17 ~CardLayout();
18
19 void addItem(QLayoutItem *item);
20 QSize sizeHint() const;
21 QSize minimumSize() const;
22 QLayoutItem *itemAt(int) const;
23 QLayoutItem *takeAt(int);
24 void setGeometry(const QRect &rect);
25
26private:
27 QList<QLayoutItem*> list;
28};
29#endif
30//! [0]
31
32
33//! [1]
34#include "card.h"
35//! [1]
36
37
38//! [2]
39QLayoutItem *CardLayout::itemAt(int idx) const
40{
41 // QList::value() performs index checking, and returns 0 if we are
42 // outside the valid range
43 return list.value(idx);
44}
45
46QLayoutItem *CardLayout::takeAt(int idx)
47{
48 // QList::take does not do index checking
49 return idx >= 0 && idx < list.size() ? list.takeAt(idx) : 0;
50}
51//! [2]
52
53
54//! [3]
55void CardLayout::addItem(QLayoutItem *item)
56{
57 list.append(item);
58}
59//! [3]
60
61
62//! [4]
63CardLayout::~CardLayout()
64{
65 deleteAllItems();
66}
67//! [4]
68
69
70//! [5]
71void CardLayout::setGeometry(const QRect &r)
72{
73 QLayout::setGeometry(r);
74
75 if (list.size() == 0)
76 return;
77
78 int w = r.width() - (list.count() - 1) * spacing();
79 int h = r.height() - (list.count() - 1) * spacing();
80 int i = 0;
81 while (i < list.size()) {
82 QLayoutItem *o = list.at(i);
83 QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h);
84 o->setGeometry(geom);
85 ++i;
86 }
87}
88//! [5]
89
90
91//! [6]
92QSize CardLayout::sizeHint() const
93{
94 QSize s(0,0);
95 int n = list.count();
96 if (n > 0)
97 s = QSize(100,70); //start with a nice default size
98 int i = 0;
99 while (i < n) {
100 QLayoutItem *o = list.at(i);
101 s = s.expandedTo(o->sizeHint());
102 ++i;
103 }
104 return s + n*QSize(spacing(), spacing());
105}
106
107QSize CardLayout::minimumSize() const
108{
109 QSize s(0,0);
110 int n = list.count();
111 int i = 0;
112 while (i < n) {
113 QLayoutItem *o = list.at(i);
114 s = s.expandedTo(o->minimumSize());
115 ++i;
116 }
117 return s + n*QSize(spacing(), spacing());
118}
119//! [6]
Note: See TracBrowser for help on using the repository browser.