PySide リファレンス

QSS

ウィンドウ

スタンドアロン

python 2系 / 3系 互換 ウィンドウ

python 2系 / 3系 互換 フラットウィンドウ

ウィンドウを画面中央へ移動させる

def center_window(self):
    """

    ウィンドウをセンターへ移動

    """
    centerPoint = QtGui.QScreen.availableGeometry(QtWidgets.QApplication.primaryScreen()).center()
    fg = self.frameGeometry()
    fg.moveCenter(centerPoint)
    self.move(fg.topLeft())
#
#   サイズと位置調整
#

width = 900
height = 500

# ディスプレイサイズ
app = QtWidgets.QApplication
geometry = app.primaryScreen().geometry()
frame_size = self.frameSize()

self.resize(width, height)
self.move(geometry.width() / 2 - frame_size.width() / 2, geometry.height() / 2 - frame_size.height() / 2)

QWidget

QWidget にスタイルシート貼れるようになるメソッド

下記をオーバーライドする。

def paintEvent(self, event):
    opt = QtWidgets.QStyleOption()
    opt.init(self)
    painter = QtGui.QPainter(self)
    style = self.style()
    style.drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)

QPushButton

アイコン画像のセット

self.setIcon(QtGui.QIcon(self.icon_path))
self.setIconSize(QtCore.QSize(200,200))

入力系ウィジェット

基本的には QLineEdit と QPlainTetxtEdit の使い方は同じ。一行だけか長文使えるかの違い。

文字入力

QLineEdit

self.lineEdit = QLinedit()

# テキスト変更時のシグナル
self.lineEdit.textChanged.connect(self.test)

# テキストのセット
self.lineEdit.setText(text)

# テキストの内容の取得
plainText = self.lineEdit.text()

# 初期化
self.lineEdit.clear()

QPlainTextEdit

self.plainTextEdit = QPlainTextEdit()

# テキスト変更時のシグナル
self.plainTextEdit.textChanged.connect(self.test)

# テキストのセット
self.plainTextEdit .setPlainText(text)

# テキストの内容の取得
plainText = self.plainTextEdit .toPlainText()

# 初期化
self.plainTextEdit.clear()

数値入力

QSpinBox

spinBox = QSpinBox()
spinBox.setMinimum(0) # 最小値
spinBox.setMaximum(10) # 最大値
spinBox.setSuffix("min") # 接尾辞を設定
spinBox.setSingleStep(10) # ステップ数

QDoubleSpinBox

# 小数点以下の桁数を設定
self.doubleSpinBox.setDecimals()

QSlider

modo10のスクリプトについて調べてみた その16

入力切替

QCheckBox

# チェックボックスの設置
self.hoge_CheckBox = QCheckBox('hoge')

# チェックボックスのオン/オフ
self.hoge_CheckBox.setChecked(True)
self.hoge_CheckBox.setChecked(False)


# 状態変更時 のシグナル
self.hoge_CheckBox.toggled.connect(self.slot)


# チェックボックスのオン/オフの取得
self.hoge_CheckBox.isChecked()

QRadioBox

# チェックが入っているID取得
hogeID = self.hoge_BtnGroup.checkedId()

# グループ化
self.group_A = QButtonGroup()
self.group_A.addButton(self.radio_a,1)
self.group_A.addButton(self.radio_b,2)

【PySide】横レイアウト/ラジオボタン/仕切り

QComboBox

self.comboBox = QComboBox()

# 複数入れる場合はリストをそのまま突っ込めば入る
self.comboBox.addItems(text_list) # コンボボックスに入力

# テキスト検索にして setInsertPolicy を設定しない場合、
# 入力された文字列がそのままコンボボックスに入る
self.comboBox.setEditable(True) # テキスト検索 ON
self.comboBox.setInsertPolicy(QComboBox.NoInsert) # テキスト追加 OFF

self.comboBox.currentIndex() # コンボボックスのインデックス取得

# 完全一致のアイテム名のインデックスを取得
self.comboBox.findText(current_combo_text, QtCore.Qt.MatchFixedString)


# シグナル
#======================================================================
self.comboBox.currentIndexChanged.connect(slot) # 変更時

# スロット
#======================================================================
self.comboBox.setCurrentIndex(index) # インデックスで選択項目指定

表示系ウィジェット

QLabel

画像を表示する

self.icon_path = 'D:\\hoge\\hoge.png'
self.icon_width  = 50
self.icon_height = 50

self.icon_Lbl = QtWidgets.QLabel()
self.icon_Lbl.setAlignment(Qt.AlignCenter) # 中央へ整列
pixmap = QtGui.QPixmap(self.icon_path)     # pixmap の作成
pixmap = pixmap.scaled(
    self.icon_width, self.icon_height,
    QtCore.Qt.KeepAspectRatio)             # アイコンのサイズ
self.icon_Lbl.setPixmap(pixmap)            # pixmap の設置
self.icon_Area.addWidget(self.icon_Lbl)    # ウィジェット の設置

QListWidget

QTableWidget

カラム幅について

ウィジェット

フォント

hoge.setFont(QtGui.QFont('游ゴシック'))
hoge.setFont(QtGui.QFont('Arial'))

整列

hoge.setAlignment(Qt.AlignCenter)
hoge.setAlignment(Qt.AlignRight)

その他

# レイアウトに設置されているウィジェットを削除
self.qlayout.removeWidget(self.widget)
del self.widget

# QWidget 直下のレイアウトの余白を削除
self.root_area.setContentsMargins(0, 0, 0, 0)