1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "testwidget.h"
52#include "elidedlabel.h"
53
54#include <QGridLayout>
55#include <QLabel>
56#include <QPushButton>
57
58//! [0]
59TestWidget::TestWidget(QWidget *parent)
60 : QWidget(parent)
61{
62 const QString romeo = tr(
63 s: "But soft, what light through yonder window breaks? / "
64 "It is the east, and Juliet is the sun. / "
65 "Arise, fair sun, and kill the envious moon, / "
66 "Who is already sick and pale with grief / "
67 "That thou, her maid, art far more fair than she."
68 );
69
70 const QString macbeth = tr(
71 s: "To-morrow, and to-morrow, and to-morrow, / "
72 "Creeps in this petty pace from day to day, / "
73 "To the last syllable of recorded time; / "
74 "And all our yesterdays have lighted fools / "
75 "The way to dusty death. Out, out, brief candle! / "
76 "Life's but a walking shadow, a poor player, / "
77 "That struts and frets his hour upon the stage, / "
78 "And then is heard no more. It is a tale / "
79 "Told by an idiot, full of sound and fury, / "
80 "Signifying nothing."
81 );
82
83 const QString harry = tr(s: "Feeling lucky, punk?");
84
85 textSamples << romeo << macbeth << harry;
86 //! [0]
87
88 //! [1]
89 sampleIndex = 0;
90 elidedText = new ElidedLabel(textSamples[sampleIndex], this);
91 elidedText->setFrameStyle(QFrame::Box);
92 //! [1]
93
94 //! [2]
95 QPushButton *switchButton = new QPushButton(tr(s: "Switch text"));
96 connect(sender: switchButton, signal: &QPushButton::clicked, receiver: this, slot: &TestWidget::switchText);
97
98 QPushButton *exitButton = new QPushButton(tr(s: "Exit"));
99 connect(sender: exitButton, signal: &QPushButton::clicked, receiver: this, slot: &TestWidget::close);
100
101 QLabel *label = new QLabel(tr(s: "Elided"));
102 label->setVisible(elidedText->isElided());
103 connect(sender: elidedText, signal: &ElidedLabel::elisionChanged, receiver: label, slot: &QLabel::setVisible);
104 //! [2]
105
106 //! [3]
107 widthSlider = new QSlider(Qt::Horizontal);
108 widthSlider->setMinimum(0);
109 connect(sender: widthSlider, signal: &QSlider::valueChanged, receiver: this, slot: &TestWidget::onWidthChanged);
110
111 heightSlider = new QSlider(Qt::Vertical);
112 heightSlider->setInvertedAppearance(true);
113 heightSlider->setMinimum(0);
114 connect(sender: heightSlider, signal: &QSlider::valueChanged, receiver: this, slot: &TestWidget::onHeightChanged);
115 //! [3]
116
117 //! [4]
118 QGridLayout *layout = new QGridLayout;
119 layout->addWidget(label, row: 0, column: 1, Qt::AlignCenter);
120 layout->addWidget(switchButton, row: 0, column: 2);
121 layout->addWidget(exitButton, row: 0, column: 3);
122 layout->addWidget(widthSlider, row: 1, column: 1, rowSpan: 1, columnSpan: 3);
123 layout->addWidget(heightSlider, row: 2, column: 0);
124 layout->addWidget(elidedText, row: 2, column: 1, rowSpan: 1, columnSpan: 3, Qt::AlignTop | Qt::AlignLeft);
125
126 setLayout(layout);
127 //! [4]
128}
129
130//! [6]
131void TestWidget::resizeEvent(QResizeEvent *event)
132{
133 Q_UNUSED(event)
134
135 int maxWidth = widthSlider->width();
136 widthSlider->setMaximum(maxWidth);
137 widthSlider->setValue(maxWidth / 2);
138
139 int maxHeight = heightSlider->height();
140 heightSlider->setMaximum(maxHeight);
141 heightSlider->setValue(maxHeight / 2);
142
143 elidedText->setFixedSize(w: widthSlider->value(), h: heightSlider->value());
144}
145//! [6]
146
147//! [7]
148void TestWidget::switchText()
149{
150 sampleIndex = (sampleIndex + 1) % textSamples.size();
151 elidedText->setText(textSamples.at(i: sampleIndex));
152}
153//! [7]
154
155//! [8]
156void TestWidget::onWidthChanged(int width)
157{
158 elidedText->setFixedWidth(width);
159}
160
161void TestWidget::onHeightChanged(int height)
162{
163 elidedText->setFixedHeight(height);
164}
165//! [8]
166
167

source code of qtbase/examples/widgets/widgets/elidedlabel/testwidget.cpp