1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

QLineEdit にアクションボタンを追加する

Last updated at Posted at 2025-03-09

前書き

ファイルダイアログでファイルパスを取得した後QLineEditにsetするUIを作ろうとして、以前はそれぞれ別のボタンとQLineEditを隣に並べていましたが、QLineEditにボタンを埋め込み単一のウィジェットとする手段を知ったので記事にしてみました。

イメージ

image.png
上がQLineEditとボタンを横に並べたもの
下が本題のQLineEditにボタンを埋め込みしたものです

上はウィジェットが二つになっているので、新たな水平レイアウト追加するなど手間があり少々面倒です。
下は単一のウィジェットになっているのでレイアウトが簡単です。

本題

QLineEditのメソッドaddActionを使用します。
addActionを使うには、アクションとアクションを追加する位置を渡します。
これで追加されるボタンはアイコンのみ表示されるのでアイコンを設定する必要があります。
LeadingPositionで入力の前の位置に追加し、TrailingPositionで後ろの位置に追加します。

サンプル
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QLineEdit

lineedit = QLineEdit()
lineedit.addAction(
    QIcon.fromTheme(QIcon.ThemeIcon.FolderIcon),
    QLineEdit.ActionPosition.TrailingPosition,
)

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?