ボタンとフォーカスフレーム(QPushButton と QFocusFrame)
QPushButton はよく使われるボタンコントロールで、さまざまな動作をトリガーします。QFocusFrame は、フォーカスを表示するために使用されるコントロールで、通常 QWidget の周りにフォーカス枠を表示するために使用されます。
QPushButtonの一般的なメソッドとシグナル:
- setText():ボタンのテキスト内容を設定します。
- setIcon():ボタンにアイコンを設定する。
- setCheckable(): ボタンがチェック可能かどうかを設定します。
- isChecked():ボタンが選択されているかどうかを返します。
- クリックされました:ボタンがクリックされた時に送られるシグナル
QFocusFrame のメジャーメソッドとシグナル。
- setWidget():フォーカス枠に関連付けられたQWidgetを設定する。
- setFrameShadow():フォーカス枠の影のスタイルを設定します。
- focusInEvent():フォーカスによる枠獲得時に発生するイベント。
- フォーカスアウトイベント:フォーカスが枠外に移動した際に発生するイベント。
QPushButton および QFocusFrame を使用したサンプルコードを以下に記載します。
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFocusFrame
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Button and Focus Frame Example")
self.button = QPushButton("Click me", self)
self.button.setGeometry(50, 50, 100, 50)
self.button.clicked.connect(self.buttonClicked)
self.focusFrame = QFocusFrame(self)
self.focusFrame.setWidget(self.button)
self.focusFrame.setGeometry(45, 45, 110, 60)
self.focusFrame.focusInEvent = self.focusInEvent
self.focusFrame.focusOutEvent = self.focusOutEvent
def buttonClicked(self):
print("Button clicked")
def focusInEvent(self, event):
print("Focus in")
def focusOutEvent(self, event):
print("Focus out")
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
このサンプルでは、QPushButtonとQFocusFrameを含むメインウィンドウが作成されます。ボタンをクリックすると、「ボタンがクリックされました」と表示されます。フォーカスフレームにフォーカスが当たったり外れたりすると、「フォーカスイン」または「フォーカスアウト」と表示されます。