<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「QTextEdit」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/qtextedit/feed" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 11 Jan 2026 05:22:07 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「QTextEdit」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【PyQt】画面開発で使えるQtWidgetsを紹介</title>
		<link>https://tech.nkhn37.net/pyqt-widgets-list/</link>
					<comments>https://tech.nkhn37.net/pyqt-widgets-list/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Tue, 22 Nov 2022 20:00:00 +0000</pubDate>
				<category><![CDATA[PyQt]]></category>
		<category><![CDATA[QCheckBox]]></category>
		<category><![CDATA[QComboBox]]></category>
		<category><![CDATA[QDateTimeEdit]]></category>
		<category><![CDATA[QLabel]]></category>
		<category><![CDATA[QLineEdit]]></category>
		<category><![CDATA[QPushButton]]></category>
		<category><![CDATA[QRadioButton]]></category>
		<category><![CDATA[QSpinBox]]></category>
		<category><![CDATA[QTextEdit]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=5902</guid>

					<description><![CDATA[PythonのGUIツールキットであるPyQtでの画面開発で使用できるQtWidgetsについて整理して紹介します。 PyQtの画面開発で使えるWidget PyQtでは、画面の構成要素として使用できるウィジェットがQt [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>PythonのGUIツールキットであるPyQtでの画面開発で使用できる<span class="marker"><strong>QtWidgets</strong></span>について整理して紹介します。</p>



<h2 class="wp-block-heading jinr-heading d--bold">PyQtの画面開発で使えるWidget</h2>



<p>PyQtでは、画面の構成要素として使用できるウィジェットが<span class="marker"><strong>QtWidgets</strong></span>に多く用意されています。本記事ではPyQtで使える画面構成要素を一つ一つ紹介していきます。</p>



<p>PyQtでの画面開発のテンプレートとしては「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/pyqt-qwidget-program-template/" target="_blank">QWidgetを継承した画面開発のテンプレート</a>」「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/pyqt-qmainwindow-program-template/" target="_blank">QMainWindowを継承した画面開発のテンプレート</a>」で紹介しています。</p>



<p>本記事では「QWidgetを継承した画面開発テンプレート」をベースに各種ウィジェットを配置する方法を紹介しています。今回紹介するようなWidgetを画面配置していくことで色々な画面を開発できるようになります。</p>



<p>なお、ウィジェットが使えるようになるとレイアウト配置したくなってくるかと思いますが、レイアウトについては「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/pyqt-widget-layout/" target="_blank">ウィジェットのレイアウト方法</a>」でもまとめていますので興味があれば参考にしてください。</p>



<h3 class="wp-block-heading jinr-heading d--bold">QLabel：ラベル</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>ラベルを表示するための「<span class="marker"><strong>QLabel</strong></span>」について紹介します。QLabelは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QLabel")
        self.resize(320, 240)

        # ===== QLabel 基本的な使い方
        label = qtw.QLabel("QLabel サンプル", self)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-100.png" alt="QLabel サンプル" class="wp-image-5909" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-100.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-100-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>ラベルは、QLabelに表示する文字列を渡すことで使用します。以降で紹介する各種Widgetも同様ですが、親画面が分かるようにselfを指定します。</p>



<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">テキストを後で設定する setText</h4>



<p>QLabelで表示する文字列については、<span class="marker"><strong>setText</strong></span>メソッドでQLabelを生成後に値を設定することができます。プログラムの処理の結果によりラベルを変更するとき等に使用できます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLabel テキストを後で設定する
        label = qtw.QLabel(self)
        label.setText("QLabel サンプル")</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-101.png" alt="QLabel サンプル setText" class="wp-image-5910" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-101.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-101-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">インデントを設定する indent</h4>



<p>QLabelで表示する文字列にインデントを設定する場合には、以下のように<span class="marker"><strong>indent</strong></span>引数に値を設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLabel インデントを設定する
        label = qtw.QLabel("QLabel サンプル", self, indent=50)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-102.png" alt="QLabel サンプル indent" class="wp-image-5911" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-102.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-102-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">マージンを設定する margin</h4>



<p>QLabelで表示する文字列にマージンを設定する場合には、以下のように<span class="marker"><strong>margin</strong></span>引数に値を設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLabel マージンを設定する
        label = qtw.QLabel("QLabel サンプル", self, margin=50)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-103.png" alt="QLabel サンプル margin" class="wp-image-5912" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-103.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-103-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">折り返しを設定する wordWrap=True</h4>



<p>QLabelで表示する文字列に折り返しの設定をする場合には、<span class="marker"><strong>wordWrap</strong></span>引数をTrueに設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLabel 折り返しを設定する
        label = qtw.QLabel("QLabel サンプル xxxxx yyyyy zzzzz", self, wordWrap=True)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-104.png" alt="QLabel サンプル wordWrap" class="wp-image-5913" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-104.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-104-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">リッチテキストを表示する </h4>



<p>QLabelは、リッチテキストを表示することも可能です。以下は、ボールド（太字）で文字列を表示している例です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLabel リッチテキストを表示
        label = qtw.QLabel("&lt;b>QLabel サンプル&lt;/b>", self)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-136.png" alt="QLabel サンプル リッチテキスト" class="wp-image-6028" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-136.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-136-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QLineEdit：エディット欄</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>利用者の入力欄（エディット欄）を表示するための「<span class="marker"><strong>QLineEdit</strong></span>」について紹介します。QLineEditは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QLineEdit")
        self.resize(320, 240)

        # ===== QLineEdit 基本的な使い方
        line_edit = qtw.QLineEdit(self)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-142.png" alt="QLineEdit サンプル" class="wp-image-6055" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-142.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-142-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>エディット欄のため利用者が値を入力することができます。入力された値を使ってプログラムのデータとして利用することが可能です。</p>



<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">デフォルト値を設定する</h4>



<p>QLineEditは、第1引数に文字列を渡すことで以下のようにデフォルト値として値を設定した状態で画面表示することが可能です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLineEdit デフォルト値を設定する
        line_edit = qtw.QLineEdit("デフォルト値", self)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-106.png" alt="QLineEdit サンプル デフォルト値" class="wp-image-5917" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-106.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-106-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">プレースホルダーテキストの表示 placeholderText</h4>



<p>入力欄（エディット欄）では、どういった入力をするべきか利用者が分かるようにプレースホルダーテキストを設定することがあります。プレースホルダーテキストとは、入力されるまでの間フィールドの内側に表示される説明文のことです。</p>



<p>QLineEditで、プレースホルダーテキストを設定したい場合は、以下のように<span class="marker"><strong>placeholderText</strong></span>引数に文字列を指定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLineEdit プレースホルダーテキストの表示
        line_edit = qtw.QLineEdit(self, placeholderText="入力してください。")</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-107.png" alt="QLineEdit サンプル placeholderText" class="wp-image-5918" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-107.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-107-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">クリアボタンの表示 clearButtonEnabled=True</h4>



<p>値が入力された際に簡単にクリアできるボタンを表示するには、以下のように<span class="marker"><strong>clearButtonEnabled</strong></span>引数をTrueを設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLineEdit クリアボタンの表示
        line_edit = qtw.QLineEdit(self, clearButtonEnabled=True)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-108.png" alt="QLineEdit サンプル clearButtonEnabled" class="wp-image-5919" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-108.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-108-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">最大文字数の指定 maxLength</h4>



<p>QLineEditで入力させる最大文字数を指定したい場合は、以下のように<span class="marker"><strong>maxLength</strong></span>引数に最大文字数を指定します。これにより、最大指定文字数以上は入力しようと思っても入力できなくなります。なお、文字数のため全角の日本語でも指定文字数分を入力できます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QLineEdit 最大長の指定
        line_edit = qtw.QLineEdit(self, maxLength=10)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-109.png" alt="QLineEdit サンプル maxLength" class="wp-image-5920" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-109.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-109-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QTextEdit：テキストエディット欄</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>利用者のテキスト入力欄を表示するための「<span class="marker"><strong>QTextEdit</strong></span>」について紹介します。QTextEditは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QTextEdit")
        self.resize(320, 240)

        # ===== QTextEdit 基本的な使い方
        textedit = qtw.QTextEdit(self)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-110.png" alt="QTextEdit サンプル" class="wp-image-5973" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-110.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-110-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>QLineEditが1行であったのに対して、QTextEditはメモ帳などのように改行したりしてテキストを入力できます。</p>



<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">プレースホルダーテキストの表示 placeholderText</h4>



<p>テキスト欄では、どういった入力をするべきか利用者が分かるようにプレースホルダーテキストを設定することがあります。プレースホルダーテキストとは、入力されるまでの間フィールドの内側に表示される説明文のことです。</p>



<p>QTextEditで、プレースホルダーテキストを設定したい場合は、以下のように<span class="marker"><strong>placeholderText</strong></span>引数に文字列を指定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QTextEdit プレースホルダーテキストの表示
        textedit = qtw.QTextEdit(self, placeholderText="テキストを入力してください。")</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-112.png" alt="QTextEdit サンプル placeholderText" class="wp-image-5976" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-112.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-112-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">折り返し設定 lineWrapMode, lineWrapColumnOrWidth</h4>



<p>QTextEditで折り返し設定を指定したい場合は、以下のように<span class="marker"><strong>lineWrapMode</strong></span>引数にqtw.QTextEdit.LineWrapMode.FixedColumnWidthを設定し、<span class="marker"><strong>lineWrapColumnOrWidt</strong></span>h引数に文字数を設定します。なお、文字数のため全角の日本語でも指定文字数分で折り返しされます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QTextEdit 折り返し設定
        textedit = qtw.QTextEdit(
            self,
            lineWrapMode=qtw.QTextEdit.LineWrapMode.FixedColumnWidth,
            lineWrapColumnOrWidth=20,
        )</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-115.png" alt="QTextEdit サンプル lineWrapMode lineWrapColumnOrWidth" class="wp-image-5982" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-115.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-115-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">テキストを入力した状態で表示 plainText</h4>



<p>QTextEditで、テキストを入力した状態で表示したい場合は、以下のように<span class="marker"><strong>plainText</strong></span>引数に文字列を渡します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QTextEdit テキストを入力した状態で表示
        textedit = qtw.QTextEdit(self, plainText="テキスト")</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-111.png" alt="QTextEdit サンプル plainText" class="wp-image-5974" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-111.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-111-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">リッチテキストを表示 html</h4>



<p>QTextEditで、リッチテキストを入力した状態で表示したい場合は、以下のように<span class="marker"><strong>html</strong></span>引数に文字列を渡します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QTextEdit リッチテキストを表示
        textedit = qtw.QTextEdit(self, html="&lt;b>テキスト&lt;/b>")</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-113.png" alt="QTextEdit サンプル html" class="wp-image-5978" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-113.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-113-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">リッチテキストを許可しない acceptRichText=False</h4>



<p>QTextEditでリッチテキストを許可しないようにするには、<span class="marker"><strong>acceptRichText</strong></span>引数をFalseに設定します。この設定でリッチテキストをコピペしようとするとプレーンテキストに変換されてペーストされます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QTextEdit Rich Textを許可しない
        textedit = qtw.QTextEdit(self, acceptRichText=False)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-114.png" alt="QTextEdit サンプル acceptRichText=False" class="wp-image-5980" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-114.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-114-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QPushButton：ボタン</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>ボタンを表示するための「<span class="marker"><strong>QPushButton</strong></span>」について紹介します。QPushButtonは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QPushButton")
        self.resize(320, 240)

        # ===== QPushButton 一般的な使い方
        push_button = qtw.QPushButton("ボタン名", self)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-116.png" alt="QPushButton サンプル" class="wp-image-5985" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-116.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-116-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>QPushButtonは、機能の実行や取消など様々なトリガーとして使用することができます。</p>



<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">ショートカットキーの設定 shortcut</h4>



<p>QPushButtonにショートカットを設定するには、以下のように<span class="marker"><strong>shortcut</strong></span>引数にqtg.QKeySequence(&#8220;Ctrl+P&#8221;)のように指定します。このようにしておくことで、マウスでクリックしなくてもCtrl+Pを押すとボタンがクリックできるようになります。もちろん文字列の部分を変えれば別のキーをショートカットにすることも可能です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QPushButton ショートカットキーの設定
        push_button = qtw.QPushButton("クリック", self, shortcut=qtg.QKeySequence("Ctrl+P"))</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-12.png" alt="QPushButton サンプル shortcut" class="wp-image-7079" srcset="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-12.png 322w, https://tech.nkhn37.net/wp-content/uploads/2023/02/image-12-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">ボタン押下した時に押下状態を保持 checkable=True</h4>



<p>QPushButtonで、ボタンを押したときにボタンを押した状態のままにしたい場合には、<span class="marker"><strong>checkable</strong></span>引数をTrueに設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QPushButton ボタン押下した時に押下状態を保持
        push_button = qtw.QPushButton("クリック", self, checkable=True)</pre>



<p>【表示結果】</p>



<div class="wp-block-columns js--scr-animation is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-10.png" alt="QPushButton サンプル checkable=True" class="wp-image-7076" srcset="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-10.png 322w, https://tech.nkhn37.net/wp-content/uploads/2023/02/image-10-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div></div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-11.png" alt="QPushButton サンプル checkable=True" class="wp-image-7077" srcset="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-11.png 322w, https://tech.nkhn37.net/wp-content/uploads/2023/02/image-11-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div></div>
</div>



<h4 class="wp-block-heading jinr-heading d--bold">ボタン押下した時に押下状態を保持(チェック状態で表示) checkable=True, checked=True</h4>



<p>QPushButtonで、チェック状態で表示して押下状態を保持したい場合には、以下のように<span class="marker"><strong>checkable</strong></span>引数と<span class="marker"><strong>checked</strong></span>引数をTrueに設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QPushButton ボタン押下した時に押下状態を保持（チェック状態で表示）
        push_button = qtw.QPushButton("クリック", self, checkable=True, checked=True)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-13.png" alt="QPushButton サンプル checkable=True, checkable=True" class="wp-image-7081" srcset="https://tech.nkhn37.net/wp-content/uploads/2023/02/image-13.png 322w, https://tech.nkhn37.net/wp-content/uploads/2023/02/image-13-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QCheckBox：チェックボックス</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>チェックボックスを表示するための「<span class="marker"><strong>QCheckBox</strong></span>」について紹介します。QCheckBoxは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCheckBox")
        self.resize(320, 240)

        # ===== QCheckBox 基本的な使い方
        checkbox = qtw.QCheckBox("チェック", self)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-121.png" alt="QCheckBox サンプル" class="wp-image-5995" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-121.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-121-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">ショートカットキーの設定 shortcut</h4>



<p>QCheckBoxにショートカットを設定するには、以下のように<span class="marker"><strong>shortcut</strong></span>引数にqtg.QKeySequence(&#8220;Ctrl+P&#8221;)のように指定します。このようにしておくことで、マウスでクリックしなくてもCtrl+Pを押すとチェックを入れたり外したりできるようになります。もちろん文字列の部分を変えれば別のキーをショートカットにすることも可能です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QCheckBox ショートカットキーの設定
        checkbox = qtw.QCheckBox("チェック", self, shortcut=qtg.QKeySequence("Ctrl+P"))</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-122.png" alt="QCheckBox サンプル shortcut" class="wp-image-5997" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-122.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-122-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">チェック状態で表示 checked</h4>



<p>QCheckBoxでチェック状態で表示したい場合は、以下のように<span class="marker"><strong>checked</strong></span>引数をTrueに設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QCheckBox チェック状態で表示
        checkbox = qtw.QCheckBox("チェック", self, checked=True)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-123.png" alt="QCheckBox サンプル checked" class="wp-image-5999" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-123.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-123-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QRadioButton：ラジオボタン</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>ラジオボタンを表示するための「<span class="marker"><strong>QRadioButton</strong></span>」について紹介します。QRadioButtonは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QRadioButton")
        self.resize(320, 240)

        # ===== QRadioButton 基本的な使い方
        radio_button = qtw.QRadioButton("チェック", self)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-124.png" alt="QRadioButton サンプル" class="wp-image-6001" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-124.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-124-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">ショートカットキーの設定 shortcut</h4>



<p>QRadioButtonにショートカットを設定するには、以下のように<span class="marker"><strong>shortcut</strong></span>引数にqtg.QKeySequence(&#8220;Ctrl+P&#8221;)のように指定します。このようにしておくことで、マウスでクリックしなくてもCtrl+Pを押すとチェックを入れたり外したりできるようになります。もちろん文字列の部分を変えれば別のキーをショートカットにすることも可能です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QRadioButton ショートカットキーの設定
        radio_button = qtw.QRadioButton("チェック", self, shortcut=qtg.QKeySequence("Ctrl+P"))</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-125.png" alt="QRadioButton サンプル shortcut" class="wp-image-6003" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-125.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-125-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold">チェック状態で表示 checked</h4>



<p>QRadioButtonでチェック状態で表示したい場合は、以下のように<span class="marker"><strong>checked</strong></span>引数をTrueに設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QRadioButton チェック状態で表示
        radio_button = qtw.QRadioButton("チェック", self, checked=True)</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-126.png" alt="QRadioButton サンプル checked" class="wp-image-6005" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-126.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-126-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QComboBox：コンボボックス</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>コンボボックスを表示するための「<span class="marker"><strong>QComboBox</strong></span>」について紹介します。QComboBoxは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QComboBox")
        self.resize(320, 240)

        # ===== QComboBox 基本的な使い方
        combobox = qtw.QComboBox(self)
        combobox.addItem("item 1", 1)
        combobox.addItem("item 2", "text")
        combobox.addItem("item 3", qtw.QWidget)
        combobox.insertItem(1, "item 1.5", 2)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>



<div class="wp-block-columns js--scr-animation is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-127.png" alt="QComboBox サンプル" class="wp-image-6007" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-127.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-127-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /><figcaption class="wp-element-caption">初期表示</figcaption></figure>
</div></div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-128.png" alt="QComboBox サンプル" class="wp-image-6008" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-128.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-128-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /><figcaption class="wp-element-caption">コンボボックス表示</figcaption></figure>
</div></div>
</div>



<p>コンボボックスに表示する項目は<span class="marker"><strong>addItem</strong></span>メソッドで追加ができます。また、位置を指定して項目を挿入したい場合は<span class="marker"><strong>insertItem</strong></span>メソッドを使用します。</p>



<p>なお、各メソッドでは項目名と値を設定しています。値を設定しておくことで、選択された項目に合わせた値を取得してプログラムで使用することができます。以下のようにWidgetを設定したりできる点も面白い点です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        combobox.addItem("item 3", qtw.QWidget)</pre>



<h3 class="wp-block-heading jinr-heading d--bold">QSpinBox：スピンボックス</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>スピンボックスを表示するための「<span class="marker"><strong>QSpinBox</strong></span>」について紹介します。QSpinBoxは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QSpinBox")
        self.resize(320, 240)

        # ===== QSpinBox 基本的な使い方
        spinbox = qtw.QSpinBox(self, value=50, minimum=0, maximum=100)

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-129.png" alt="QSpinBox サンプル" class="wp-image-6011" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-129.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-129-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>QSpinBoxは、上記のように値を上下のボタンで増やしたり減らしたりできます。また、直接編集して値を入力することも可能です。また、<span class="marker"><strong>minumum</strong></span>引数と<span class="marker"><strong>maximum</strong></span>引数で値の範囲を制限することができます。</p>



<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">ステップ数を設定 singleStep</h4>



<p>QSpinBoxで、値のステップ数を設定したい場合は、以下のように<span class="marker"><strong>singleStep</strong></span>引数にステップ数を設定します。以下の例では、上下のボタンを押すごとに5ずつ値が変化します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QSpinBox ステップ数を設定
        spinbox = qtw.QSpinBox(self, value=50, minimum=0, maximum=100, singleStep=5)</pre>



<p>【表示結果】</p>



<div class="wp-block-columns js--scr-animation is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-131.png" alt="QSpinBox サンプル singleStep" class="wp-image-6014" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-131.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-131-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /><figcaption class="wp-element-caption">初期表示</figcaption></figure>
</div></div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-132.png" alt="QSpinBox サンプル singleStep" class="wp-image-6015" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-132.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-132-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /><figcaption class="wp-element-caption">↑を1回クリックした時</figcaption></figure>
</div>
</div>



<h4 class="wp-block-heading jinr-heading d--bold">プレフィックスとサフィックスの表示  prefix, suffix</h4>



<p>QSpinBoxで、値の前につくプレフィックスと後につくサフィックスを表示したい場合には、以下のように<span class="marker"><strong>prefix</strong></span>引数や<span class="marker"><strong>suffix</strong></span>引数に文字列を指定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QSpinBox プレフィックスとサフィックスを表示
        spinbox = qtw.QSpinBox(
            self, value=50, minimum=0, maximum=100, prefix="¥", suffix="+ 税"
        )</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-134.png" alt="QSpinBox サンプル prefix suffix" class="wp-image-6020" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-134.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-134-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold">QDateTimeEdit：日付エディット</h3>



<h4 class="wp-block-heading jinr-heading d--bold">基本的な使い方</h4>



<p>日付エディット欄を表示するための「<span class="marker"><strong>QDateTimeEdit</strong></span>」について紹介します。QDateTimeEditは、基本的には以下のように使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys

from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
from PyQt6 import QtWidgets as qtw


class MainWindow(qtw.QWidget):
    """メインウィンドウ"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("QDateTimeEdit")
        self.resize(320, 240)

        # ===== QDateTimeEdit 基本的な使い方
        datetimebox = qtw.QDateTimeEdit(
            self, date=qtc.QDate.currentDate(), time=qtc.QTime(0, 0)
        )

        # 画面表示
        self.show()


def main():
    """メイン関数"""
    app = qtw.QApplication(sys.argv)
    mv = MainWindow()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-137.png" alt="QDateTimeEdit サンプル" class="wp-image-6034" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-137.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-137-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>上記のように日付エディット欄が表示され、時間情報を入力することができます。初期表示の値は<span class="marker"><strong>date</strong></span>引数や<span class="marker"><strong>time</strong></span>引数に、qtc.QDateやqtc.QTimeを使って時間を日付や時刻を設定することができます。また、qtc.QDate.currentDate()やqtc.QTime.currentTime()を使うことで現在日付や時刻を取得することも可能です。</p>



<p>以降での使い方のバリエーションを紹介します。実行は上記プログラムをベースとして、定義部分を置き換えて使ってみてください。なお、以降で紹介する引数は一つ一つ紹介しますが、複数組み合わせで指定して使用可能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold">日時フォーマットの指定 displayFormat</h4>



<p>QDateTimeEditで、日付フォーマットを指定したい場合は、以下のように<span class="marker"><strong>displayFormat</strong></span>引数にフォーマットを表す文字列を指定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QDateTimeEdit 日時フォーマットの指定
        datetimebox = qtw.QDateTimeEdit(
            self,
            date=qtc.QDate.currentDate(),
            time=qtc.QTime(0, 0),
            displayFormat="yyyy-MM-dd HH:mm:ss",
        )</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-141.png" alt="QDateTimeEdit サンプル dsplayFormat" class="wp-image-6042" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-141.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-141-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<p>なお、日付・時刻のフォーマットとしては以下のようなものが使用できます。</p>



<figure class="wp-block-table"><table><thead><tr><th>フォーマット文字</th><th>内容</th></tr></thead><tbody><tr><td>d</td><td>日</td></tr><tr><td>M</td><td>月</td></tr><tr><td>yy</td><td>年（2桁）</td></tr><tr><td>yyyy</td><td>年（4桁）</td></tr><tr><td>H</td><td>時間</td></tr><tr><td>m</td><td>分</td></tr><tr><td>s</td><td>秒</td></tr></tbody></table></figure>



<h4 class="wp-block-heading jinr-heading d--bold">カレンダーのポップアップを設定 calendarPopup=True</h4>



<p>QDateTimeEditで、カレンダーで入力するためのポップアップを表示できるようにするには、<span class="marker"><strong>calendarPopup</strong></span>引数をTrueに設定します。以下のように、カレンダーを表示して利用者に入力してもらうことが可能になります。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QDateTimeEdit カレンダーのポップアップを設定
        datetimebox = qtw.QDateTimeEdit(
            self, date=qtc.QDate.currentDate(), time=qtc.QTime(0, 0), calendarPopup=True
        )</pre>



<p>【表示結果】</p>



<div class="wp-block-columns js--scr-animation is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-138.png" alt="QDateTimeEdit サンプル calendarPopup" class="wp-image-6037" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-138.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-138-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /><figcaption class="wp-element-caption">初期表示</figcaption></figure>
</div></div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"><div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-139.png" alt="QDateTimeEdit サンプル calendarPopup" class="wp-image-6038" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-139.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-139-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /><figcaption class="wp-element-caption">カレンダーポップアップ</figcaption></figure>
</div></div>
</div>



<h4 class="wp-block-heading jinr-heading d--bold">期間の最大・最小を設定 minimumDate, maximumDate</h4>



<p>QDateTimeEditで、指定できる期間の最大、最少を設定するには、<span class="marker"><strong>minimumDate</strong></span>引数や<span class="marker"><strong>maximumDate</strong></span>引数に日付情報を設定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        # ===== QDateTimeEdit 期間の最大・最小を設定
        datetimebox = qtw.QDateTimeEdit(
            self,
            date=qtc.QDate.currentDate(),
            time=qtc.QTime(0, 0),
            calendarPopup=True,
            minimumDate=qtc.QDate(2000, 1, 1),
            maximumDate=qtc.QDate(2030, 12, 31),
        )</pre>



<p>【表示結果】</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="322" height="272" src="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-140.png" alt="QDateTimeEdit サンプル minimumDate maximumDate" class="wp-image-6041" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/11/image-140.png 322w, https://tech.nkhn37.net/wp-content/uploads/2022/11/image-140-300x253.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>
</div>


<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p>PythonのGUIツールキットであるPyQtでの画面開発で使用できる<span class="marker"><strong>QtWidgets</strong></span>について整理して紹介しました。Qtで提供されているすべてを網羅できているわけではありませんが、中心的なWidgetについて多く紹介しました。</p>



<p>PyQtを使った画面開発際の参考としていただければよいかなと思います。</p>



<section class="wp-block-jinr-blocks-simplebox b--jinr-block-container"><div class="b--jinr-block b--jinr-box d--heading-box8  "><div class="a--simple-box-title d--bold">ソースコード</div><div class="c--simple-box-inner">
<p>上記で紹介しているソースコードについては <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-libraries/pyqt/qtwidgets-basics" target="_blank" rel="noreferrer noopener">GitHub</a> にて公開しています。参考にしていただければと思います。</p>
</div></div></section>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/pyqt-widgets-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-04-07 18:03:50 by W3 Total Cache
-->