1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited. 7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 8## All rights reserved. 9## 10## This file is part of the examples of PyQt. 11## 12## $QT_BEGIN_LICENSE:BSD$ 13## You may use this file under the terms of the BSD license as follows: 14## 15## "Redistribution and use in source and binary forms, with or without 16## modification, are permitted provided that the following conditions are 17## met: 18## * Redistributions of source code must retain the above copyright 19## notice, this list of conditions and the following disclaimer. 20## * Redistributions in binary form must reproduce the above copyright 21## notice, this list of conditions and the following disclaimer in 22## the documentation and/or other materials provided with the 23## distribution. 24## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 25## the names of its contributors may be used to endorse or promote 26## products derived from this software without specific prior written 27## permission. 28## 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 40## $QT_END_LICENSE$ 41## 42############################################################################# 43 44 45from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, 46 QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout, 47 QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit, 48 QVBoxLayout) 49 50 51class Dialog(QDialog): 52 NumGridRows = 3 53 NumButtons = 4 54 55 def __init__(self): 56 super(Dialog, self).__init__() 57 58 self.createMenu() 59 self.createHorizontalGroupBox() 60 self.createGridGroupBox() 61 self.createFormGroupBox() 62 63 bigEditor = QTextEdit() 64 bigEditor.setPlainText("This widget takes up all the remaining space " 65 "in the top-level layout.") 66 67 buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) 68 69 buttonBox.accepted.connect(self.accept) 70 buttonBox.rejected.connect(self.reject) 71 72 mainLayout = QVBoxLayout() 73 mainLayout.setMenuBar(self.menuBar) 74 mainLayout.addWidget(self.horizontalGroupBox) 75 mainLayout.addWidget(self.gridGroupBox) 76 mainLayout.addWidget(self.formGroupBox) 77 mainLayout.addWidget(bigEditor) 78 mainLayout.addWidget(buttonBox) 79 self.setLayout(mainLayout) 80 81 self.setWindowTitle("Basic Layouts") 82 83 def createMenu(self): 84 self.menuBar = QMenuBar() 85 86 self.fileMenu = QMenu("&File", self) 87 self.exitAction = self.fileMenu.addAction("E&xit") 88 self.menuBar.addMenu(self.fileMenu) 89 90 self.exitAction.triggered.connect(self.accept) 91 92 def createHorizontalGroupBox(self): 93 self.horizontalGroupBox = QGroupBox("Horizontal layout") 94 layout = QHBoxLayout() 95 96 for i in range(Dialog.NumButtons): 97 button = QPushButton("Button %d" % (i + 1)) 98 layout.addWidget(button) 99 100 self.horizontalGroupBox.setLayout(layout) 101 102 def createGridGroupBox(self): 103 self.gridGroupBox = QGroupBox("Grid layout") 104 layout = QGridLayout() 105 106 for i in range(Dialog.NumGridRows): 107 label = QLabel("Line %d:" % (i + 1)) 108 lineEdit = QLineEdit() 109 layout.addWidget(label, i + 1, 0) 110 layout.addWidget(lineEdit, i + 1, 1) 111 112 self.smallEditor = QTextEdit() 113 self.smallEditor.setPlainText("This widget takes up about two thirds " 114 "of the grid layout.") 115 116 layout.addWidget(self.smallEditor, 0, 2, 4, 1) 117 118 layout.setColumnStretch(1, 10) 119 layout.setColumnStretch(2, 20) 120 self.gridGroupBox.setLayout(layout) 121 122 def createFormGroupBox(self): 123 self.formGroupBox = QGroupBox("Form layout") 124 layout = QFormLayout() 125 layout.addRow(QLabel("Line 1:"), QLineEdit()) 126 layout.addRow(QLabel("Line 2, long text:"), QComboBox()) 127 layout.addRow(QLabel("Line 3:"), QSpinBox()) 128 self.formGroupBox.setLayout(layout) 129 130 131if __name__ == '__main__': 132 133 import sys 134 135 app = QApplication(sys.argv) 136 dialog = Dialog() 137 sys.exit(dialog.exec_()) 138