[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: Can't resize containers in scrollArea
From:       Maurizio Berti <maurizio.berti () gmail ! com>
Date:       2021-08-27 16:44:14
Message-ID: CAPn+-XQGy76kqt_n_TgLQczVvNKof1BGHdwefF=ZKKwzJr6Vkg () mail ! gmail ! com
[Download RAW message or body]

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@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@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

[Attachment #3 (text/html)]

<div dir="ltr"><div dir="ltr">Sorry, I completely forgot that scroll areas support \
the size adjust policy, so you can just do the following:<div><br></div><div><font \
face="monospace">    \
table.setSizeAdjustPolicy(table.AdjustToContents)</font><br></div></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 27 ago 2021 alle \
ore 18:33 Maurizio Berti &lt;<a \
href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>&gt; ha \
scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div \
dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Well, you are setting a Fixed \
horizontal policy on the tables, which means that they cannot be &quot;shrunk&quot; \
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).<div><br></div><div>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:</div><div><br></div><div>- the horizontal header length();</div><div>- \
the vertical header size hint width;</div><div>- the vertical scroll bar size hint \
width;<br></div><div>- the frame width of the view, multiplied by two (left and right \
side);</div><div><br></div><div><div><font face="monospace">            width = \
(</font></div><div><font face="monospace">                h_header.length() + \
</font></div><div><font face="monospace">                \
table.verticalHeader().sizeHint().width() + </font></div><div><font face="monospace"> \
table.verticalScrollBar().sizeHint().width() + </font></div><div><font \
face="monospace">                table.frameWidth() * 2</font></div><div><font \
face="monospace">            )</font></div><div><font face="monospace">            \
table.setFixedWidth(width)</font></div></div><div><br></div><div>Note that the above \
will only work *after* setting the header labels *and* filling the whole \
table.</div><div><br>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).</div><div><br>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: <a \
href="https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html" \
target="_blank">https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html</a></div><div><br></div><div>Maurizio</div></div></div></div></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 27 ago 2021 alle \
ore 14:27 paparucino &lt;<a href="mailto:paparucino@gmail.com" \
target="_blank">paparucino@gmail.com</a>&gt; ha scritto:<br></div><blockquote \
class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex">Hello, I&#39;m new to the list and besides \
apologizing for any errors please <br> correct me so I can improve.<br>
My problem is that I can&#39;t delete the white space after the table with <br>
the data. Pls look at the attached drawing.<br>
The script I use is the one below.<br>
I noticed that whatever the value used for the various sizes (apart from <br>
MainWindow.resize (900, 650)) none of them affect the size of both the <br>
internal table and its container. I guess they are default values and I <br>
am not able to change them.<br>
Any help is appreciated.<br>
<br>
===================<br>
<br>
import calendar<br>
from functions import *<br>
from PyQt5 import QtCore, QtGui, QtWidgets<br>
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton<br>
<br>
<br>
class SpecialStyledItemDelegate(QtWidgets.QStyledItemDelegate):<br>
     def __init__(self, parent=None):<br>
         super().__init__(parent)<br>
         self._values = dict()<br>
<br>
     def add_text(self, text, row):<br>
         self._values[row] = text<br>
<br>
     def initStyleOption(self, option, index):<br>
         super().initStyleOption(option, index)<br>
         row = index.row()<br>
         if row in self._values:<br>
             option.text = self._values[row]<br>
             option.displayAlignment = QtCore.Qt.AlignCenter<br>
<br>
<br>
class Ui_MainWindow(QMainWindow):<br>
     def __init__(self):<br>
         super().__init__()<br>
         self.setupUi(self)<br>
<br>
     def setupUi(self, MainWindow):<br>
<br>
         MainWindow.setObjectName(&quot;MainWindow&quot;)<br>
         MainWindow.resize(900, 650)<br>
         self.centralwidget = QtWidgets.QWidget(MainWindow)<br>
         self.centralwidget.setObjectName(&quot;centralwidget&quot;)<br>
<br>
         self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)<br>
         self.horizontalLayout.setObjectName(&quot;horizontalLayout&quot;)<br>
<br>
         self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)<br>
         self.scrollArea.setWidgetResizable(True)<br>
         self.scrollArea.setObjectName(&quot;scrollArea&quot;)<br>
         self.scrollAreaWidgetContents = QtWidgets.QWidget()<br>
         #self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, <br>
0, 0))<br>
self.scrollAreaWidgetContents.setObjectName(&quot;scrollAreaWidgetContents&quot;)<br>
         self.layout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)<br>
         #self.layout.setContentsMargins(0, 0, 0, 0)<br>
         self.layout.setObjectName(&quot;layout&quot;)<br>
<br>
<br>
         conn = x<br>
         cursor = conn.cursor(buffered=True, dictionary=True)<br>
         mese = &#39;9&#39;<br>
         anno = &#39;2021&#39;<br>
<br>
         self.alphamese = Functions.convert_number_month[mese]<br>
         daysinmonth = calendar.monthrange(int(anno), int(mese))[1]<br>
         query = &quot;SELECT * FROM `alldata` WHERE anno = &#39;%s&#39; AND mese = \
<br> &#39;%s&#39;&quot; % (anno, mese)<br>
         cursor.execute(query)<br>
         search = cursor.fetchall()<br>
         zz = &#39;table_&#39;<br>
         cc = 0<br>
         for row in search:<br>
             self.vol_name = row[&#39;nome&#39;]<br>
             self.c_query = &quot;SELECT grp FROM volontari WHERE cognome = <br>
&#39;%s&#39;&quot; % (self.vol_name)<br>
             cursor.execute(self.c_query)<br>
             self.grp = cursor.fetchall()<br>
             for val in self.grp:<br>
                 table =&#39;zz%s&#39;%(str(cc))<br>
                 cc = cc+1<br>
                 self.group = val[&#39;grp&#39;]<br>
                 if self.group == &#39;C&#39;:<br>
<br>
                     table = QTableWidget(self.scrollAreaWidgetContents)<br>
                     table.setGeometry(QtCore.QRect(0, 5, 150, 597))<br>
                     sizePolicy = <br>
QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, <br>
QtWidgets.QSizePolicy.Expanding)<br>
                     #sizePolicy.setHorizontalStretch(50)<br>
                     #sizePolicy.setVerticalStretch(50)<br>
#sizePolicy.setHeightForWidth(table.sizePolicy().hasHeightForWidth())<br>
                     table.setSizePolicy(sizePolicy)<br>
                     table.setObjectName(&quot;StripTable&quot;)<br>
                     table.setRowCount(33)<br>
                     table.setColumnCount(3)<br>
<br>
                     self.special_delegate = SpecialStyledItemDelegate()<br>
                     table.setItemDelegate(self.special_delegate)<br>
<br>
                     h_header = table.horizontalHeader()<br>
                     h_header.hide()<br>
                     for i in range(h_header.count() - 1):<br>
                         h_header.setSectionResizeMode(i, <br>
QtWidgets.QHeaderView.ResizeToContents)<br>
                     h_header.setSectionResizeMode(2, <br>
QtWidgets.QHeaderView.Fixed)<br>
                     v_header = table.verticalHeader()<br>
                     v_header.hide()<br>
                     v_header.setDefaultSectionSize(17)<br>
<br>
                     table.setSpan(1, 0, 1, 3)<br>
                     table.setSpan(0, 0, 1, 3)<br>
<br>
                     zz = 1<br>
                     self.m_query = &quot;SELECT * FROM `alldata` WHERE anno <br>
= &#39;%s&#39; AND mese = &#39;%s&#39; AND nome = &#39;%s&#39;&quot; % (<br>
                     anno, mese, self.vol_name)<br>
                     cursor.execute(self.m_query)<br>
                     result = cursor.fetchall()<br>
                     self.special_delegate.add_text(self.vol_name, 0)<br>
                     self.special_delegate.add_text(self.alphamese, 1)<br>
<br>
                     #daysinmonth = 30<br>
                 while zz &lt;= daysinmonth:<br>
                     for inrow in result:<br>
                         ##<br>
                         # Writes data in cell tables<br>
                         ##<br>
                         day = &#39;d&#39; + str(zz)<br>
                         self.value = inrow[day]<br>
                         a_date = datetime.date(int(anno), int(mese), zz)<br>
                         self.dow = a_date.strftime(&quot;%a&quot;)<br>
                         self.dow = Functions.convert_en_it[self.dow]<br>
                         if (self.dow == &#39;Sun&#39; or self.dow == &#39;Sat&#39;) \
                and (<br>
                                 self.value == &#39;P&#39; or self.value == \
&#39;D&#39; <br> or self.value == &#39;F&#39; or self.value == &#39;B&#39;):<br>
                             self.value = Functions.eva_mp1[self.value]<br>
                         else:<br>
                             self.value = Functions.eva_mp[self.value]<br>
                         table.setItem(zz + 1, 0, <br>
QtWidgets.QTableWidgetItem(str(zz)))<br>
                         table.setItem(zz + 1, 1, <br>
QtWidgets.QTableWidgetItem(self.dow))<br>
                         item = QtWidgets.QTableWidgetItem(self.value)<br>
item.setTextAlignment(QtCore.Qt.AlignCenter)<br>
                         table.setItem(zz + 1, 2, item)<br>
                         zz += 1<br>
             self.layout.addWidget(table)<br>
<br>
self.scrollArea.setWidget(self.scrollAreaWidgetContents)<br>
             self.horizontalLayout.addWidget(self.scrollArea)<br>
             MainWindow.setCentralWidget(self.centralwidget)<br>
<br>
             self.retranslateUi(MainWindow)<br>
             QtCore.QMetaObject.connectSlotsByName(MainWindow)<br>
<br>
     def retranslateUi(self, MainWindow):<br>
         _translate = QtCore.QCoreApplication.translate<br>
         MainWindow.setWindowTitle(_translate(&quot;MainWindow&quot;, \
&quot;MainWindow&quot;))<br> <br>
<br>
<br>
<br>
     def show_new_window(self):<br>
         print(&#39;Printato&#39;)<br>
         pass<br>
<br>
app = QApplication(sys.argv)<br>
<br>
window = Ui_MainWindow()<br>
window.show()<br>
<br>
app.exec()<br>
<br>
<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr">È difficile \
avere una convinzione precisa quando si parla delle ragioni del cuore. - \
&quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div> </blockquote></div><br \
clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">È difficile \
avere una convinzione precisa quando si parla delle ragioni del cuore. - \
&quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic