aboutsummaryrefslogtreecommitdiffstats
path: root/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.cpp
blob: 81e7edc8ac656723e9d4ef5854f2788e6fd5f6a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! [0]
lineEdit = qt_find_obj_child(myWidget, "QLineEdit", "my line edit")
if lineEdit:
    lineEdit.setText("Default")
//! [0]


//! [1]
obj = QPushButton()
obj.metaObject().className()                # returns "QPushButton"

QPushButton.staticMetaObject.className()    # returns "QPushButton"
//! [1]


//! [2]
QPushButton.staticMetaObject.className()    # returns "QPushButton"

obj = QPushButton()
obj.metaObject().className()                # returns "QPushButton"
//! [2]


//! [3]
obj = QTimer()                              # QTimer inherits QObject

timer = obj
# timer == (QObject *)obj

button = obj
# button == 0
//! [3]


//! [4]
timer = QTimer()                    # QTimer inherits QObject
timer.inherits("QTimer")            # returns true
timer.inherits("QObject")           # returns true
timer.inherits("QAbstractButton")   # returns false

# QLayout inherits QObject and QLayoutItem
layout = QLayout()
layout.inherits("QObject")          # returns true
layout.inherits("QLayoutItem")      # returns false
//! [4]


//! [5]
print "MyClass::setPrecision(): (%s) invalid precision %f" % \
       (qPrintable(objectName()), newPrecision)
//! [5]


//! [6]
class MainWindow(QMainWindow):
    def __init__(self):
        self.textEdit = QTextEdit()
        setCentralWidget(self.textEdit)
        textEdit.installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj == textEdit:
            if event.type() == QEvent.KeyPress:
                keyEvent = event
                print "Ate key press", keyEvent.key()
                return true
            else:
                return false
        else:
            # pass the event on to the parent class
            return QMainWindow.eventFilter(self, obj, event)
//! [6]


//! [7]
myObject.moveToThread(QApplication.instance().thread())
//! [7]


//! [8]
class MyObject(QObject):
    def __init__(self, parent):
        QObject.__init__(self, parent)

        self.startTimer(50)         # 50-millisecond timer
        self.startTimer(1000)       # 1-second timer
        self.startTimer(60000)      # 1-minute timer


    def timerEvent(self, event):
        print "Timer ID:", event.timerId()

//! [8]


//! [9]
list = window().queryList("QAbstractButton")
for obj in list:
    obj.setEnabled(false)
//! [9]


//! [10]
button = parentWidget.findChild(QPushButton, "button1")
//! [10]


//! [11]
list = parentWidget.findChild(QListWidget)
//! [11]


//! [12]
widgets = parentWidget.findChildren(QWidget, "widgetname")
//! [12]


//! [13]
allPButtons = parentWidget.findChildren(QPushButton)
//! [13]


//! [14]
monitoredObj.installEventFilter(filterObj)
//! [14]


//! [15]
class KeyPressEater(QObject):
    def eventFilter(self, obj, event):
        if event.type() == QEvent.KeyPress:
            print "Ate key press", event.key()
            return True
        else:
            # standard event processing
            return QObject.eventFilter(self, obj, event)
//! [15]


//! [16]
keyPressEater = KeyPressEater(self)
pushButton = QPushButton(self)
listView = QListView(self)

pushButton.installEventFilter(keyPressEater)
listView.installEventFilter(keyPressEater)
//! [16]


//! [17]
def __init__(self):
    senderLabel = QLabel(self.tr("Name:"))
    recipientLabel = QLabel(self.tr("Name:", "recipient"))
    # ...
//! [17]


//! [18]
n = messages.count();
showMessage(self.tr("%n message(s) saved", "", n));
//! [18]


//! [19]
if n == 1:
    self.tr("%n message saved")
else:
    self.tr("%n messages saved")
//! [19]


//! [20]
label.setText(self.tr("F\374r \310lise"))
//! [20]


//! [21]
if receivers(SIGNAL('valueChanged()')) > 0:
    data = get_the_value()  # expensive operation
    self.valueChanged(data)
//! [21]


//! [22]
label = QLabel()
scrollBar = QScrollBar()
QObject.connect(scrollBar, SIGNAL('valueChanged()'),
                label,  SLOT('setNum()'))
//! [22]


//! [23]
// WRONG
QObject.connect(scrollBar, SIGNAL('valueChanged()'),
                 label, SLOT('setNum()'));
//! [23]


//! [24]
class MyWidget(QWidget):
    def __init__(self):
        myButton = QPushButton(self)
        connect(myButton, SIGNAL('clicked()'),
                self, SIGNAL('buttonClicked()'))
//! [24]


//! [25]
QObject.connect: Cannot queue arguments of type 'MyType'
(Make sure 'MyType' is registered using qRegisterMetaType().)
//! [25]


//! [26]
disconnect(myObject, 0, 0, 0)
//! [26]


//! [27]
myObject.disconnect()
//! [27]


//! [28]
disconnect(myObject, SIGNAL('mySignal()'), 0, 0)
//! [28]


//! [29]
myObject.disconnect(SIGNAL('mySignal()'))
//! [29]


//! [30]
disconnect(myObject, 0, myReceiver, 0)
//! [30]


//! [31]
myObject.disconnect(myReceiver)
//! [31]


//! [32]
if QLatin1String(signal) == SIGNAL('valueChanged()'):
    # signal is valueChanged()
//! [32]


//! [33]
def on_<object name>_<signal name>(<signal parameters>)
//! [33]


//! [34]
def on_button1_clicked()
//! [34]


//! [35]
class MyClass(QObject):
    Q_CLASSINFO("Author", "Pierre Gendron")
    Q_CLASSINFO("URL", "http://www.my-organization.qc.ca")

//! [35]


//! [36]
Q_PROPERTY(type name
           READ getFunction
           [WRITE setFunction]
           [RESET resetFunction]
           [DESIGNABLE bool]
           [SCRIPTABLE bool]
           [STORED bool]
	   [USER bool])
//! [36]


//! [37]
Q_PROPERTY(QString title READ title WRITE setTitle USER true)
//! [37]


//! [38]
#this not apply for Python
class MyClass(QObject):

    Q_OBJECT
    Q_ENUMS(Priority)

public:
    MyClass(QObject *parent = 0);
    ~MyClass();

    enum Priority { High, Low, VeryHigh, VeryLow };
    void setPriority(Priority priority);
    Priority priority() const;
};
//! [38]


//! [39]
#this not apply for Python
Q_FLAGS(Options Alignment)
//! [39]


//! [40]
//: This name refers to a host name.
hostNameLabel.setText(self.tr("Name:"))

#: This text refers to a C++ code example. 
example = self.tr("Example")
//! [40]

//! [explicit tr context]
text = QScrollBar.tr("Page up")
//! [explicit tr context]