PySideSimplicissimus Module 7 CombineAllIn1 Japanese

From Qt Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

日本語 English

すべてをひとつに組み合わせる

これまでいろいろな設計をためしてきた combine.py の最後のバージョンです。このバージョンでは combine.ui とその派生物である ui_combine.py ファイルを取り除きます。この方法の明確な利点は、ずばりファイルが少ないことです。欠点は、いざ後から見た目を変更しようと思ったときに、手戻りがいらないというQtDesignerの利点がもう使えないことです。基礎的なプログラムの単純なGUIであれば、この方法を使う価値があるかもしれません。ともあれ、私の愛読するGUI設計の本ではこのフォームを使用しており、ビジュアルな支援のいらない簡単にフォームを設計する方法として勧めています。

コードやその他のリソースはいつものように以下の "リポジトリ"からダウンロードできます。

プログラムの修正はまずui_combine_allin1.pyのsetupUi関数をプログラム本体にコピーすることから始めます。プログラム名は combine_allin1.py とします。次にコピーした構文をすべてメインプログラムの_init関数に貼り付けます。ここで少し変更をします。幸い、この変更はプログラムのプロセスの理解を確認するとても良い機会です。ついでに、インポートが必要なウィジェットを限定しましょう。そうすればもうコードのなかでQtCoreとQtGuiを明示的に指示する必要がなくなります。コードがすっきりしますね!

プログラムリストは次のとおりです。

#[[Image:/usr/bin/env python
# combine_allin1.py - combination of ShowGPL, About, Close scripts
# The purpose of this version of program is to show implementation 
# of most code in one file - all_in_1|]].

import sys
import platform

import PySide

from PySide.QtCore import QRect
from PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton, QMessageBox, QIcon, QAction, QWidget, QGridLayout, QTextEdit, QMenuBar, QMenu, QStatusBar

version''_ = '0.0.0'
import qrc_combine

class MainWindow(QMainWindow):
 def ''init''(self, parent=None):
 super(MainWindow, self).''init''(parent)
# self.setObjectName("MainWindow")
 self.resize(731, 475)
 centralwidget = QWidget(self)
# centralwidget.setObjectName("centralwidget")
 gridLayout = QGridLayout(centralwidget)
# gridLayout.setObjectName("gridLayout")
 # textEdit needs to be a class variable.
 self.textEdit = QTextEdit(centralwidget)
# self.textEdit.setObjectName("textEdit")
 gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
 self.setCentralWidget(centralwidget)
 menubar = QMenuBar(self)
 menubar.setGeometry(QRect(0, 0, 731, 29))
# menubar.setObjectName("menubar")
 menu_File = QMenu(menubar)
# menu_File.setObjectName("menu_File")
 self.setMenuBar(menubar)
 statusbar = QStatusBar(self)
# statusbar.setObjectName("statusbar")
 self.setStatusBar(statusbar)
 actionShow_GPL = QAction(self)
# actionShow_GPL.setObjectName("actionShow_GPL")
 actionShow_GPL.triggered.connect(self.showGPL)
 action_About = QAction(self)
# action_About.setObjectName("action_About")
 action_About.triggered.connect(self.about)
 iconToolBar = self.addToolBar("iconBar.png")
#——————————————————
# Add icons to appear in tool bar - step 1
 actionShow_GPL.setIcon(QIcon(":/showgpl.png"))
 action_About.setIcon(QIcon(":/about.png"))
 action_Close = QAction(self)
 action_Close.setCheckable(False)
 action_Close.setObjectName("action_Close")
 action_Close.setIcon(QIcon(":/quit.png"))
#——————————————————
# Show a tip on the Status Bar - step 2
 actionShow_GPL.setStatusTip("Show GPL Licence")
 action_About.setStatusTip("Pop up the About dialog.")
 action_Close.setStatusTip("Close the program.")
#——————————————————
 menu_File.addAction(actionShow_GPL)
 menu_File.addAction(action_About)
 menu_File.addAction(action_Close)
 menubar.addAction(menu_File.menuAction())

iconToolBar.addAction(actionShow_GPL)
 iconToolBar.addAction(action_About)
 iconToolBar.addAction(action_Close)
 action_Close.triggered.connect(self.close)

def showGPL(self):
 '''Read and display GPL licence.'''
 self.textEdit.setText(open('COPYING.txt').read())

def about(self):
 '''Popup a box with about message.'''
 QMessageBox.about(self, "About PyQt, Platform and the like",
 """<b> About this program </b> v %s
 <p>Copyright &copy; 2011 Your Name.
 All rights reserved in accordance with
 GPL v2 or later - NO WARRANTIES!
 <p>This application can be used for
 displaying OS and platform details.
 <p>Python %s - PySide version %s - Qt version %s on s"""  (''version'', platform.python_version(), PySide.''version'', PySide.QtCore.''version'', platform.system()))

if ''name'' == '''main''':
 app = QApplication(sys.argv)
 frame = MainWindow()
 frame.show()
 sys.exit(app.exec_())

特定の構文がそのまま残っていて、コメントアウトされていることに気づいたかと思います。コマンド setObjetName('name_string') を含む構文です。コメントアウトした理由は、このコマンドがプログラムで全く使用されないからです。使用しなくてもプログラムは同じように実行されます。しかし、削除はしません。というのも、これらはもともと Qt Desginer によって作成されたフォームを、 pyside-uic スクリプトを使って変換したものだからです。ですから、ひょっとするとある種のデバッグをする場合に役に立つかもしれません。それに単にコメントアウトするだけですので手間もかかりません(実際には何も費用はかかりません!)。いってみれば妥協の産物というわけです。

このプログラムは、我々が探求してきた combine.py の最後のバリエーションです。気は熟しました。そろそろシンプルなエンジニアリングの問題を探索をしてもいい頃合いです。そして、その基礎プログラム用のためのGUI設計を、PySideを使って行いましょう。この例題はトラスの力を求めるとても簡単なものです。例題で使うデータ処理メソッドは現実的なもので、なおかつ規模もちょうどよいですから、このチュートリアルにはうってつけです。では、またお会いしましょう。 Asta la vista!