PySideSimplicissimus Module 3 AboutBox 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 [French]

About Box

このダイアログボックスは、About Box自身のバージョン、使用中の QtPySidePython の各バージョン、及びプログラムが実行中の プラットフォーム を同時に表示します。

最初の一歩は Qt Designer を使ってメインウインドウを視覚的に設計します。プッシュボタンをひとつ追加しましょう。Saving Asを使って、ファイルにabout.ui (拡張子uiは Qt Designer が追加します)と名前をつけます。

私のホームページに about.ui を用意しました 。以下のURLからダウンロード可能です。 Qt Designer で開いて、今の段階でGUIがどのように見えるか確認しましょう。 about.py のソースも同様にダウンロード可能です。実際のところ、コードをディスプレイからクリップボードへコピーしてエディタに貼りつけるよりも、オリジナルのソースコードをダウンロードするほうがより安全です。ちなみに私は、絶対お薦めのあるエディタを使ってPythonプログラミングをしています。PythonとQtで書かれた Eric4 です。

クリックで about.ui をダウンロードして、時間のあるときに Qt Designer で見てください。

about.ui はXMLファイルです。でも読めないからといって気にしないように ー Pythonだって読むことができないんですから…Pythonが読めるようにするには、pyside-toolsパッケージのスクリプトを使って処理をする必要があります:

pyside-uic about.ui > ui_about.py

これでPythonの読み込み可能なファイル ui_about.py がプログラムでインポートして使えるようになりました。次のコードリストをご覧ください。

#!/usr/bin/env python
# about.py - display about box with info on platform etc.

import sys
import platform

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

from ui_about import Ui_MainWindow

''version'' = '0.0.1'

class MainWindow(QMainWindow, Ui_MainWindow):
 def ''init''(self, parent=None):
 super(MainWindow, self).''init''(parent)
 self.setupUi(self)
 self.aboutButton.clicked.connect(self.about)

def about(self):
 '''Popup a box with about message.'''
 QMessageBox.about(self, "About PySide, Platform and the like",
 """<b>Platform Details</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 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_()

メインプログラムは最後にありますが、これはよくやるパターンです。アプリケーション app を呼び出して、 sys.argv のすべてのコマンドライン引数を渡します。 window frame の名前は frame ですが他の名前でもかまいません。 frame.show() は、アプリケーションサイクルが実行を開始した直後に frame を表示するようシステムに通知します。最後に app.exec_() でアプリケーションサイクルを開始します。アプリケーションサイクルは、ボタンのクリックなど、ユーザーとの対話を可能にします。

aboutダイアログのポップアップボックス内のテキスト内容に関するコードの注意がひとつ:文字 copy; はHTML表現で解釈されると、円で囲まれた小さなc(著作権マーク)になります。実際にプログラムでは前の文に示したように記述する必要があります。

この about.py は他のPythonプログラムと同じように実行できます。是非ためしてください!