Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
src_corelib_global_qglobal_widgets.cpp
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QLabel
>
5
#
include
<
QStyle
>
6
7
void
label_example
()
8
{
9
QLabel *label =
new
QLabel;
10
//! [0]
11
label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
12
label->setAlignment({ });
13
//! [0]
14
}
15
16
QStyle
*
style
()
17
{
18
static
QStyle *s =
nullptr
;
19
return
s;
20
}
21
22
void
snippet_16
()
23
{
24
//! [1]
25
#
if
QT_VERSION
>=
QT_VERSION_CHECK
(
4
,
1
,
0
)
26
QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon);
27
#
else
28
QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon);
29
QIcon icon(pixmap);
30
#
endif
31
//! [1]
32
}
33
34
//! [2]
35
#
include
<
QApplication
>
36
#
include
<
stdio
.
h
>
37
#
include
<
stdlib
.
h
>
38
39
QtMessageHandler
originalHandler
=
nullptr
;
40
41
void
logToFile
(QtMsgType type,
const
QMessageLogContext &context,
const
QString &msg)
42
{
43
QString message = qFormatLogMessage(type, context, msg);
44
static
FILE *f = fopen(
"log.txt"
,
"a"
);
45
fprintf(f,
"%s\n"
, qPrintable(message));
46
fflush(f);
47
48
if
(
originalHandler
)
49
originalHandler
(type, context, msg);
50
}
51
52
int
main
(
int
argc,
char
**argv)
53
{
54
originalHandler
= qInstallMessageHandler(logToFile);
55
QApplication app(argc, argv);
56
// ...
57
return
app.exec();
58
}
59
//! [2]
60
61
class
FriendlyConversation
62
{
63
public
:
64
QString
greeting
(
int
type);
65
};
66
67
QString
tr
(
const
char
*)
68
{
69
return
""
;
70
}
71
72
73
//! [3]
74
static
const
char
*
greeting_strings
[] = {
75
QT_TRANSLATE_NOOP(
"FriendlyConversation"
,
"Hello"
),
76
QT_TRANSLATE_NOOP(
"FriendlyConversation"
,
"Goodbye"
)
77
};
78
79
QString
FriendlyConversation
::
greeting
(
int
type)
80
{
81
return
tr(greeting_strings[type]);
82
}
83
84
QString
global_greeting
(
int
type)
85
{
86
return
qApp->translate(
"FriendlyConversation"
,
87
greeting_strings[type]);
88
}
89
//! [3]
90
91
92
namespace
repetition
93
{
94
class
FriendlyConversation
95
{
96
public
:
97
QString
greeting
(
int
type);
98
};
99
100
QString
tr
(
const
char
*text,
const
char
*comment)
101
{
102
return
""
;
103
}
104
105
106
//! [4]
107
108
static
struct
{
const
char
*source;
const
char
*comment; } greeting_strings[] =
109
{
110
QT_TRANSLATE_NOOP3(
"FriendlyConversation"
,
"Hello"
,
111
"A really friendly hello"
),
112
QT_TRANSLATE_NOOP3(
"FriendlyConversation"
,
"Goodbye"
,
113
"A really friendly goodbye"
)
114
};
115
116
QString
FriendlyConversation
::
greeting
(
int
type)
117
{
118
return
tr(greeting_strings[type].source,
119
greeting_strings[type].comment);
120
}
121
122
QString
global_greeting
(
int
type)
123
{
124
return
qApp->translate(
"FriendlyConversation"
,
125
greeting_strings[type].source,
126
greeting_strings[type].comment);
127
}
128
//! [4]
129
}
130
131
namespace
snippet_qttranslatennoop3
132
{
133
class
FriendlyConversation
134
{
135
public
:
136
QString
greeting
(
int
type,
int
count);
137
};
138
139
QString
tr
(
const
char
*text,
const
char
*comment,
int
n)
140
{
141
return
""
;
142
}
143
144
//! [qttranslatennoop]
145
static
struct
{
const
char
*
const
source;
const
char
*
const
comment; } status_strings[] = {
146
QT_TRANSLATE_N_NOOP3(
"Message Status"
,
"Hello, you have %n message(s)"
,
147
"A login message status"
),
148
QT_TRANSLATE_N_NOOP3(
"Message status"
,
"You have %n new message(s)"
,
149
"A new message query status"
)
150
};
151
152
QString
FriendlyConversation
::
greeting
(
int
type,
int
count)
153
{
154
return
tr(status_strings[type].source,
155
status_strings[type].comment, count);
156
}
157
158
QString
global_greeting
(
int
type,
int
count)
159
{
160
return
qApp->translate(
"Message Status"
,
161
status_strings[type].source,
162
status_strings[type].comment,
163
count);
164
}
165
//! [qttranslatennoop]
166
}
167
168
class
TheClass
:
public
QWidget
169
{
170
public
:
171
TheClass
(QWidget *parent =
nullptr
) :
QWidget
(
parent
) {
172
addLabels
(
)
;
173
}
174
void
addLabels
();
175
};
176
177
//! [qttrid_noop]
178
static
const
char
*
const
ids
[] = {
179
//% "This is the first text."
180
QT_TRID_NOOP(
"qtn_1st_text"
),
181
//% "This is the second text."
182
QT_TRID_NOOP(
"qtn_2nd_text"
),
183
0
184
};
185
186
void
TheClass
::
addLabels
()
187
{
188
for
(
int
i = 0; ids[i]; ++i)
189
new
QLabel(qtTrId(ids[i]),
this
);
190
}
191
//! [qttrid_noop]
192
193
void
qwidget_example
()
194
{
195
//! [5]
196
QWidget w = QWidget();
197
//! [5]
198
}
199
200
//! [qt-version-check]
201
#
include
<
QtGlobal
>
202
203
#
if
QT_VERSION
>=
QT_VERSION_CHECK
(
5
,
0
,
0
)
204
#
include
<
QtWidgets
>
205
#
else
206
#
include
<
QtGui
>
207
#
endif
208
//! [qt-version-check]
FriendlyConversation
[2]
Definition
src_corelib_global_qglobal_widgets.cpp:62
FriendlyConversation::greeting
QString greeting(int type)
Definition
src_corelib_global_qglobal_widgets.cpp:79
TheClass
Definition
src_corelib_global_qglobal_widgets.cpp:169
TheClass::addLabels
void addLabels()
Definition
src_corelib_global_qglobal_widgets.cpp:186
TheClass::TheClass
TheClass(QWidget *parent=nullptr)
Definition
src_corelib_global_qglobal_widgets.cpp:171
repetition::FriendlyConversation
Definition
src_corelib_global_qglobal_widgets.cpp:95
repetition::FriendlyConversation::greeting
QString greeting(int type)
Definition
src_corelib_global_qglobal_widgets.cpp:116
snippet_qttranslatennoop3::FriendlyConversation
Definition
src_corelib_global_qglobal_widgets.cpp:134
snippet_qttranslatennoop3::FriendlyConversation::greeting
QString greeting(int type, int count)
Definition
src_corelib_global_qglobal_widgets.cpp:152
repetition
[3]
Definition
src_corelib_global_qglobal_widgets.cpp:93
repetition::global_greeting
QString global_greeting(int type)
Definition
src_corelib_global_qglobal_widgets.cpp:122
repetition::tr
QString tr(const char *text, const char *comment)
Definition
src_corelib_global_qglobal_widgets.cpp:100
snippet_qttranslatennoop3
Definition
src_corelib_global_qglobal_widgets.cpp:132
snippet_qttranslatennoop3::tr
QString tr(const char *text, const char *comment, int n)
Definition
src_corelib_global_qglobal_widgets.cpp:139
snippet_qttranslatennoop3::global_greeting
QString global_greeting(int type, int count)
Definition
src_corelib_global_qglobal_widgets.cpp:158
main
int main(int argc, char *argv[])
[ctor_close]
Definition
src_corelib_animation_qpropertyanimation.cpp:100
style
QStyle * style()
Definition
src_corelib_global_qglobal_widgets.cpp:16
global_greeting
QString global_greeting(int type)
Definition
src_corelib_global_qglobal_widgets.cpp:84
greeting_strings
static const char * greeting_strings[]
[3]
Definition
src_corelib_global_qglobal_widgets.cpp:74
snippet_16
void snippet_16()
[14]
Definition
src_corelib_global_qglobal_widgets.cpp:22
logToFile
void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Definition
src_corelib_global_qglobal_widgets.cpp:41
label_example
void label_example()
Definition
src_corelib_global_qglobal_widgets.cpp:7
ids
static const char *const ids[]
[qttrid_noop]
Definition
src_corelib_global_qglobal_widgets.cpp:178
qwidget_example
void qwidget_example()
[qttrid_noop]
Definition
src_corelib_global_qglobal_widgets.cpp:193
originalHandler
QtMessageHandler originalHandler
[2]
Definition
src_corelib_global_qglobal_widgets.cpp:39
tr
QString tr(const char *)
Definition
src_corelib_global_qglobal_widgets.cpp:67
qtbase
src
corelib
doc
snippets
code
src_corelib_global_qglobal_widgets.cpp
Generated on
for Qt by
1.18.0