QGroupBox with two rows of widgets

Rich Shepard rshepard at appl-ecosys.com
Mon May 3 16:21:54 BST 2021


Running Slackware64-14.2, Python-3.7.2, PyQt-5.13.2.

QGroupBox is a container holding layouts with widgets. I want to group six
widgets -- in two rows -- into a groupbox. Having each row of widgets
in a group with the same title was successful, but I'm failing at having
both rows of widgets in a single group box.

A minimal working example is attached.

The problem is in lines 72 (and the following line):

$ python biomwe.py 
Traceback (most recent call last):
   File "biomwe.py", line 92, in <module>
     bio = BiotaWindow()
   File "biomwe.py", line 72, in __init__
     tbox.addLayout(tb1)
AttributeError: 'QGroupBox' object has no attribute 'addLayout'

and

$ python biomwe.py 
Traceback (most recent call last):
   File "biomwe.py", line 92, in <module>
     bio = BiotaWindow()
   File "biomwe.py", line 72, in __init__
     tbox.addWidget(tb1)
AttributeError: 'QGroupBox' object has no attribute 'addWidget'

I've not found an example of what I'm trying to do in Alan Moore's book or
in my web searches. The latter have examples of single widgets in a
QVBoxLayout, but not multiple widgets in each row.

Looking forward to learning how to do this.

TIA,

Rich
-------------- next part --------------
#!/usr/bin/env python3

import sys

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

class BiotaWindow(qtw.QWidget):
    def __init__(self):
        super().__init__()
        bio = qtw.QWidget(windowTitle='Biota')
        self.setFixedSize(800, 600)

        # Widgets
        #Row 2
        class_label = qtw.QLabel('Class', self)
        class_name = qtw.QLineEdit(self,
            clearButtonEnabled = True,
            maxLength = 16)
        order_label = qtw.QLabel('Order', self)
        order_name = qtw.QLineEdit(self,
            clearButtonEnabled = True,
            maxLength = 16)
        family_label = qtw.QLabel('Family', self)
        family_name = qtw.QLineEdit(self,
            clearButtonEnabled = True,
            maxLength = 16)
                        
        # Row 3
        genus_label = qtw.QLabel('Genus', self)
        genus_name = qtw.QLineEdit(self,
            clearButtonEnabled = True,
            maxLength = 16)
        species_label = qtw.QLabel('Species', self)
        species_name = qtw.QLineEdit(self,
            clearButtonEnabled = True,
            maxLength = 16)
        subspecies_label = qtw.QLabel('Subspecies', self)
        subspecies_name = qtw.QLineEdit(self,
            placeholderText = 'Subspecies',
            clearButtonEnabled = True,
            maxLength = 16)
        common_label = qtw.QLabel('Common Name', self)
        common_name = qtw.QLineEdit(self,
            clearButtonEnabled = True,
            maxLength = 24)
        
        # Group boxes
        tbox = qtw.QGroupBox('Taxonomy')
        tbox.setLayout(qtw.QVBoxLayout())
        tb1 = qtw.QHBoxLayout()
        tb2 = qtw.QHBoxLayout()
        
        # Layouts used for widgets
        tb1.layout().addWidget(class_label)
        tb1.layout().addWidget(class_name)
        tb1.layout().addWidget(order_label)
        tb1.layout().addWidget(order_name)
        tb1.layout().addWidget(family_label)
        tb1.layout().addWidget(family_name)
        tb2.layout().addWidget(genus_label)
        tb2.layout().addWidget(genus_name)
        tb2.layout().addWidget(species_label)
        tb2.layout().addWidget(species_name)
        tb2.layout().addWidget(subspecies_label)
        tb2.layout().addWidget(subspecies_name)
        tb2.layout().addWidget(common_label)
        tb2.layout().addWidget(common_name)
        
        # Main window layout
        tbox.addWidget(tb1)
        tbox.addWidget(tb2)
        inner = qtw.QVBoxLayout()
        inner.addWidget(tbox)
        inner.addWidget(fbox)
        outer = qtw.QWidget(self)
        outer.setLayout(inner)
        
        self.show()
         
    # Methods
    def save(self):
        pass

    def cancel(save):
        pass


if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    bio = BiotaWindow()
    sys.exit(app.exec())       


More information about the PyQt mailing list