Y
You're not right to layout to widget.
All QWidget sub-classes can use the mocks to manage their daughters.The QWidget function:setLayout() applies the mockery to the view.self.mainLayout = QVBoxLayout()self.setLayout(self.mainLayout)orself.mainLayout = QVBoxLayout(self)It's the same thing!I recommend that you read
https://doc.qt.io/qt-5/layout.html and
https://doc.qt.io/qt-5/qwidget.html#setLayout I made a little easier on your application,
But still getting the data in the way you're planning,
It's gonna be really hard.I filled out the method. print_report not functional,
that'll work on the stick on the "print report" button.In the first part, it is shown how to extract the data from
QGroupBox('main info') and it's just flowers. ♪ ♪
how to extract data from other sources QGroupBox It's scary to think.I suggest you try to create, like a dictionary, self.mainDict = {}and fill it in as it goes.In the second part, method print_report slightly shown
Get the data out of that dictionary.import sys
from pprint import pprint as pp
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, \
QLabel, QGroupBox, QHBoxLayout, QVBoxLayout, QDialog, QGridLayout, \
QComboBox, QDateEdit, QFormLayout
from PyQt5 import QtCore
class NewWork(QGroupBox):
def init(self, parent=None):
super(NewWork, self).init(parent)
self.row = 2
self.parent = parent
self.numNewWork = parent.numNewWork
print(self.numNewWork)
self.setStyleSheet("background: teal;")
self.hide()
def initwork(self):
workinfo = QGroupBox(f'work info {self.numNewWork}')
materialinfo = QGroupBox(f'material info {self.numNewWork}')
machineinfo = QGroupBox(f'machine info {self.numNewWork}')
personinfo = QGroupBox(f'person info {self.numNewWork}')
workline = QLineEdit()
pkbline = QLineEdit()
pkeline = QLineEdit()
storona = QComboBox()
storona.addItems(['choose side', 'left side', 'right side', 'all side'])
self.workgrid = QFormLayout(workinfo)
self.workgrid.addRow('work', workline)
self.workgrid.addRow('pk begin', pkbline)
self.workgrid.addRow('pk end', pkeline)
self.workgrid.addWidget(storona)
machinelay = QGridLayout(machineinfo)
machine = QPushButton('add machine')
machine.clicked.connect(lambda ch, lay=machinelay: self.add_machine(lay, r=self.row))
machinelay.addWidget(machine, 1, 0, 1, 0)
thirdlay = QHBoxLayout()
thirdlay.addWidget(workinfo)
thirdlay.addWidget(materialinfo)
thirdlay.addWidget(machineinfo)
thirdlay.addWidget(personinfo)
self.parent.mainDict[f'work info {self.numNewWork}'] = (workline, pkbline, pkeline, storona)
self.parent.mainDict[f'machine info {self.numNewWork}'] = []
return thirdlay # ---->>>
def add_machine(self, lay, r=2):
#print(lay, r)
name = lay.parent().title()
line_add = QLineEdit(self)
line_add1 = QLineEdit(self)
line_add2 = QLineEdit(self)
lay.addWidget(QLabel('<h3>machine</h3>'), r, 1)
lay.addWidget(line_add, r, 2)
lay.addWidget(QLabel('number'), r, 3)
lay.addWidget(line_add1, r, 4)
lay.addWidget(QLabel('owner'), r, 5)
lay.addWidget(line_add2, r, 6)
self.row += 1
self.parent.mainDict[name].append([line_add, line_add1, line_add2])
class TextApp(QWidget):
def init(self):
super().init()
self.mainDict = {}
self.initUI()
def initUI(self):
maininfo = QGroupBox('main info')
maininfogrid = QFormLayout(maininfo)
fioline = QLineEdit(self)
dateline = QDateEdit(self)
dateline.setDate(QtCore.QDate.currentDate())
dateline.setCalendarPopup(True)
maininfogrid.addRow('fio', fioline)
maininfogrid.addRow('date', dateline)
addwork = QPushButton('add work', self)
addwork.clicked.connect(self.add_work)
printreport = QPushButton('print report', self)
printreport.clicked.connect(self.print_report)
secondlay = QHBoxLayout()
secondlay.addWidget(addwork)
secondlay.addWidget(printreport)
self.maingrid = QVBoxLayout(self)
self.maingrid.addWidget(maininfo)
self.maingrid.addLayout(secondlay)
self.mainDict['main info'] = (fioline, dateline)
self.numNewWork = 1
self.add_work()
def add_work(self):
workwidget = NewWork(self)
thirdlay = workwidget.initwork() # <<<----
self.maingrid.addLayout(thirdlay)
self.numNewWork += 1
def print_report(self):
# первая часть
print(f'\n layout().count() -> {self.layout().count()}')
print(self.layout().itemAt(0))
print(f'\t -> {self.layout().itemAt(0).widget()} ')
print(f'\t -> {self.layout().itemAt(0).widget().children()}')
print(f'\t -> {self.layout().itemAt(0).widget().children()[2].text()}')
print(f'\t -> {self.layout().itemAt(0).widget().children()[4].dateTime().toString("dd-MM-yyyy")}')
print(f'{"-"*30}\n')
# втора часть
pp(self.mainDict)
print('\nmain info:')
print('\t', self.mainDict['main info'][0].text())
print('\t', self.mainDict['main info'][1].dateTime().toString("dd-MM-yyyy"))
# 'work info 1'
print('work info 1:')
print('\t', self.mainDict['work info 1'][0].text())
print('\t', self.mainDict['work info 1'][1].text())
print('\t', self.mainDict['work info 1'][2].text())
print('\t', self.mainDict['work info 1'][3].currentText())
# 'machine info 1'
_list = self.mainDict['machine info 1']
if _list:
print('machine info 1:')
for i in _list:
print('\t', i[0].text(), i[1].text(), i[2].text())
if name == 'main':
app = QApplication(sys.argv)
ex = TextApp()
ex.show()
sys.exit(app.exec_())