PySide/ja: Difference between revisions

From FreeCAD Documentation
No edit summary
(Created page with "PySideは次の3ページで説明されていますが、それらは相互に従う必要があります。")
Line 72: Line 72:
[[File:PySideScreenSnapshot2.jpg]]
[[File:PySideScreenSnapshot2.jpg]]


PySideは次の3ページで説明されていますが、それらは相互に従う必要があります。
PySide is described in the following 3 pages which should follow on one from each other:


* [[PySide_Beginner_Examples|Beginner PySide Examples]] (Hello World, announcements, enter text, enter number)
* [[PySide_Beginner_Examples|Beginner PySide Examples]] (Hello World, announcements, enter text, enter number)

Revision as of 12:36, 9 January 2019

PySide

PySideはクロスプラットフォームのGUIツールキットQtのPythonバインディングです。 FreeCADはPython内部のすべてのGUI(Graphic User Interface)目的にPySideを使用します。 PySideは、以前にFreeCADによってGUIに使用されていたPyQtパッケージに代わるものです。 PySideはより許容されるライセンスを持っています。 違いの詳細についてはPySideとPyQtの違いを参照してください。

FreeCAD内部からPyQtモジュールを使用することでFreeCADのインターフェイスを完全に制御することができます。例えば以下のことを行うことができます。

  • 独自のパネル、ウィジット、ツールバーの追加
  • 既存のパネルへの要素の追加や非表示
  • 全ての要素の間のコネクションに対する変更、リダイレクト、追加

PyQtには豊富なAPIドキュメントがあり、またネット上にはどの様にそれが動作するのか教えてくれるチュートリアルがたくさんあります。

もしFreeCADインターフェイスをいじりたいのであれば一番最初にやるべきことはFreeCADのメインウィンドウの参照を作成することです:

import sys
from PyQt4 import QtGui
app = QtGui.qApp
mw = app.activeWindow()

すると例えばインターフェイスの全てのウィジットをブラウジングすることなどができるようになります:

for child in mw.children():
   print 'widget name = ', child.objectName(), ', widget type = ', child

Qtインターフェイスのウィジットは普通、"コンテナ"ウィジット内に入れ子状態になっています。つまりメインウィンドウの子ウィンドウ自体も他の子ウィンドウを収納できるのです。ウィジットのタイプによって行えることは様々です。何ができるのかについてはAPIドキュメントをチェックしてください。

新しいウィジットの追加には例えばdockWidget(FreeCADのサイドパネルの一つに配置できます)が便利です:

myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)

さらにいろいろなものをウィジットに直接追加できます:

myWidget.setObjectName("my Nice New Widget")
myWidget.resize(QtCore.QSize(300,100)) # ウィジットのサイズ設定
label = QtGui.QLabel("Hello World", myWidget) # ラベルを作成
label.setGeometry(QtCore.QRect(50,50,200,24)) # そのサイズを設定
label.setObjectName("myLabel") # 名前を設定することで名前を使って検索できるようにします

しかしよく好まれる方法は一度にウィジットの設定を全て行えるUIオブジェクトの作成です。この方法の大きな利点はQt Designerを使うことでこういったUIオブジェクトをグラフィカルな方法で作成できるということです。Qt Designerによって生成された標準的なオブジェクトは以下のようになります:

class myWidget_Ui(object):
  def setupUi(self, myWidget):
    myWidget.setObjectName("my Nice New Widget")
    myWidget.resize(QtCore.QSize(300,100).expandedTo(myWidget.minimumSizeHint())) # ウィジットのサイズを設定

    self.label = QtGui.QLabel(myWidget) # ラベルを作成
    self.label.setGeometry(QtCore.QRect(50,50,200,24)) # そのサイズを設定
    self.label.setObjectName("label") # 名前を設定することで名前を使って検索できるようにします

  def retranslateUi(self, draftToolbar): # ウィジットの翻訳を管理する組み込みのQt関数
    myWidget.setWindowTitle(QtGui.QApplication.translate("myWidget", "My Widget", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("myWidget", "Welcome to my new widget!", None, QtGui.QApplication.UnicodeUTF8))

使用するには以下のようにして新しく作成したウィジットに適用するだけで構いません:

myNewFreeCADWidget = QtGui.QDockWidget() # 新しいウィジットを作成
myNewFreeCADWidget.ui = myWidget_Ui() # UIスクリプトをロード
myNewFreeCADWidget.ui.setupUi(myNewFreeCADWidget) # UIを設定
FCmw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myNewFreeCADWidget) # メインウィンドウにウィジットを追加
print 'Hello World'

With Python's print statement you have only limited control of the appearance and behaviour. PySide supplies the missing control and also handles environments (such as the FreeCAD macro file environment) where the built-in facilities of Python are not enough.

PySide's abilities range from:

to:

PySideは次の3ページで説明されていますが、それらは相互に従う必要があります。

They divide the subject matter into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has overview and background material giving a description of PySide and how it is put together while the second and third pages are mostly code examples at different levels.

The intention is that the associated pages will provide simple Python code to run PySide so that the user working on a problem can easily copy the code, paste it into their own work, adapt it as necessary and return to their problem solving with FreeCAD. Hopefully they don't have to go chasing off across the internet looking for answers to PySide questions. At the same time this page is not intended to replace the various comprehensive PySide tutorials and reference sites available on the web.

Pivy
Scripted objects