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
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
8{
9 QLabel *label = new QLabel;
10 //! [0]
11 label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
12 label->setAlignment({ });
13 //! [0]
14}
15
17{
18 static QStyle *s = nullptr;
19 return s;
20}
21
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
40
41void 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
49 originalHandler(type, context, msg);
50}
51
52int main(int argc, char **argv)
53{
54 originalHandler = qInstallMessageHandler(logToFile);
55 QApplication app(argc, argv);
56 // ...
57 return app.exec();
58}
59//! [2]
60
62{
63public:
64 QString greeting(int type);
65};
66
67QString tr(const char *)
68{
69 return "";
70}
71
72
73//! [3]
74static const char *greeting_strings[] = {
75 QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
76 QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
77};
78
80{
81 return tr(greeting_strings[type]);
82}
83
85{
86 return qApp->translate("FriendlyConversation",
87 greeting_strings[type]);
88}
89//! [3]
90
91
92namespace repetition
93{
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
117 {
118 return tr(greeting_strings[type].source,
119 greeting_strings[type].comment);
120 }
121
123 {
124 return qApp->translate("FriendlyConversation",
125 greeting_strings[type].source,
126 greeting_strings[type].comment);
127 }
128 //! [4]
129}
130
132{
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
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
168class TheClass : public QWidget
169{
170 public:
171 TheClass(QWidget *parent = nullptr) : QWidget(parent) {
173 }
174 void addLabels();
175};
176
177//! [qttrid_noop]
178static 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
187{
188 for (int i = 0; ids[i]; ++i)
189 new QLabel(qtTrId(ids[i]), this);
190}
191//! [qttrid_noop]
192
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]
TheClass(QWidget *parent=nullptr)
QString tr(const char *text, const char *comment)
QString tr(const char *text, const char *comment, int n)
int main(int argc, char *argv[])
[ctor_close]
QString global_greeting(int type)
static const char * greeting_strings[]
[3]
void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg)
static const char *const ids[]
[qttrid_noop]
void qwidget_example()
[qttrid_noop]
QtMessageHandler originalHandler
[2]
QString tr(const char *)