PySideSimplicissimus Module 6 AlternativeCombine Japanese

From Qt Wiki
Revision as of 17:31, 12 March 2015 by AutoSpider (talk | contribs) (Decode HTML entity names)
Jump to navigation Jump to search
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 [French]

組み合わせの代案

この例題のプログラム名はcombine_alter.pyです。このプログラムはcombine.pyとほとんど同じ動作をしますが、わずかに違いがあります−多重継承を回避しているのです。多重継承はPythonの素晴らしい特徴ですが、多くのプログラマー、特に他の言語から移行した人々は、多重継承を使いたがりません。というのもPythonは多重継承をサポートする数少ない言語だからです(例えばC++は多重継承をサポートしていますが、javaやC#などの一般的なオブジェクト指向言語ではサポートされていません)。私達は最初に多重継承を使う方法を学びましたが、実はこのように多重継承には好き嫌いがあるのです。プログラム名をcombine.pyに"alter"を追加したcombine_alter.pyとして、combine.pyとの違いをはっきりさせましょう。combine.uiとqrc_combine.pyには変更がないのでファイルのアップロードに頭を悩まされることはありません。そのまま使いましょう。プログラムはいつもの場所から入手できます。

combine_alter.pyとcombine.pyとの違いはそれほど大きくありません。ui_combineのimport文を変更していますが、これは純粋に美しさのためです。ローカル変数uiが初期化構文の冒頭で導入されています。ご覧のようにuiはinit関数以外にも使用するので、クラス変数にも格納します。Pythonプログラマーにとってはおなじみだと思いますが、init関数の引数はローカル変数として受けとり、速やかにクラス変数へ格納します。

#!/usr/bin/env python
# combine_alter.py - combination of ShowGPL, About, Close scripts

import sys
import platform

import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton, QMessageBox, QIcon

version''_ = '0.0.0'
from ui_combine import Ui_MainWindow as Ui
import qrc_combine

class MainWindow(QMainWindow):
 def ''init''(self, parent=None):
 super(MainWindow, self).''init''(parent)
 ui = Ui()
 # Store ui as class variable self.ui
 self.ui = ui
 ui.setupUi(self)
 ui.actionShow_GPL.triggered.connect(self.showGPL)
 ui.action_About.triggered.connect(self.about)
 iconToolBar = self.addToolBar("iconBar.png")
#——————————————————
# Add icons to appear in tool bar - step 1
 ui.actionShow_GPL.setIcon(QIcon(":/showgpl.png"))
 ui.action_About.setIcon(QIcon(":/about.png"))
 ui.action_Close.setIcon(QIcon(":/quit.png"))
#——————————————————
# Show a tip on the Status Bar - step 2
 ui.actionShow_GPL.setStatusTip("Show GPL Licence")
 ui.action_About.setStatusTip("Pop up the About dialog.")
 ui.action_Close.setStatusTip("Close the program.")
#——————————————————
 iconToolBar.addAction(ui.actionShow_GPL)
 iconToolBar.addAction(ui.action_About)
 iconToolBar.addAction(ui.action_Close)

def showGPL(self):
 '''Read and display GPL licence.'''
 self.ui.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; 2010 Joe Bloggs.
 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()
 app.exec_()

多重継承を使うか使わないか、どちらの方法を選ぶのかは基本的には好みの問題です。いずれにしても決めるのは簡単ですが、将来出会ったときのために、両方の方法についていくらかでも理解しておくと良いでしょう。

基本的なプログラム構成のわずかな表現の違いをお楽しみください!