My code does not return the actual value of the QWidget

0

Good evening I have the following code: The error that occurs to me is that when I print the size of self.widget.width() I get the value of 100 while in the QT Designer I show that the value is 450

The second error is that when you pass the value to the graph using visual.pygal.Line(width=self.widget.width()) , it maintains that static value and does not change the value as the widget increases or decreases the value of its width ()

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import pygal
from pygal.style import BlueStyle


class nGrafico(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("n_grafico.ui",self)
        Self.graf (1,2,3,4,5,6)

    def graf(self,l,l1,l2,l3,l4,l5):

        print(self.widget.width())

        visual = pygal.Line(style=BlueStyle,title=u"Reporte de Internet",legend_at_bottom=True)
        visual.x_labels=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
        visual.add("Internet Matutino", l)
        visual.add("Internet Vespertino", l1)

        data = visual.render_data_uri()
        self.widget.setUrl(QUrl(data))

        self.graf2(l2,l3)
        self.graf3(l4,l5)

    def graf2(self,l,l1):
        visual = pygal.Line(style=BlueStyle,title="Reporte de Recargas",legend_at_bottom=True)
        visual.x_labels=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

        visual.add("Recargas Matutino", l)
        visual.add("Recargas Vespertino", l1)

        data = visual.render_data_uri()
        self.widget2.setUrl(QUrl(data))

    def graf3(self,l,l1):
        visual = pygal.Line(style=BlueStyle,title="Reporte de Sistema",legend_at_bottom=True)
        visual.x_labels=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

        visual.add("Sistema Matutino", l)
        visual.add("Sistema Vespertino", l1)

        data = visual.render_data_uri()
        self.widget3.setUrl(QUrl(data))



#app = QApplication([])
#n = nGrafico()
#n.show()
#app.exec_()

File .ui.

<?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>875</width>
            <height>581</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <layout class="QGridLayout" name="gridLayout">
            <property name="leftMargin">
             <number>0</number>
            </property>
            <property name="topMargin">
             <number>0</number>
            </property>
            <property name="rightMargin">
             <number>0</number>
            </property>
            <property name="bottomMargin">
             <number>0</number>
            </property>
            <property name="spacing">
             <number>0</number>
            </property>
            <item row="1" column="1">
             <widget class="QWebEngineView" name="widget4" native="true"/>
            </item>
            <item row="0" column="1">
             <widget class="QWebEngineView" name="widget2" native="true"/>
            </item>
            <item row="1" column="0">
             <widget class="QWebEngineView" name="widget3" native="true"/>
            </item>
            <item row="0" column="0">
             <widget class="QWebEngineView" name="widget" native="true"/>
            </item>
           </layout>
          </widget>
         </widget>
         <customwidgets>
          <customwidget>
           <class>QWebEngineView</class>
           <extends>QWidget</extends>
           <header>PyQt5.QtWebEngineWidgets</header>
           <container>1</container>
          </customwidget>
         </customwidgets>
         <resources/>
         <connections/>
        </ui>
    
asked by Revsky01 22.09.2018 в 07:34
source

1 answer

2
  

The error that occurs to me is that when I print the size of   self.widget.width () throws me the value of 100 while in the QT   Designer shows me that the value is 450

The size of the widget used by the layout is the sizeHint () and depends on the content, for efficiency reasons Qt does not update the items before displaying them, so the size of the widget is different before and after it is displayed. You are calculating the size of the widget before it is displayed without the layout doing its job, and in the case of Qt Designer it will load the widget and display it, therefore the layout has already done its job.

If you want to get the correct size you must show the widget first:

self.show()
print(self.widget.width())
  

the value to the graph using visual.pygal.Line (width = self.widget.width ()), maintains that value   static and does not change the value as the widget increases or   Decrease the value of your width ()

Well that is to be expected since you never point to the pygal item that the window has changed in size, the pygal items have the size that was initially set, the solution is that each time you change the size set them in the pygal item, and then generate the new url for it we must overwrite the resizeEvent () method of the QWebEngineView, so the solution is to promote the widget for this we create the custom widget:

pygalview.py

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import pygal
from pygal.style import BlueStyle


class PyGalView(QtWebEngineWidgets.QWebEngineView):
    def add_item(self, item):
        setattr(self, "item", item)
        url = item.render_data_uri()
        self.setUrl(QtCore.QUrl(url))

    def resizeEvent(self, event):
        if hasattr(self, "item"):
            item = getattr(self, "item")
            item.width = self.size().width()
            item.height = self.size().height()
            url = item.render_data_uri()
            self.setUrl(QtCore.QUrl(url))
        super(PyGalView, self).resizeEvent(event)


if __name__ == '__main__':
    import sys
    import random

    app = QtWidgets.QApplication(sys.argv)
    visual = pygal.Line(style=BlueStyle,title=u"Reporte de Internet",legend_at_bottom=True)
    visual.x_labels= list(range(1, 32))
    l, l1 = [[random.randint(0, 1000) for _ in range(30)] for _ in range(2)]
    visual.add("Internet Matutino", l)
    visual.add("Internet Vespertino", l1)
    w = PyGalView()
    w.add_item(visual)
    w.show()
    sys.exit(app.exec_())

We edit the .ui to use the PyGalView:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>875</width>
    <height>581</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <property name="spacing">
     <number>0</number>
    </property>
    <item row="1" column="1">
     <widget class="PyGalView" name="widget4"/>
    </item>
    <item row="0" column="1">
     <widget class="PyGalView" name="widget2"/>
    </item>
    <item row="1" column="0">
     <widget class="PyGalView" name="widget3"/>
    </item>
    <item row="0" column="0">
     <widget class="PyGalView" name="widget"/>
    </item>
   </layout>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PyGalView</class>
   <extends>QWebEngineView</extends>
   <header location="global">pygalview</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

main.py

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, uic
import pygal
from pygal.style import BlueStyle
import random


class nGrafico(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        uic.loadUi("n_grafico.ui", self)
        values = [[random.randint(0, 1000) for _ in range(30)] for _ in range(6)]
        self.graf(*values)

    def graf(self, l, l1, l2, l3, l4, l5):
        visual = pygal.Line(style=BlueStyle,title=u"Reporte de Internet",legend_at_bottom=True)
        visual.x_labels = list(range(1, 32))
        visual.add("Internet Matutino", l)
        visual.add("Internet Vespertino", l1)
        self.widget.add_item(visual)
        self.graf2(l2,l3)
        self.graf3(l4,l5)

    def graf2(self, l, l1):
        visual = pygal.Line(style=BlueStyle,title="Reporte de Recargas",legend_at_bottom=True)
        visual.x_labels = list(range(1, 32))
        visual.add("Recargas Matutino", l)
        visual.add("Recargas Vespertino", l1)
        self.widget2.add_item(visual)

    def graf3(self,l,l1):
        visual = pygal.Line(style=BlueStyle,title="Reporte de Sistema",legend_at_bottom=True)
        visual.x_labels = list(range(1, 32))
        visual.add("Sistema Matutino", l)
        visual.add("Sistema Vespertino", l1)
        self.widget3.add_item(visual)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    n = nGrafico()
    n.show()
    sys.exit(app.exec_())
├── main.py
├── n_grafico.ui
└── pygalview.py

    
answered by 22.09.2018 / 16:52
source