Can't resize containers in scrollArea
paparucino
paparucino at gmail.com
Fri Aug 27 13:26:51 BST 2021
Hello, I'm new to the list and besides apologizing for any errors please
correct me so I can improve.
My problem is that I can't delete the white space after the table with
the data. Pls look at the attached drawing.
The script I use is the one below.
I noticed that whatever the value used for the various sizes (apart from
MainWindow.resize (900, 650)) none of them affect the size of both the
internal table and its container. I guess they are default values and I
am not able to change them.
Any help is appreciated.
===================
import calendar
from functions import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
class SpecialStyledItemDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__(parent)
self._values = dict()
def add_text(self, text, row):
self._values[row] = text
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
row = index.row()
if row in self._values:
option.text = self._values[row]
option.displayAlignment = QtCore.Qt.AlignCenter
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(900, 650)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
#self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0,
0, 0))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.layout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
#self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setObjectName("layout")
conn = x
cursor = conn.cursor(buffered=True, dictionary=True)
mese = '9'
anno = '2021'
self.alphamese = Functions.convert_number_month[mese]
daysinmonth = calendar.monthrange(int(anno), int(mese))[1]
query = "SELECT * FROM `alldata` WHERE anno = '%s' AND mese =
'%s'" % (anno, mese)
cursor.execute(query)
search = cursor.fetchall()
zz = 'table_'
cc = 0
for row in search:
self.vol_name = row['nome']
self.c_query = "SELECT grp FROM volontari WHERE cognome =
'%s'" % (self.vol_name)
cursor.execute(self.c_query)
self.grp = cursor.fetchall()
for val in self.grp:
table ='zz%s'%(str(cc))
cc = cc+1
self.group = val['grp']
if self.group == 'C':
table = QTableWidget(self.scrollAreaWidgetContents)
table.setGeometry(QtCore.QRect(0, 5, 150, 597))
sizePolicy =
QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed,
QtWidgets.QSizePolicy.Expanding)
#sizePolicy.setHorizontalStretch(50)
#sizePolicy.setVerticalStretch(50)
#sizePolicy.setHeightForWidth(table.sizePolicy().hasHeightForWidth())
table.setSizePolicy(sizePolicy)
table.setObjectName("StripTable")
table.setRowCount(33)
table.setColumnCount(3)
self.special_delegate = SpecialStyledItemDelegate()
table.setItemDelegate(self.special_delegate)
h_header = table.horizontalHeader()
h_header.hide()
for i in range(h_header.count() - 1):
h_header.setSectionResizeMode(i,
QtWidgets.QHeaderView.ResizeToContents)
h_header.setSectionResizeMode(2,
QtWidgets.QHeaderView.Fixed)
v_header = table.verticalHeader()
v_header.hide()
v_header.setDefaultSectionSize(17)
table.setSpan(1, 0, 1, 3)
table.setSpan(0, 0, 1, 3)
zz = 1
self.m_query = "SELECT * FROM `alldata` WHERE anno
= '%s' AND mese = '%s' AND nome = '%s'" % (
anno, mese, self.vol_name)
cursor.execute(self.m_query)
result = cursor.fetchall()
self.special_delegate.add_text(self.vol_name, 0)
self.special_delegate.add_text(self.alphamese, 1)
#daysinmonth = 30
while zz <= daysinmonth:
for inrow in result:
##
# Writes data in cell tables
##
day = 'd' + str(zz)
self.value = inrow[day]
a_date = datetime.date(int(anno), int(mese), zz)
self.dow = a_date.strftime("%a")
self.dow = Functions.convert_en_it[self.dow]
if (self.dow == 'Sun' or self.dow == 'Sat') and (
self.value == 'P' or self.value == 'D'
or self.value == 'F' or self.value == 'B'):
self.value = Functions.eva_mp1[self.value]
else:
self.value = Functions.eva_mp[self.value]
table.setItem(zz + 1, 0,
QtWidgets.QTableWidgetItem(str(zz)))
table.setItem(zz + 1, 1,
QtWidgets.QTableWidgetItem(self.dow))
item = QtWidgets.QTableWidgetItem(self.value)
item.setTextAlignment(QtCore.Qt.AlignCenter)
table.setItem(zz + 1, 2, item)
zz += 1
self.layout.addWidget(table)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.horizontalLayout.addWidget(self.scrollArea)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def show_new_window(self):
print('Printato')
pass
app = QApplication(sys.argv)
window = Ui_MainWindow()
window.show()
app.exec()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 210827-135101.jpg
Type: image/jpeg
Size: 136603 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210827/1bd60dc8/attachment-0001.jpg>
More information about the PyQt
mailing list