Can't resize containers in scrollArea
Maurizio Berti
maurizio.berti at gmail.com
Fri Aug 27 17:44:14 BST 2021
Sorry, I completely forgot that scroll areas support the size adjust
policy, so you can just do the following:
table.setSizeAdjustPolicy(table.AdjustToContents)
Il giorno ven 27 ago 2021 alle ore 18:33 Maurizio Berti <
maurizio.berti at gmail.com> ha scritto:
> Well, you are setting a Fixed horizontal policy on the tables, which means
> that they cannot be "shrunk" even if they could, and they will then use
> their default size hint (which is usually 256x192 for widgets that inherit
> from QAbstractScrollArea, so, all item views).
>
> Assuming that the table contents are not going to change, the solution is
> to set a fixed width based on the contents, and the width is computed by
> the sum of:
>
> - the horizontal header length();
> - the vertical header size hint width;
> - the vertical scroll bar size hint width;
> - the frame width of the view, multiplied by two (left and right side);
>
> width = (
> h_header.length() +
> table.verticalHeader().sizeHint().width() +
> table.verticalScrollBar().sizeHint().width() +
> table.frameWidth() * 2
> )
> table.setFixedWidth(width)
>
> Note that the above will only work *after* setting the header labels *and*
> filling the whole table.
>
> A better implementation would use a subclass of the table widget and
> update the fixed width whenever the model changes (row/column amount, data
> changed).
>
> On an unrelated matter, please consider that you should **NEVER** modify
> the files generated by pyuic, and you should instead implement the logic
> (including the dynamic table creation) in a separate script, and then use a
> subclass of QMainWindow (or the QWidget subclass used in Designer, like
> QDialog for instance) *and* the imported form class, as suggested for the
> multiple inheritance method in the official PyQt guidelines about using
> Designer:
> https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html
>
> Maurizio
>
> Il giorno ven 27 ago 2021 alle ore 14:27 paparucino <paparucino at gmail.com>
> ha scritto:
>
>> 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()
>>
>>
>>
>>
>
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>
--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210827/80d02f34/attachment-0001.htm>
More information about the PyQt
mailing list