From b8427aa18824e89a3b28b6b821da4efe0827951b Mon Sep 17 00:00:00 2001 From: Cristian Maureira-Fredes Date: Sat, 23 May 2020 14:10:31 +0200 Subject: Rename and update some snippets * Renaming a few .cpp files into .py * Replacing the use of SIGNAL() and SLOT() * Fixing Python syntax * Removing C++-isms from translated Python code * Including the snippets from QStackedLayout Task-number: PYSIDE-691 Fixes: PYSIDE-1309 Change-Id: I431be3d930b2adc36a22118901baa6799581adb2 Reviewed-by: Friedemann Kleint --- .../doc/src/snippets/clipboard/clipwindow.cpp | 106 ----- .../doc/src/snippets/clipboard/clipwindow.py | 105 +++++ .../snippets/code/src_corelib_kernel_qobject.cpp | 369 ---------------- .../snippets/code/src_corelib_kernel_qobject.py | 375 ++++++++++++++++ ...ols_designer_src_lib_sdk_abstractformwindow.cpp | 75 ---- ...ools_designer_src_lib_sdk_abstractformwindow.py | 74 ++++ ...designer_src_lib_sdk_abstractpropertyeditor.cpp | 73 --- ..._designer_src_lib_sdk_abstractpropertyeditor.py | 71 +++ .../doc/src/snippets/dialogs/dialogs.cpp | 129 ------ .../doc/src/snippets/dialogs/dialogs.py | 123 ++++++ .../doc/src/snippets/dropevents/window.h | 81 ---- .../doc/src/snippets/dropevents/window.py | 65 +++ .../doc/src/snippets/qstackedlayout/main.py | 87 ++++ .../doc/src/snippets/qstackedwidget/main.cpp | 78 ---- .../doc/src/snippets/qstackedwidget/main.py | 77 ++++ .../doc/src/snippets/qxmlquery/bindingExample.cpp | 59 --- .../doc/src/snippets/qxmlquery/bindingExample.py | 59 +++ .../doc/src/snippets/sqldatabase/sqldatabase.cpp | 489 --------------------- .../doc/src/snippets/sqldatabase/sqldatabase.py | 489 +++++++++++++++++++++ .../doc/src/snippets/widgets-tutorial/template.py | 66 +++ 20 files changed, 1591 insertions(+), 1459 deletions(-) delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.h create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.py create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedlayout/main.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.py delete mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.cpp create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.py create mode 100644 sources/pyside2/doc/codesnippets/doc/src/snippets/widgets-tutorial/template.py (limited to 'sources/pyside2/doc/codesnippets/doc/src') diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.cpp deleted file mode 100644 index 726ceb818..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.cpp +++ /dev/null @@ -1,106 +0,0 @@ -############################################################################ -## -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the examples of Qt for Python. -## -## $QT_BEGIN_LICENSE:BSD$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## BSD License Usage -## Alternatively, you may use this file under the terms of the BSD license -## as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -form PySide2.QtGui import * - - -def __init__(self, parent): - QMainWindow.__init__(self, parent) - clipboard = QApplication.clipboard() - - centralWidget = QWidget(self) - currentItem = QWidget(centralWidget) - mimeTypeLabel = QLabel(tr("MIME types:"), currentItem) - mimeTypeCombo = QComboBox(currentItem) - dataLabel = QLabel(tr("Data:"), currentItem) - dataInfoLabel = QLabel("", currentItem) - - previousItems = QListWidget(centralWidget) - -//! [0] - connect(clipboard, SIGNAL("dataChanged()"), self, SLOT("updateClipboard()")) -//! [0] - connect(mimeTypeCombo, SIGNAL("activated(const QString &)"), - self, SLOT("updateData(const QString &))") - - currentLayout = QVBoxLayout(currentItem) - currentLayout.addWidget(mimeTypeLabel) - currentLayout.addWidget(mimeTypeCombo) - currentLayout.addWidget(dataLabel) - currentLayout.addWidget(dataInfoLabel) - currentLayout.addStretch(1) - - mainLayout = QHBoxLayout(centralWidget) - mainLayout.addWidget(currentItem, 1) - mainLayout.addWidget(previousItems) - - setCentralWidget(centralWidget) - setWindowTitle(tr("Clipboard")) - -//! [1] -def updateClipboard(self): - formats = clipboard.mimeData().formats() - data = clipboard.mimeData().data(format) -//! [1] - - mimeTypeCombo.clear() - mimeTypeCombo.insertStringList(formats) - - size = clipboard.mimeData().data(formats[0]).size() - Item = QListWidgetItem(previousItems) - Item.setText(tr("%1 (%2 bytes)").arg(formats[0]).arg(size)) - - updateData(formats[0]) -//! [2] -//! [2] - -def updateData(self, format) - data = clipboard.mimeData().data(format) - dataInfoLabel.setText(tr("%1 bytes").arg(data.size())) diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.py new file mode 100644 index 000000000..1cbd56241 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/clipboard/clipwindow.py @@ -0,0 +1,105 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +form PySide2.QtGui import * + + +def __init__(self, parent): + QMainWindow.__init__(self, parent) + clipboard = QApplication.clipboard() + + centralWidget = QWidget(self) + currentItem = QWidget(centralWidget) + mimeTypeLabel = QLabel(tr("MIME types:"), currentItem) + mimeTypeCombo = QComboBox(currentItem) + dataLabel = QLabel(tr("Data:"), currentItem) + dataInfoLabel = QLabel("", currentItem) + + previousItems = QListWidget(centralWidget) + +//! [0] + clipboard.dataChanged.connect(self.updateClipboard) +//! [0] + mimeTypeCombo.activated[str].connect(self.updateData) + + currentLayout = QVBoxLayout(currentItem) + currentLayout.addWidget(mimeTypeLabel) + currentLayout.addWidget(mimeTypeCombo) + currentLayout.addWidget(dataLabel) + currentLayout.addWidget(dataInfoLabel) + currentLayout.addStretch(1) + + mainLayout = QHBoxLayout(centralWidget) + mainLayout.addWidget(currentItem, 1) + mainLayout.addWidget(previousItems) + + setCentralWidget(centralWidget) + setWindowTitle(tr("Clipboard")) + +//! [1] +def updateClipboard(self): + formats = clipboard.mimeData().formats() + data = clipboard.mimeData().data(format) +//! [1] + + mimeTypeCombo.clear() + mimeTypeCombo.insertStringList(formats) + + size = clipboard.mimeData().data(formats[0]).size() + Item = QListWidgetItem(previousItems) + Item.setText(tr("%1 (%2 bytes)").arg(formats[0]).arg(size)) + + updateData(formats[0]) +//! [2] +//! [2] + +def updateData(self, format) + data = clipboard.mimeData().data(format) + dataInfoLabel.setText(tr("%1 bytes").arg(data.size())) diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.cpp deleted file mode 100644 index 1de4dbbe8..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.cpp +++ /dev/null @@ -1,369 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of Qt for Python. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [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__() -//! [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] diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.py new file mode 100644 index 000000000..7e50f6701 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/src_corelib_kernel_qobject.py @@ -0,0 +1,375 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +//! [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(): ({}) invalid precision {}".format(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] +a_list = window().queryList("QAbstractButton") +for obj in a_list: + obj.setEnabled(false) +//! [9] + + +//! [10] +button = parentWidget.findChild(QPushButton, "button1") +//! [10] + + +//! [11] +a_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(int)'), + label, SLOT('setNum(int)')); +# or scrollBar.valueChanged.connect(label.setNum) +//! [22] + + +//! [23] +// WRONG +QObject.connect(scrollBar, SIGNAL('valueChanged(int value)'), + label, SLOT('setNum(int value)')); +//! [23] + + +//! [24] +class MyWidget(QWidget): + def __init__(self): + myButton = QPushButton(self) + myButton.clicked.connect(self.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__() +//! [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 does not apply to Python +class MyClass(QObject): + + #Q_OBJECT, not needed + #Q_ENUMS(Priority), not supported + + def __init__(self, parent=None): + pass + + class Priority(Enum): + High = 1 + Low = 2 + VeryHigh = 3 + VeryLow 4 + + def setPriority(self, priority): + pass + + priority = Property(...) +}; +//! [38] + + +//! [39] +#this does not apply to 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] diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.cpp deleted file mode 100644 index f9d97e6ce..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of Qt for Python. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] - QDesignerFormWindowInterface *formWindow; - formWindow = QDesignerFormWindowInterface::findFormWindow(myWidget); -//! [0] - - -//! [1] - QList forms; - QDesignerFormWindowInterface *formWindow; - - QDesignerFormWindowManagerInterface *manager = formEditor->formWindowManager(); - - for (int i = 0; i < manager->formWindowCount(); i++) { - formWindow = manager->formWindow(i); - forms.append(formWindow); - } -//! [1] - - -//! [2] - if (formWindow->isManaged(myWidget)) - formWindow->manageWidget(myWidget->childWidget); -//! [2] - - diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.py new file mode 100644 index 000000000..6cf5dd21c --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractformwindow.py @@ -0,0 +1,74 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +//! [0] + formWindow = QDesignerFormWindowInterface() + formWindow = QDesignerFormWindowInterface.findFormWindow(myWidget) +//! [0] + + +//! [1] + forms = [] # QList + formWindow = QDesignerFormWindowInterface() + + manager = formEditor.formWindowManager() + + for i in range(manager.formWindowCount()): + formWindow = manager.formWindow(i) + forms.append(formWindow) +//! [1] + + +//! [2] + if formWindow.isManaged(myWidget): + formWindow.manageWidget(myWidget.childWidget) +//! [2] + + diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.cpp deleted file mode 100644 index 255231512..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of Qt for Python. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] - QDesignerPropertyEditorInterface *propertyEditor = 0; - propertyEditor = formEditor->propertyEditor(); - - connect(propertyEditor, SIGNAL(propertyChanged(QString, QVariant)), - this, SLOT(checkProperty(QString, QVariant))); -//! [0] - - -//! [1] - void checkProperty(QString property, QVariant value) { - QDesignerPropertyEditorInterface *propertyEditor = 0; - propertyEditor = formEditor->propertyEditor(); - - QObject *object = propertyeditor->object(); - MyCustomWidget *widget = qobject_cast(object); - - if (widget && property == aProperty && value != expectedValue) - {...} - } -//! [1] - - diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.py new file mode 100644 index 000000000..1e2ac506f --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/code/tools_designer_src_lib_sdk_abstractpropertyeditor.py @@ -0,0 +1,71 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +//! [0] + propertyEdit = QDesignerPropertyEditorInterface() + propertyEditor = formEditor.propertyEditor() + + propertyEditor.propertyChanged.connect(self.checkProperty) +//! [0] + + +//! [1] + def checkProperty(self, property, value): + propertyEditor = QDesignerPropertyEditorInterface() + propertyEditor = formEditor.propertyEditor() + + object = propertyeditor.object() + widget = MyCustomWidget(object) + + if (widget and property == aProperty and value != expectedValue): + # ... +//! [1] + + diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.cpp deleted file mode 100644 index 047434f6d..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.cpp +++ /dev/null @@ -1,129 +0,0 @@ -############################################################################ -## -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the examples of Qt for Python. -## -## $QT_BEGIN_LICENSE:BSD$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## BSD License Usage -## Alternatively, you may use this file under the terms of the BSD license -## as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - - -//! [0] -def find(self) - - if !self.findDialog: - self.findDialog = FindDialog(self) - connect(findDialog, SIGNAL("findNext()"), self, SLOT("findNext()")) - - - self.findDialog.show() - self.findDialog.raise() - self.findDialog.activateWindow() - -//! [0] - -//! [1] -def countWords(self): - dialog = WordCountDialog(self) - dialog.setWordCount(document().wordCount()) - dialog.exec_() - -//! [1] - -//! [2] - mb = QMessageBox("Application Name", - "Hardware failure.\n\nDisk error detected\nDo you want to stop?", - QMessageBox.Question, - QMessageBox.Yes | QMessageBox.Default, - QMessageBox.No | QMessageBox.Escape, - QMessageBox.NoButton) - if mb.exec() == QMessageBox.No: - # try again -//! [2] - -//! [3] - progress = QProgressDialog("Copying files...", "Abort Copy", 0, numFiles, self) - progress.setWindowModality(Qt.WindowModal) - - for i in rang(numFiles): - progress.setValue(i) - - if progress.wasCanceled(): - break - #... copy one file - - progress.setValue(numFiles) -//! [3] - -//! [4] -# Operation constructor -def __init__(self, parent): - QObject.__init__(self, parent) - - pd = QProgressDialog("Operation in progress.", "Cancel", 0, 100) - connect(pd, SIGNAL("canceled()"), self, SLOT("cancel()")) - t = QTimer(self) - connect(t, SIGNAL("timeout()"), self, SLOT("perform()")) - t.start(0) - -//! [4] //! [5] - -def perform(self): - - pd.setValue(steps) - #... perform one percent of the operation - steps++ - if steps > pd.maximum(): - t.stop() - -//! [5] //! [6] - -def cancel(self): - - t.stop() - #... cleanup - -//! [6] - - diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.py new file mode 100644 index 000000000..7bdcb0c91 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/dialogs/dialogs.py @@ -0,0 +1,123 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + + +//! [0] +def find(self): + if not self.findDialog: + self.findDialog = FindDialog(self) + self.findDialog.findNext.connect(self.findNext) + + self.findDialog.show() + self.findDialog.raise() + self.findDialog.activateWindow() + +//! [0] + +//! [1] +def countWords(self): + dialog = WordCountDialog(self) + dialog.setWordCount(document().wordCount()) + dialog.exec_() + +//! [1] + +//! [2] + mb = QMessageBox("Application Name", + "Hardware failure.\n\nDisk error detected\nDo you want to stop?", + QMessageBox.Question, + QMessageBox.Yes | QMessageBox.Default, + QMessageBox.No | QMessageBox.Escape, + QMessageBox.NoButton) + if mb.exec() == QMessageBox.No: + # try again +//! [2] + +//! [3] + progress = QProgressDialog("Copying files...", "Abort Copy", 0, numFiles, self) + progress.setWindowModality(Qt.WindowModal) + + for i in range(numFiles): + progress.setValue(i) + + if progress.wasCanceled(): + break + #... copy one file + + progress.setValue(numFiles) +//! [3] + +//! [4] +# Operation constructor +def __init__(self, parent=None): + QObject.__init__(self, parent) + + pd = QProgressDialog("Operation in progress.", "Cancel", 0, 100) + pd.canceled.connect(self.cancel) + t = QTimer(self) + t.timeout.connect(self.perform) + t.start(0) + +//! [4] //! [5] + +def perform(self): + pd.setValue(steps) + #... perform one percent of the operation + steps += 1 + if steps > pd.maximum(): + t.stop() + +//! [5] //! [6] + +def cancel(self): + t.stop() + #... cleanup + +//! [6] diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.h b/sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.h deleted file mode 100644 index 8607b6953..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.h +++ /dev/null @@ -1,81 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of Qt for Python. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef WINDOW_H -#define WINDOW_H - -#include -#include -#include - -class QComboBox; -class QFrame; -class QTextBrowser; - -class Window : public QWidget -{ - Q_OBJECT - -public: - Window(QWidget *parent = 0); - -protected: - void dragEnterEvent(QDragEnterEvent *event); - void dropEvent(QDropEvent *event); - -private: - QComboBox *mimeTypeCombo; - QFrame *dropFrame; - QTextBrowser *textBrowser; - QString oldText; - QStringList oldMimeTypes; -}; - -#endif diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.py new file mode 100644 index 000000000..66a6a7ba2 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/dropevents/window.py @@ -0,0 +1,65 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +from PySide2.QtWidgets import QWidget, QComboBox, QFrame, QTextBrowser + +class Window(QWidget): + + def __init__(self, parent=None): + self.mimeTypeCombo = QComboBox() + self.dropFrame = QFrame() + self.textBrowser = QTextBrowser() + self.oldText = "" + self.oldMimeTypes = [] + + def dragEnterEvent(self, event): + pass + def dropEvent(self, event): + pass diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedlayout/main.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedlayout/main.py new file mode 100644 index 000000000..184128406 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedlayout/main.py @@ -0,0 +1,87 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +from PySide2.QtWidgets import QApplication, QWidget, QStackedLayout, QComboBox + +class Widget(QWidget) + def __init__(self, parent=None): + QWidget.__init__(self, parent) +//! [0] + self.firstPageWidget = QWidget() + self.secondPageWidget = QWidget() + self.thirdPageWidget = QWidget() + + self.stackedLayout = QStackedLayout() + self.stackedLayout.addWidget(self.firstPageWidget) + self.stackedLayout.addWidget(self.secondPageWidget) + self.stackedLayout.addWidget(self.thirdPageWidget) + +//! [0] //! [1] + self.pageComboBox = QComboBox() + self.pageComboBox.addItem(tr("Page 1")) + self.pageComboBox.addItem(tr("Page 2")) + self.pageComboBox.addItem(tr("Page 3")) + self.pageComboBox.activated.connect(self.stackedLayout.setCurrentIndex) +//! [1] + +//! [2] + self.mainLayout = QVBoxLayout() +//! [2] + self.mainLayout.addWidget(self.pageComboBox) +//! [3] + self.mainLayout.addLayout(self.stackedLayout) + self.setLayout(self.mainLayout) +//! [3] + +if __name__ == "__main__": + app = QApplication(sys.argv) + widget = Widget() + widget.show() + sys.exit(app.exec_()) diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.cpp deleted file mode 100644 index 98a08be98..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -############################################################################ -## -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the examples of Qt for Python. -## -## $QT_BEGIN_LICENSE:BSD$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## BSD License Usage -## Alternatively, you may use this file under the terms of the BSD license -## as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - - -//! [0] - firstPageWidget = QWidget() - secondPageWidget = QWidget() - thirdPageWidget = QWidget() - - stackedWidget = QStackedWidget() - stackedWidget.addWidget(firstPageWidget) - stackedWidget.addWidget(secondPageWidget) - stackedWidget.addWidget(thirdPageWidget) - -//! [0] //! [1] - pageComboBox = QComboBox() - pageComboBox.addItem(tr("Page 1")) - pageComboBox.addItem(tr("Page 2")) - pageComboBox.addItem(tr("Page 3")) - connect(pageComboBox, SIGNAL("activated(int)"), - stackedWidget, SLOT("setCurrentIndex(int)")) - -//! [1] //! [2] - layout = QVBoxLayout() -//! [2] - layout.addWidget(pageComboBox) -//! [3] - layout.addWidget(stackedWidget) - setLayout(layout) -//! [3] - diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.py new file mode 100644 index 000000000..6f2c49d8e --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/qstackedwidget/main.py @@ -0,0 +1,77 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + + +//! [0] + firstPageWidget = QWidget() + secondPageWidget = QWidget() + thirdPageWidget = QWidget() + + stackedWidget = QStackedWidget() + stackedWidget.addWidget(firstPageWidget) + stackedWidget.addWidget(secondPageWidget) + stackedWidget.addWidget(thirdPageWidget) + +//! [0] //! [1] + pageComboBox = QComboBox() + pageComboBox.addItem(tr("Page 1")) + pageComboBox.addItem(tr("Page 2")) + pageComboBox.addItem(tr("Page 3")) + pageComboBox.activated[int].connect(stackedWidget.setCurrentIndex) + +//! [1] //! [2] + layout = QVBoxLayout() +//! [2] + layout.addWidget(pageComboBox) +//! [3] + layout.addWidget(stackedWidget) + setLayout(layout) +//! [3] + diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.cpp deleted file mode 100644 index 734f603a9..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of Qt for Python. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] - QBuffer device; - device.setData(myQString.toUtf8()); - device.open(QIODevice::ReadOnly); - - QXmlQuery query; - query.setQuery("doc($inputDocument)/query[theDocument]"); - query.bindVariable("inputDocument", &device); -//! [0] diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.py new file mode 100644 index 000000000..16c12b7bc --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/qxmlquery/bindingExample.py @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of Qt for Python. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] + device = QBuffer() + device.setData(myQString.toUtf8()) + device.open(QIODevice.ReadOnly) + + query = QXmlQuery() + query.setQuery("doc($inputDocument)/query[theDocument]") + query.bindVariable("inputDocument", device) +//! [0] diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.cpp b/sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.cpp deleted file mode 100644 index 29ac9c87d..000000000 --- a/sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.cpp +++ /dev/null @@ -1,489 +0,0 @@ -############################################################################ -## -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the examples of Qt for Python. -## -## $QT_BEGIN_LICENSE:BSD$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## BSD License Usage -## Alternatively, you may use this file under the terms of the BSD license -## as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -from PySide2.QtGui import * -from PySide2.QtSql import * - - -def tr(text): - return QApplication.translate(text, text) - -def QSqlDatabase_snippets(): -//! [0] - db = QSqlDatabase.addDatabase("QPSQL") - db.setHostName("acidalia") - db.setDatabaseName("customdb") - db.setUserName("mojito") - db.setPassword("J0a1m8") - ok = db.open() -//! [0] - -//! [1] - db = QSqlDatabase.database() -//! [1] - -def QSqlField_snippets(): -//! [2] - field = QSqlField("age", QVariant.Int) - field.setValue(QPixmap()) # WRONG -//! [2] - -//! [3] - field = QSqlField("age", QVariant.Int) - field.setValue(QString("123")) # casts QString to int -//! [3] - -//! [4] - query = QSqlQuery() -//! [4] //! [5] - record = query.record() -//! [5] //! [6] - field = record.field("country") -//! [6] - -def doSomething(str): - pass - -def QSqlQuery_snippets(): - # typical loop -//! [7] - query = QSqlQuery("SELECT country FROM artist") - while query.next(): - country = query.value(0) - doSomething(country) -//! [7] - - - # field index lookup -//! [8] - query = QSqlQuery("SELECT * FROM artist") - fieldNo = query.record().indexOf("country") - while query.next(): - country = query.value(fieldNo) - doSomething(country) -//! [8] - - # named with named -//! [9] - query = QSqlQuery() - query.prepare("INSERT INTO person (id, forename, surname) " - "VALUES (:id, :forename, :surname)") - query.bindValue(":id", 1001) - query.bindValue(":forename", "Bart") - query.bindValue(":surname", "Simpson") - query.exec_() -//! [9] - - # positional with named -//! [10] - query = QSqlQuery() - query.prepare("INSERT INTO person (id, forename, surname) " - "VALUES (:id, :forename, :surname)") - query.bindValue(0, 1001) - query.bindValue(1, "Bart") - query.bindValue(2, "Simpson") - query.exec_() -//! [10] - - # positional 1 -//! [11] - query = QSqlQuery() - query.prepare("INSERT INTO person (id, forename, surname) " - "VALUES (?, ?, ?)") - query.bindValue(0, 1001) - query.bindValue(1, "Bart") - query.bindValue(2, "Simpson") - query.exec_() -//! [11] - - # positional 2 -//! [12] - query = QSqlQuery() - query.prepare("INSERT INTO person (id, forename, surname) " - "VALUES (?, ?, ?)") - query.addBindValue(1001) - query.addBindValue("Bart") - query.addBindValue("Simpson") - query.exec_() -//! [12] - - # stored -//! [13] - query = QSqlQuery() - query.prepare("CALL AsciiToInt(?, ?)") - query.bindValue(0, "A") - query.bindValue(1, 0, QSql.Out) - query.exec_() - i = query.boundValue(1) # i is 65 -//! [13] - - query = QSqlQuery() - - # examine with named binding -//! [14] - i = query.boundValues() - while i.hasNext(): - i.next() - print i.key(), ": ", i.value() -//! [14] - - # examine with positional binding -//! [15] - list_ = query.boundValues().values() - for item in list: - print item -//! [15] - -def QSqlQueryModel_snippets(): - -//! [16] - model = QSqlQueryModel() - model.setQuery("SELECT name, salary FROM employee") - model.setHeaderData(0, Qt.Horizontal, tr("Name")) - model.setHeaderData(1, Qt.Horizontal, tr("Salary")) - -//! [17] - view = QTableView() -//! [17] //! [18] - view.setModel(model) -//! [18] //! [19] - view.show() -//! [16] //! [19] //! [20] - view.setEditTriggers(QAbstractItemView.NoEditTriggers) -//! [20] - -//! [21] - model = QSqlQueryModel() - model.setQuery("SELECT * FROM employee") - salary = model.record(4).value("salary") -//! [21] - -//! [22] - salary = model.data(model.index(4, 2)) -//! [22] - - for row in range(model.rowCount()): - for (col in range(model.columnCount())): - print model.data(model.index(row, col)) - - -class MyModel(QSqlQueryModel) - m_specialColumnNo = 0 - def data(item, role): -//! [23] - if item.column() == self.m_specialColumnNo: - # handle column separately - pass - - return QSqlQueryModel.data(item, role) - -//! [23] - - -def QSqlTableModel_snippets(): - -//! [24] - model = QSqlTableModel() - model.setTable("employee") - model.setEditStrategy(QSqlTableModel.OnManualSubmit) - model.select() - model.removeColumn(0) # don't show the ID - model.setHeaderData(0, Qt.Horizontal, tr("Name")) - model.setHeaderData(1, Qt.Horizontal, tr("Salary")) - - view = QTableView() - view.setModel(model) - view.show() -//! [24] - - -//! [25] - model = QSqlTableModel() - model.setTable("employee") - QString name = model.record(4).value("name") -//! [25] - -def sql_intro_snippets(): - -//! [26] - db = QSqlDatabase.addDatabase("QMYSQL") - db.setHostName("bigblue") - db.setDatabaseName("flightdb") - db.setUserName("acarlson") - db.setPassword("1uTbSbAs") - ok = db.open() -//! [26] - -//! [27] - firstDB = QSqlDatabase.addDatabase("QMYSQL", "first") - secondDB = QSqlDatabase.addDatabase("QMYSQL", "second") -//! [27] - -//! [28] - defaultDB = QSqlDatabase.database() -//! [28] //! [29] - firstDB = QSqlDatabase.database("first") -//! [29] //! [30] - secondDB = QSqlDatabase.database("second") -//! [30] - - # SELECT1 -//! [31] - query = QSqlQuery() - query.exec_("SELECT name, salary FROM employee WHERE salary > 50000") -//! [31] - -//! [32] - while query.next(): - name = query.value(0) - salary = query.value(1) - print name, salary -//! [32] - - # FEATURE -//! [33] - query = QSqlQuery() - query.exec_("SELECT name, salary FROM employee WHERE salary > 50000") - - defaultDB = QSqlDatabase.database() - if defaultDB.driver().hasFeature(QSqlDriver.QuerySize): - numRows = query.size() - else: - # self can be very slow - query.last() - numRows = query.at() + 1 -//! [33] - - # INSERT1 -//! [34] - query = QSqlQuery() - query.exec_("INSERT INTO employee (id, name, salary) " - "VALUES (1001, 'Thad Beaumont', 65000)") -//! [34] - - # NAMED BINDING -//! [35] - query = QSqlQuery() - query.prepare("INSERT INTO employee (id, name, salary) " - "VALUES (:id, :name, :salary)") - query.bindValue(":id", 1001) - query.bindValue(":name", "Thad Beaumont") - query.bindValue(":salary", 65000) - query.exec_() -//! [35] - - # POSITIONAL BINDING -//! [36] - query = QSqlQuery() - query.prepare("INSERT INTO employee (id, name, salary) " - "VALUES (?, ?, ?)") - query.addBindValue(1001) - query.addBindValue("Thad Beaumont") - query.addBindValue(65000) - query.exec_() -//! [36] - - # UPDATE1 -//! [37] - query = QSqlQuery() - query.exec_("UPDATE employee SET salary = 70000 WHERE id = 1003") -//! [37] - - # DELETE1 -//! [38] - query = QSqlQuery() - query.exec_("DELETE FROM employee WHERE id = 1007") -//! [38] - - # TRANSACTION -//! [39] - QSqlDatabase.database().transaction() - query = QSqlQuery() - query.exec_("SELECT id FROM employee WHERE name = 'Torild Halvorsen'") - if query.next(): - employeeId = query.value(0) - query.exec_("INSERT INTO project (id, name, ownerid) " - "VALUES (201, 'Manhattan Project', " - + QString.number(employeeId) + ')') - - QSqlDatabase.database().commit() -//! [39] - - # SQLQUERYMODEL1 -//! [40] - model = QSqlQueryModel() - model.setQuery("SELECT * FROM employee") - - for i in range(model.rowCount()): - _id = model.record(i).value("id") - name = model.record(i).value("name") - print _id, name - -//! [40] - } - - { - # SQLTABLEMODEL1 -//! [41] - model = QSqlTableModel() - model.setTable("employee") - model.setFilter("salary > 50000") - model.setSort(2, Qt.DescendingOrder) - model.select() - - for i in range(model.rowCount()): - name = model.record(i).value("name") - salary = model.record(i).value("salary") - print "%s: %d" % (name, salary) - -//! [41] - - # SQLTABLEMODEL2 - model = QSqlTableModel() - model.setTable("employee") - -//! [42] - for i in range(model.rowCount()): - record = model.record(i) - salary = record.value("salary") - salary *= 1.1 - record.setValue("salary", salary) - model.setRecord(i, record) - - model.submitAll() -//! [42] - - # SQLTABLEMODEL3 - row = 1 - column = 2 -//! [43] - model.setData(model.index(row, column), 75000) - model.submitAll() -//! [43] - - # SQLTABLEMODEL4 -//! [44] - model.insertRows(row, 1) - model.setData(model.index(row, 0), 1013) - model.setData(model.index(row, 1), "Peter Gordon") - model.setData(model.index(row, 2), 68500) - model.submitAll() -//! [44] - -//! [45] - model.removeRows(row, 5) -//! [45] - -//! [46] - model.submitAll() -//! [46] - -//! [47] -class XyzResult(QSqlResult): - def __init__(driver): - QSqlResult.__init__(self, driver) - pass - - def data(self, index): - return QVariant() - - def isNull(self, index): - return False - - def reset(self, query): - return False - - def fetch(self, index): - return False - - def fetchFirst(self): - return False - - def fetchLast(self): - return False - - def size(self): - return 0 - - def numRowsAffected(self): - return 0 - - def record(self): - return QSqlRecord() - -//! [47] - -//! [48] -class XyzDriver(QSqlDriver) - def hasFeature(self, feature): - return False - - def open(self, db, user, password, host, port, options): - return False - - def close(self): - pass - - def createResult(self): - return XyzResult(self) - -//! [48] - -def main(): - app = QApplication([]) - - QSqlDatabase_snippets() - QSqlField_snippets() - QSqlQuery_snippets() - QSqlQueryModel_snippets() - QSqlTableModel_snippets() - - driver = XyzDriver() - result = XyzResult(driver) diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.py new file mode 100644 index 000000000..7c28cf5e6 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/sqldatabase/sqldatabase.py @@ -0,0 +1,489 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the examples of Qt for Python. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +from PySide2.QtGui import * +from PySide2.QtSql import * + + +def tr(text): + return QApplication.translate(text, text) + +def QSqlDatabase_snippets(): +//! [0] + db = QSqlDatabase.addDatabase("QPSQL") + db.setHostName("acidalia") + db.setDatabaseName("customdb") + db.setUserName("mojito") + db.setPassword("J0a1m8") + ok = db.open() +//! [0] + +//! [1] + db = QSqlDatabase.database() +//! [1] + +def QSqlField_snippets(): +//! [2] + field = QSqlField("age", QVariant.Int) + field.setValue(QPixmap()) # WRONG +//! [2] + +//! [3] + field = QSqlField("age", QVariant.Int) + field.setValue(str(123)) # casts str to int +//! [3] + +//! [4] + query = QSqlQuery() +//! [4] //! [5] + record = query.record() +//! [5] //! [6] + field = record.field("country") +//! [6] + +def doSomething(str): + pass + +def QSqlQuery_snippets(): + # typical loop +//! [7] + query = QSqlQuery("SELECT country FROM artist") + while query.next(): + country = query.value(0) + doSomething(country) +//! [7] + + + # field index lookup +//! [8] + query = QSqlQuery("SELECT * FROM artist") + fieldNo = query.record().indexOf("country") + while query.next(): + country = query.value(fieldNo) + doSomething(country) +//! [8] + + # named with named +//! [9] + query = QSqlQuery() + query.prepare("INSERT INTO person (id, forename, surname) " + "VALUES (:id, :forename, :surname)") + query.bindValue(":id", 1001) + query.bindValue(":forename", "Bart") + query.bindValue(":surname", "Simpson") + query.exec_() +//! [9] + + # positional with named +//! [10] + query = QSqlQuery() + query.prepare("INSERT INTO person (id, forename, surname) " + "VALUES (:id, :forename, :surname)") + query.bindValue(0, 1001) + query.bindValue(1, "Bart") + query.bindValue(2, "Simpson") + query.exec_() +//! [10] + + # positional 1 +//! [11] + query = QSqlQuery() + query.prepare("INSERT INTO person (id, forename, surname) " + "VALUES (?, ?, ?)") + query.bindValue(0, 1001) + query.bindValue(1, "Bart") + query.bindValue(2, "Simpson") + query.exec_() +//! [11] + + # positional 2 +//! [12] + query = QSqlQuery() + query.prepare("INSERT INTO person (id, forename, surname) " + "VALUES (?, ?, ?)") + query.addBindValue(1001) + query.addBindValue("Bart") + query.addBindValue("Simpson") + query.exec_() +//! [12] + + # stored +//! [13] + query = QSqlQuery() + query.prepare("CALL AsciiToInt(?, ?)") + query.bindValue(0, "A") + query.bindValue(1, 0, QSql.Out) + query.exec_() + i = query.boundValue(1) # i is 65 +//! [13] + + query = QSqlQuery() + + # examine with named binding +//! [14] + i = query.boundValues() + while i.hasNext(): + i.next() + print i.key(), ": ", i.value() +//! [14] + + # examine with positional binding +//! [15] + list_ = query.boundValues().values() + for item in list: + print item +//! [15] + +def QSqlQueryModel_snippets(): + +//! [16] + model = QSqlQueryModel() + model.setQuery("SELECT name, salary FROM employee") + model.setHeaderData(0, Qt.Horizontal, tr("Name")) + model.setHeaderData(1, Qt.Horizontal, tr("Salary")) + +//! [17] + view = QTableView() +//! [17] //! [18] + view.setModel(model) +//! [18] //! [19] + view.show() +//! [16] //! [19] //! [20] + view.setEditTriggers(QAbstractItemView.NoEditTriggers) +//! [20] + +//! [21] + model = QSqlQueryModel() + model.setQuery("SELECT * FROM employee") + salary = model.record(4).value("salary") +//! [21] + +//! [22] + salary = model.data(model.index(4, 2)) +//! [22] + + for row in range(model.rowCount()): + for (col in range(model.columnCount())): + print model.data(model.index(row, col)) + + +class MyModel(QSqlQueryModel) + m_specialColumnNo = 0 + def data(item, role): +//! [23] + if item.column() == self.m_specialColumnNo: + # handle column separately + pass + + return QSqlQueryModel.data(item, role) + +//! [23] + + +def QSqlTableModel_snippets(): + +//! [24] + model = QSqlTableModel() + model.setTable("employee") + model.setEditStrategy(QSqlTableModel.OnManualSubmit) + model.select() + model.removeColumn(0) # don't show the ID + model.setHeaderData(0, Qt.Horizontal, tr("Name")) + model.setHeaderData(1, Qt.Horizontal, tr("Salary")) + + view = QTableView() + view.setModel(model) + view.show() +//! [24] + + +//! [25] + model = QSqlTableModel() + model.setTable("employee") + name = model.record(4).value("name") +//! [25] + +def sql_intro_snippets(): + +//! [26] + db = QSqlDatabase.addDatabase("QMYSQL") + db.setHostName("bigblue") + db.setDatabaseName("flightdb") + db.setUserName("acarlson") + db.setPassword("1uTbSbAs") + ok = db.open() +//! [26] + +//! [27] + firstDB = QSqlDatabase.addDatabase("QMYSQL", "first") + secondDB = QSqlDatabase.addDatabase("QMYSQL", "second") +//! [27] + +//! [28] + defaultDB = QSqlDatabase.database() +//! [28] //! [29] + firstDB = QSqlDatabase.database("first") +//! [29] //! [30] + secondDB = QSqlDatabase.database("second") +//! [30] + + # SELECT1 +//! [31] + query = QSqlQuery() + query.exec_("SELECT name, salary FROM employee WHERE salary > 50000") +//! [31] + +//! [32] + while query.next(): + name = query.value(0) + salary = query.value(1) + print name, salary +//! [32] + + # FEATURE +//! [33] + query = QSqlQuery() + query.exec_("SELECT name, salary FROM employee WHERE salary > 50000") + + defaultDB = QSqlDatabase.database() + if defaultDB.driver().hasFeature(QSqlDriver.QuerySize): + numRows = query.size() + else: + # self can be very slow + query.last() + numRows = query.at() + 1 +//! [33] + + # INSERT1 +//! [34] + query = QSqlQuery() + query.exec_("INSERT INTO employee (id, name, salary) " + "VALUES (1001, 'Thad Beaumont', 65000)") +//! [34] + + # NAMED BINDING +//! [35] + query = QSqlQuery() + query.prepare("INSERT INTO employee (id, name, salary) " + "VALUES (:id, :name, :salary)") + query.bindValue(":id", 1001) + query.bindValue(":name", "Thad Beaumont") + query.bindValue(":salary", 65000) + query.exec_() +//! [35] + + # POSITIONAL BINDING +//! [36] + query = QSqlQuery() + query.prepare("INSERT INTO employee (id, name, salary) " + "VALUES (?, ?, ?)") + query.addBindValue(1001) + query.addBindValue("Thad Beaumont") + query.addBindValue(65000) + query.exec_() +//! [36] + + # UPDATE1 +//! [37] + query = QSqlQuery() + query.exec_("UPDATE employee SET salary = 70000 WHERE id = 1003") +//! [37] + + # DELETE1 +//! [38] + query = QSqlQuery() + query.exec_("DELETE FROM employee WHERE id = 1007") +//! [38] + + # TRANSACTION +//! [39] + QSqlDatabase.database().transaction() + query = QSqlQuery() + query.exec_("SELECT id FROM employee WHERE name = 'Torild Halvorsen'") + if query.next(): + employeeId = query.value(0) + query.exec_("INSERT INTO project (id, name, ownerid) " + "VALUES (201, 'Manhattan Project', " + + str(employeeId) + ')') + + QSqlDatabase.database().commit() +//! [39] + + # SQLQUERYMODEL1 +//! [40] + model = QSqlQueryModel() + model.setQuery("SELECT * FROM employee") + + for i in range(model.rowCount()): + _id = model.record(i).value("id") + name = model.record(i).value("name") + print _id, name + +//! [40] + } + + { + # SQLTABLEMODEL1 +//! [41] + model = QSqlTableModel() + model.setTable("employee") + model.setFilter("salary > 50000") + model.setSort(2, Qt.DescendingOrder) + model.select() + + for i in range(model.rowCount()): + name = model.record(i).value("name") + salary = model.record(i).value("salary") + print "%s: %d" % (name, salary) + +//! [41] + + # SQLTABLEMODEL2 + model = QSqlTableModel() + model.setTable("employee") + +//! [42] + for i in range(model.rowCount()): + record = model.record(i) + salary = record.value("salary") + salary *= 1.1 + record.setValue("salary", salary) + model.setRecord(i, record) + + model.submitAll() +//! [42] + + # SQLTABLEMODEL3 + row = 1 + column = 2 +//! [43] + model.setData(model.index(row, column), 75000) + model.submitAll() +//! [43] + + # SQLTABLEMODEL4 +//! [44] + model.insertRows(row, 1) + model.setData(model.index(row, 0), 1013) + model.setData(model.index(row, 1), "Peter Gordon") + model.setData(model.index(row, 2), 68500) + model.submitAll() +//! [44] + +//! [45] + model.removeRows(row, 5) +//! [45] + +//! [46] + model.submitAll() +//! [46] + +//! [47] +class XyzResult(QSqlResult): + def __init__(driver): + QSqlResult.__init__(self, driver) + pass + + def data(self, index): + return QVariant() + + def isNull(self, index): + return False + + def reset(self, query): + return False + + def fetch(self, index): + return False + + def fetchFirst(self): + return False + + def fetchLast(self): + return False + + def size(self): + return 0 + + def numRowsAffected(self): + return 0 + + def record(self): + return QSqlRecord() + +//! [47] + +//! [48] +class XyzDriver(QSqlDriver) + def hasFeature(self, feature): + return False + + def open(self, db, user, password, host, port, options): + return False + + def close(self): + pass + + def createResult(self): + return XyzResult(self) + +//! [48] + +def main(): + app = QApplication([]) + + QSqlDatabase_snippets() + QSqlField_snippets() + QSqlQuery_snippets() + QSqlQueryModel_snippets() + QSqlTableModel_snippets() + + driver = XyzDriver() + result = XyzResult(driver) diff --git a/sources/pyside2/doc/codesnippets/doc/src/snippets/widgets-tutorial/template.py b/sources/pyside2/doc/codesnippets/doc/src/snippets/widgets-tutorial/template.py new file mode 100644 index 000000000..d38829fc7 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/doc/src/snippets/widgets-tutorial/template.py @@ -0,0 +1,66 @@ +############################################################################ +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the documentation of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## BSD License Usage +## Alternatively, you may use this file under the terms of the BSD license +## as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################ + +//! [main.cpp body] +import sys +from PySide2.QtWidgets import QApplication + +# Include header files for application components. +# ... + +if __name__ == "__main__": + app = QApplication(sys.argv) + + # Set up and show widgets. + # ... + + sys.exit(app.exec_()) +} +//! [main.cpp body] -- cgit v1.2.3