{"id":5056,"date":"2022-09-22T08:07:52","date_gmt":"2022-09-22T08:07:52","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5056"},"modified":"2022-10-14T03:41:41","modified_gmt":"2022-10-14T03:41:41","slug":"pyqt-qformlayout","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qformlayout\/","title":{"rendered":"PyQt QFormLayout"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PyQt <code>QFormLayout<\/code> to arrange widgets in a form.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qformlayout'>Introduction to the PyQt QFormLayout <a href=\"#introduction-to-the-pyqt-qformlayout\" class=\"anchor\" id=\"introduction-to-the-pyqt-qformlayout\" title=\"Anchor for Introduction to the PyQt QFormLayout\">#<\/a><\/h2>\n\n\n\n<p>When creating a data-entry form, you often need to place fields in rows. And on each row, you place a label next to an input widget.<\/p>\n\n\n\n<p>PyQt provides you with a convenient two-column form that arranges the widgets on a form. The left column has a label and the right column has an input widget.<\/p>\n\n\n\n<p>To create a form layout, you use <code>QFormLayout<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">layout = QFormLayout(self)\nself.setLayout(layout) <span class=\"hljs-comment\"># self is the parent widget<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Adding widgets to the form layout can be done with the <code>addRow()<\/code> method. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">layout.addRow(<span class=\"hljs-string\">'Field 1'<\/span>, input_widget1)\nlayout.addRow(<span class=\"hljs-string\">'Field 2'<\/span>, input_widget2)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>addRow()<\/code> method takes a string and a widget and automatically creates the <code>QLabel<\/code> widget for the string.<\/p>\n\n\n\n<p>If you pass a single widget such as a <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qlabel\/\">QLabel<\/a><\/code>, the widget will automatically span both columns. In practice, you can use this feature for creating headings or section labels.<\/p>\n\n\n\n<p>Besides providing conveniences, <code>QFormLayout<\/code> adheres to the platform&#8217;s look and feel guidelines. For example, when used on macOS labels are right-justified while when used on Windows, labels are left-justified.<\/p>\n\n\n\n<p>In addition, when displayed on a narrow screen, the layout automatically collapses to a single column with labels above the input widgets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qformlayout-example'>PyQt QFormLayout example <a href=\"#pyqt-qformlayout-example\" class=\"anchor\" id=\"pyqt-qformlayout-example\" title=\"Anchor for PyQt QFormLayout example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to create a signup form using the <code>QFormLayout<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QPushButton, QLineEdit,  QFormLayout\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowTitle(<span class=\"hljs-string\">'Sign Up Form'<\/span>)\n\n        layout = QFormLayout()\n        self.setLayout(layout)\n\n        layout.addRow(<span class=\"hljs-string\">'Name:'<\/span>, QLineEdit(self))\n        layout.addRow(<span class=\"hljs-string\">'Email:'<\/span>, QLineEdit(self))\n        layout.addRow(<span class=\"hljs-string\">'Password:'<\/span>, QLineEdit(self, echoMode=QLineEdit.EchoMode.Password))\n        layout.addRow(<span class=\"hljs-string\">'Confirm Password:'<\/span>, QLineEdit(self, echoMode=QLineEdit.EchoMode.Password))\n        layout.addRow(<span class=\"hljs-string\">'Phone:'<\/span>, QLineEdit(self))\n\n        layout.addRow(QPushButton(<span class=\"hljs-string\">'Sign Up'<\/span>))\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"389\" height=\"318\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QFormLayout.png\" alt=\"\" class=\"wp-image-5057\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QFormLayout.png 389w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QFormLayout-300x245.png 300w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use PyQt <code>QFormLayout<\/code> to create data-entry forms.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5056\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qformlayout\/\"\n\t\t\t\tdata-post-title=\"PyQt QFormLayout\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5056\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qformlayout\/\"\n\t\t\t\tdata-post-title=\"PyQt QFormLayout\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to efficiently use PyQt QFormLayout to arrange widgets in a data-entry form.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5056","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5056"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5056\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}