QtBase  v6.3.1
testwidget.cpp
Go to the documentation of this file.
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 
60  : QWidget(parent)
61 {
62  const QString romeo = tr(
63  "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  "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("Feeling lucky, punk?");
84 
85  textSamples << romeo << macbeth << harry;
87 
89  sampleIndex = 0;
90  elidedText = new ElidedLabel(textSamples[sampleIndex], this);
91  elidedText->setFrameStyle(QFrame::Box);
93 
95  QPushButton *switchButton = new QPushButton(tr("Switch text"));
96  connect(switchButton, &QPushButton::clicked, this, &TestWidget::switchText);
97 
98  QPushButton *exitButton = new QPushButton(tr("Exit"));
99  connect(exitButton, &QPushButton::clicked, this, &TestWidget::close);
100 
101  QLabel *label = new QLabel(tr("Elided"));
102  label->setVisible(elidedText->isElided());
105 
107  widthSlider = new QSlider(Qt::Horizontal);
108  widthSlider->setMinimum(0);
109  connect(widthSlider, &QSlider::valueChanged, this, &TestWidget::onWidthChanged);
110 
111  heightSlider = new QSlider(Qt::Vertical);
112  heightSlider->setInvertedAppearance(true);
113  heightSlider->setMinimum(0);
114  connect(heightSlider, &QSlider::valueChanged, this, &TestWidget::onHeightChanged);
116 
119  layout->addWidget(label, 0, 1, Qt::AlignCenter);
120  layout->addWidget(switchButton, 0, 2);
121  layout->addWidget(exitButton, 0, 3);
122  layout->addWidget(widthSlider, 1, 1, 1, 3);
123  layout->addWidget(heightSlider, 2, 0);
124  layout->addWidget(elidedText, 2, 1, 1, 3, Qt::AlignTop | Qt::AlignLeft);
125 
126  setLayout(layout);
128 }
129 
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(widthSlider->value(), heightSlider->value());
144 }
146 
148 void TestWidget::switchText()
149 {
150  sampleIndex = (sampleIndex + 1) % textSamples.size();
151  elidedText->setText(textSamples.at(sampleIndex));
152 }
154 
156 void TestWidget::onWidthChanged(int width)
157 {
158  elidedText->setFixedWidth(width);
159 }
160 
161 void TestWidget::onHeightChanged(int height)
162 {
163  elidedText->setFixedHeight(height);
164 }
166 
void setText(const QString &text)
[0]
Definition: elidedlabel.cpp:68
bool isElided
Definition: elidedlabel.h:62
void elisionChanged(bool elided)
void clicked(bool checked=false)
int value
the slider's current value
void valueChanged(int value)
void setInvertedAppearance(bool)
void setFrameStyle(int)
Definition: qframe.cpp:335
@ Box
Definition: qframe.h:76
void setLayout(QGraphicsLayout *layout)
QGraphicsLayout * layout
The layout of the widget.
The QGridLayout class lays out widgets in a grid.
Definition: qgridlayout.h:57
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPushButton widget provides a command button.
Definition: qpushbutton.h:56
The QResizeEvent class contains event parameters for resize events. \inmodule QtGui.
Definition: qevent.h:612
The QSlider widget provides a vertical or horizontal slider.
Definition: qslider.h:54
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
int width
the width of the widget excluding any window frame
Definition: qwidget.h:148
int height
the height of the widget excluding any window frame
Definition: qwidget.h:149
virtual void setVisible(bool visible)
Definition: qwidget.cpp:8198
void setFixedHeight(int h)
Definition: qwidget.cpp:4181
void setFixedWidth(int w)
Definition: qwidget.cpp:4161
void setFixedSize(const QSize &)
void resizeEvent(QResizeEvent *event) override
[6]
Definition: testwidget.cpp:131
QPushButton
[1]
@ AlignTop
Definition: qnamespace.h:178
@ AlignCenter
Definition: qnamespace.h:188
@ AlignLeft
Definition: qnamespace.h:169
@ Horizontal
Definition: qnamespace.h:124
@ Vertical
Definition: qnamespace.h:125
GLint GLsizei GLsizei height
GLint GLsizei width
GLuint GLsizei const GLchar * label
[43]
struct _cl_event * event
Definition: qopenglext.h:2998
#define tr(X)
Q_UNUSED(salary)
[21]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent