diff options
| author | Cristian Maureira-Fredes <Cristian.Maureira-Fredes@qt.io> | 2020-05-23 14:10:31 +0200 |
|---|---|---|
| committer | Cristian Maureira-Fredes <Cristian.Maureira-Fredes@qt.io> | 2020-05-25 18:37:51 +0200 |
| commit | b8427aa18824e89a3b28b6b821da4efe0827951b (patch) | |
| tree | a440c1cab4e85c8c9ed95e38969e21707fc97087 /sources/pyside2/doc/codesnippets/examples/mainwindows/application | |
| parent | 89f5b036809e8c92d1ce5845417b644064953507 (diff) | |
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 <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside2/doc/codesnippets/examples/mainwindows/application')
| -rw-r--r-- | sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.h | 112 | ||||
| -rw-r--r-- | sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.py (renamed from sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp) | 82 |
2 files changed, 152 insertions, 42 deletions
diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.h b/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.h new file mode 100644 index 000000000..bdb7bcf22 --- /dev/null +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.h @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples 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 (QAction, QApplication, QMainWindow, QMenu, + QPlainTextEdit, QSessionManager) + + +//! [0] +class MainWindow(QMainWindow): + def __init__(self, parent=None): + self.textEdit = QPlainTextEdit() + self.curFile = "" + # ... + + def loadFile(self, fileName): + pass + + def closeEvent(self, event): + pass + + def newFile(self): + pass + + def open(self): + pass + + def save(self): + pass + + def saveAs(self): + pass + + def about(self): + pass + + def documentWasModified(self): + pass + # Enable this only if QT_NO_SESSIONMANAGER is not defined + # def commitData(self): + # pass + + def createActions(self): + pass + + def createStatusBar(self): + pass + + def readSettings(self): + pass + + def writeSettings(self): + pass + + def maybeSave(self): + pass + + def saveFile(self, fileName): + pass + + def setCurrentFile(self, fileName): + pass + + def strippedName(self, fullFileName): + pass +//! [0] diff --git a/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp b/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.py index b0331aa79..f976bb8e3 100644 --- a/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp +++ b/sources/pyside2/doc/codesnippets/examples/mainwindows/application/mainwindow.py @@ -49,28 +49,30 @@ ############################################################################ //! [0] -from PySide2.QtGui import * +from PySide2.QtCore import Qt, QFile, QFileInfo, QSettings, QTextStream +from PySide2.QtGui import QIcon +from PySide2.Widgets import (QAction, QApplication, QFileDialog, QMainWindow, + QPlainTextEdit, QFileDialog, QMessageBox, ) //! [0] //! [1] -def __init__(self): +def __init__(self, parent=None): QMainWindow.__init__(self) //! [1] //! [2] - textEdit = QPlainTextEdit() - setCentralWidget(textEdit) + self.textEdit = QPlainTextEdit() + self.setCentralWidget(textEdit) - createActions() - createMenus() - createToolBars() - createStatusBar() + self.createActions() + self.createMenus() + self.createToolBars() + self.createStatusBar() - readSettings() + self.readSettings() - connect(textEdit.document(), SIGNAL("contentsChanged()"), - self, SLOT("documentWasModified()")) + self.textEdit.document().contentsChanged.connect(self.documentWasModified) - setCurrentFile("") - setUnifiedTitleAndToolBarOnMac(True) + self.setCurrentFile("") + self.setUnifiedTitleAndToolBarOnMac(True) //! [2] @@ -97,7 +99,7 @@ def open(self): //! [7] //! [8] if maybeSave(): fileName = QFileDialog.getOpenFileName(self) - if !fileName.isEmpty(): + if not fileName.isEmpty(): loadFile(fileName) //! [8] @@ -142,70 +144,68 @@ def MainWindow.createActions(self): Act = QAction(QIcon(":/images/new.png"), tr("&New"), self) Act.setShortcuts(QKeySequence.New) Act.setStatusTip(tr("Create a new file")) - connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) + Act.triggered.connect(newFile) //! [19] - openAct = QAction(QIcon(":/images/open.png"), tr("&Open..."), self) + openAct = QAction(QIcon(":/images/open.png"), tr("&Open..."), self) openAct.setShortcuts(QKeySequence.Open) openAct.setStatusTip(tr("Open an existing file")) - connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) + openAct.triggered.connect(open) //! [18] //! [19] - saveAct = QAction(QIcon(":/images/save.png"), tr("&Save"), self) + saveAct = QAction(QIcon(":/images/save.png"), tr("&Save"), self) saveAct.setShortcuts(QKeySequence.Save) saveAct.setStatusTip(tr("Save the document to disk")) - connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + saveAct.triggered.connect(save) - saveAsAct = QAction(tr("Save &As..."), self) + saveAsAct = QAction(tr("Save &As..."), self) saveAsAct.setShortcuts(QKeySequence.SaveAs) saveAsAct.setStatusTip(tr("Save the document under a name")) - connect(saveAsAct, SIGNAL("triggered()"), self, SLOT("saveAs()")) + saveAsAct.triggered.connect(saveAs) //! [20] - exitAct = QAction(tr("E&xit"), self) + exitAct = QAction(tr("E&xit"), self) exitAct.setShortcut(tr("Ctrl+Q")) //! [20] exitAct.setStatusTip(tr("Exit the application")) - connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + exitAct.triggered.connect(close) //! [21] - cutAct = QAction(QIcon(":/images/cut.png"), tr("Cu&t"), self) + cutAct = QAction(QIcon(":/images/cut.png"), tr("Cu&t"), self) //! [21] cutAct.setShortcuts(QKeySequence.Cut) cutAct.setStatusTip(tr("Cut the current selection's contents to the " "clipboard")) - connect(cutAct, SIGNAL("triggered()"), textEdit, SLOT("cut()")) + cutAct.triggered.connect(cut) - copyAct = QAction(QIcon(":/images/copy.png"), tr("&Copy"), self) + copyAct = QAction(QIcon(":/images/copy.png"), tr("&Copy"), self) copyAct.setShortcuts(QKeySequence.Copy) copyAct.setStatusTip(tr("Copy the current selection's contents to the " "clipboard")) - connect(copyAct, SIGNAL("triggered()"), textEdit, SLOT("copy()")) + copyAct.triggered.connect(copy) - pasteAct = QAction(QIcon(":/images/paste.png"), tr("&Paste"), self) + pasteAct = QAction(QIcon(":/images/paste.png"), tr("&Paste"), self) pasteAct.setShortcuts(QKeySequence.Paste) pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " "selection")) - connect(pasteAct, SIGNAL("triggered()"), textEdit, SLOT("paste()")) + pasteAct.triggered.connect(textEdit.paste) - aboutAct = QAction(tr("&About"), self) + aboutAct = QAction(tr("&About"), self) aboutAct.setStatusTip(tr("Show the application's About box")) - connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + aboutAct.triggered.connect(about) //! [22] aboutQtAct = QAction(tr("About &Qt"), self) aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) - connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + aboutQtAct.triggered.connect(qApp.aboutQt) //! [22] //! [23] cutAct.setEnabled(False) //! [23] //! [24] copyAct.setEnabled(False) - connect(textEdit, SIGNAL("copyAvailable(bool)"), - cutAct, SLOT("setEnabled(bool)")) - connect(textEdit, SIGNAL("copyAvailable(bool)"), - copyAct, SLOT("setEnabled(bool)")) + textEdit.copyAvailable[bool].connect(cutAct.setEnabled) + textEdit.copyAvailable[bool].connect(copyAct.setEnabled) } //! [24] @@ -298,10 +298,8 @@ def loadFile(self, fileName): //! [42] //! [43] file = QFile(fileName) if !file.open(QFile.ReadOnly | QFile.Text): - QMessageBox.warning(self, tr("Application"), - tr("Cannot read file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())) + QMessageBox.warning(self, tr("Application"), tr("Cannot read file " + "{}:\n{}.".format(fileName, file.errorString()))) return in = QTextStream(file) @@ -309,8 +307,8 @@ def loadFile(self, fileName): textEdit.setPlainText(in.readAll()) QApplication.restoreOverrideCursor() - setCurrentFile(fileName) - statusBar().showMessage(tr("File loaded"), 2000) + self.setCurrentFile(fileName) + self.statusBar().showMessage(tr("File loaded"), 2000) //! [43] |
