QtViewportSize.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

QtViewportSize.

Post by keithsloan52 »

I am working on an update to the OpenSCAD workbench Add Element command.
With my updates there are now two Qt TextEdit windows, the first one defined in AddSCADWIdget is the main text entry Window.
I would like to have it so that its size can be larger than currently allowed when the parent Window is enlarged.
It seems to be limited by the maximumViewportSize which if I print it out is 640 x 480.
I have very limited knowledge of Qt and not able to work out how the Viewport works or where its maximum is set.
Anybody able to help?

Update to OpenSCADCommands.py attached
OpenSCADCommands.py
(26.66 KiB) Downloaded 23 times
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: QtViewportSize.

Post by onekk »

I can't find the relevant code, but when programming with Qt, doing interfaces by hand, i have found that sometimes it is not the field but the layout container.

The field could expand at maximun of the space allowed by the Layout container, if the layout container, is contained (worry for the joke) in another container, two container and some other setting, to define the border are responsible for the max dimension.

Try to see if in this page you could find something useful.

https://doc.qt.io/qt-5/layout.html

se under "Manual Layout" and "Layout Issues", maybe if the "interface" is managed by FreeCAD, maybe reusing some of this spaces (like the space in TreeView), some signal behaviour could be reimplemented.
The widget will get an event of type QEvent::LayoutRequest when the layout needs to be recalculated. Reimplement QWidget::event() to handle QEvent::LayoutRequest events.
Plus it the widget is inserted in Grid Layout some space are reserved in the grid to make some borders, plus columns could be limited in expansion, as you could fix a ratio, says column 1 has to be 2 times large than column2 and similar things.

https://doc.qt.io/qt-5/qgridlayout.html#details

Hope it helps

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: QtViewportSize.

Post by keithsloan52 »

onekk wrote: Tue Apr 27, 2021 3:13 pm I can't find the relevant code, but when programming with Qt, doing interfaces by hand, i have found that sometimes it is not the field but the layout container.

The field could expand at maximun of the space allowed by the Layout container, if the layout container, is contained (worry for the joke) in another container, two container and some other setting, to define the border are responsible for the max dimension.

Try to see if in this page you could find something useful.

https://doc.qt.io/qt-5/layout.html

se under "Manual Layout" and "Layout Issues", maybe if the "interface" is managed by FreeCAD, maybe reusing some of this spaces (like the space in TreeView), some signal behaviour could be reimplemented.
The widget will get an event of type QEvent::LayoutRequest when the layout needs to be recalculated. Reimplement QWidget::event() to handle QEvent::LayoutRequest events.
Plus it the widget is inserted in Grid Layout some space are reserved in the grid to make some borders, plus columns could be limited in expansion, as you could fix a ratio, says column 1 has to be 2 times large than column2 and similar things.

https://doc.qt.io/qt-5/qgridlayout.html#details

Hope it helps

Regards

Carlo D.
Agree it is probably something down to the layout.

The relevant Layout is

Code: Select all

layout= QtGui.QVBoxLayout()
        layout.addLayout(layouth)
        layout.addWidget(self.checkboxmesh)
        layout.addWidget(self.textEdit)
        layout.addWidget(self.textMsg)
        self.setLayout(layout)
And what is required is that the textEdit widget self.TextEdit expands to the maximum for the parent window,
currently it can look like
6D062A0B-B6E1-4488-8ACD-B8C597AFBEA4.jpeg
6D062A0B-B6E1-4488-8ACD-B8C597AFBEA4.jpeg (83.42 KiB) Viewed 871 times
i.e. Want the widget with the text cube in it to expand to the maximum.
If I print out Its maximum height I get 16777215
If I print out its maximumViewportSize I get 640,480 hence my original post.
Last edited by keithsloan52 on Tue Apr 27, 2021 6:54 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: QtViewportSize.

Post by onekk »

You have to resize the text. try to guess the exact panel dimension and the set explicitly a text height:

Code: Select all

class log_box(QtGui.QDialog):
    """"""
    ap_style  = "font-size: 14px"

    def __init__(self):
        super(log_box, self).__init__()
        self.initUI()

    def initUI(self):
        self.result = usr_canc
        # create our window
        sc_wid = QtGui.QDesktopWidget().screenGeometry().width()
        sc_hei = QtGui.QDesktopWidget().screenGeometry().height()
        # get dimensions for available space on screen
        av_wid = QtGui.QDesktopWidget().availableGeometry().width()
        av_hei = QtGui.QDesktopWidget().availableGeometry().height()
        w_wid = av_wid * 0.50
        w_hei = av_hei * 0.50
        x_loc = (sc_wid - w_wid) * 0.5
        y_loc = (sc_hei - w_hei) * 0.5
        # define window        xLoc,yLoc,xDim,yDim
        self.setGeometry(x_loc, y_loc, w_wid, w_hei)
        self.setWindowTitle("Boxes-FC Log Window")
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

        but_hei = 30
        self.txt_win = QtGui.QLabel()
        self.txt_win.setWordWrap(True)
        self.txt_win.setStyleSheet(self.ap_style)
        self.txt_win.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)

        #print(dir(self.txt_win))

        sc_a = QtGui.QScrollArea()
        #sc_a.setBackgroundRole(QtGui.QPalette.Dark)
        sc_a.setWidgetResizable(True)
        sc_a.setWidget(self.txt_win)

        btn_cont = QtGui.QFrame()

        Btn_can = QtGui.QPushButton('Cancel', self)
        Btn_can.clicked.connect(self.onCancel)
        Btn_can.setAutoDefault(True)
        Btn_can.move(150, w_hei - but_hei * 1.5)
        # OK button
        Btn_ok = QtGui.QPushButton('OK', self)
        Btn_ok.clicked.connect(self.onOk)
        Btn_ok.move(260, w_hei - but_hei * 1.5)
        # now make the window visible

        lay1 = QtGui.QHBoxLayout(self)
        lay1.addWidget(Btn_ok)
        lay1.addWidget(Btn_can)
        btn_cont.setLayout(lay1)


        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(sc_a)
        lay.addWidget(btn_cont)

        self.setLayout(lay)
        self.show()

    def set_text(self, msg):
        self.txt_win.setText(msg)

    def onCancel(self):
        self.result = usr_canc
        self.close()

    def onOk(self):
        self.result == userOK
        self.close()


Not the proper code, but see at the start when i guess the main FreeCAD window dimension and resize the logbox accordingly.

I have to go now, maybe later, feel free to PM me.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: QtViewportSize.

Post by onekk »

See if this could be of some help:
The widget cannot be resized to a larger size than the maximum widget size.

By default, this property contains a size in which both width and height have values of 16777215.

Note: The definition of the QWIDGETSIZE_MAX macro limits the maximum size of widgets.

https://doc.qt.io/qt-5/qwidget.html#maximumSize-prop

About QSize property

https://doc.qt.io/qt-5/qsizepolicy.html

You have done fo textMsg

Code: Select all

self.textMsg.setMaximumHeight(h)
What is the widget that need to be resized?

if it is TExtEdit

Code: Select all

h = a minimum height dimension, maybe calculated relative from the windows size say 0.5 
self.textEdit.setMinimunHeight(h)
self.textEdit.setWidgetResizable(True)

If not working, try to use the Qgrilayout and set columnspan and rowspan accordingly, i.e

Code: Select all

	layouth=QtGui.QHBoxLayout()
        layouth.addWidget(self.buttonadd)
        layouth.addWidget(self.buttonload)
        layouth.addWidget(self.buttonsave)
        layouth.addWidget(self.buttonrefresh)
        layouth.addWidget(self.buttonclear)
        layout= QtGui.QVBoxLayout()
        layout.addLayout(layouth)
        layout.addWidget(self.checkboxmesh)
        layout.addWidget(self.textEdit)
        layout.addWidget(self.textMsg)
        self.setLayout(layout)
transform like this.

Code: Select all

	layout=QtGui.QGridLayout()
	#addWidget(QWidget, int r, int c, int rowspan, int columnspan)
        layout.addWidget(self.buttonadd, 0, 0, 1, 1)
        layout.addWidget(self.buttonload, 0, 1, 1, 1))
        layout.addWidget(self.buttonsave, 0, 2, 1, 1))
        layout.addWidget(self.buttonrefresh, 0, 3, 1, 1))
        layout.addWidget(self.buttonclear,  0, 4, 1, 1))
        layout.addWidget(self.checkboxmesh, 1, 0, 1, 4))
        layout.addWidget(self.textEdit,  2, 0, 2, 4) )
        layout.addWidget(self.textMsg, 4, 0, 1,41))
        self.setLayout(layout)
Gridlayout is more customizable, as you could even set the column interspace and many othe things. like minimun column and row height and width.

Hope it helps

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: QtViewportSize.

Post by keithsloan52 »

Thanks will give it a try.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: QtViewportSize.

Post by onekk »

try first the minimunsize and then resizable = False, it has less impact on the existing code.

see

https://doc.qt.io/qt-5/qwidget.html#setMinimumSize-1

but also:

https://doc.qt.io/qt-5/qsize.html#details

You could use this trick if you are referring to a widget in a complex code.

name the widget:

Code: Select all

label.setObjectName("label1")
Suppose that self.mainw is your "main windows" if you want to have the "reference" of the object to manipulate, you have to assign a self.something variabel name, but if you store only the main window you could simply find the objec by name in his childrem with:

Code: Select all


wid = self.mainw.findChild(QtCore.QObject, "lable1")
wid.addWidget(self.txt_win, 0, 0)
The example is done with a grid layout container in which I have to add a widget.

another trick to uniform things is using stylesheets:

suppose that you want to differentiate the label from the text in a bunch of input fields, you coudl set two styles, and then assign them simply with:

Code: Select all

self.ap_style  = "font-size: 14px"
self.tx_style  = "font-size: 16px"


....
wid.setStyleSheet(self.ap_style)
Another trick if you use the gridlayout is that you could set the row minimun height, per row, using:

Code: Select all

            wid.setRowMinimumHeight(row, 25)
            wid.setRowStretch(row, 1)
obviously wid is the GridLayout widget and row is the row number 25 is the size in pixel (Qt unit of measure)


Hope it helps

Regards

Carlo D.
Last edited by onekk on Wed Apr 28, 2021 5:25 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: QtViewportSize.

Post by keithsloan52 »

self.textEdit.setWidgetResizable(True) does not work textEdit does not have such a function https://doc.qt.io/qt-5/qtextedit-members.html

Grid layout does not help. the size of textEdit window stays the same.

Don't want to set minimum size its not limited maximum size that is needed.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: QtViewportSize.

Post by onekk »

Try with setSizePolicy

void QSizePolicy::setVerticalStretch(int stretchFactor)

Sets the vertical stretch factor of the size policy to the given stretchFactor. stretchFactor must be in the range [0,255].

When two widgets are adjacent to each other in a vertical layout, setting the vertical stretch factor of the widget on the top to 2 and the factor of widget on the bottom to 1 will ensure that the widget on the top will always be twice the size of the one on the bottom.

See also verticalStretch(), setHorizontalStretch(), and setVerticalPolicy().
see https://doc.qt.io/qt-5/qsizepolicy.html#details

Code: Select all

sizepolicy = QtWidgets.QSizePolicy(
            QtWidgets.QSizePolicy.Expanding,
            QtWidgets.QSizePolicy.Expanding))
            
self.textEdit.setSizePolicy(sizepolicy)         
            
QSizePolicy::Expanding GrowFlag | ShrinkFlag | ExpandFlag The sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).

QSizePolicy::Ignored ShrinkFlag | GrowFlag | IgnoreFlag The sizeHint() is ignored. The widget will get as much space as possible.

From this text I suppose that if you could inspect the SizePolicy and maybe change it to Expanding, or Ignored, or try to modify sizeHint() as it seem that the policy is to respect or not respect sizeHint() value.

Sadly I can't experiment, if there is a standalone file to test, maybe I could try for a while and see how it works. It depends also on the platform, on which platform you are testing, I know that Windows and Linux are sligthly different, in some aspects.

But I don't know the translation of Pyside in FreeCAD, fo the proper "names" as the "translation" done in the PySide to PySide2 adaption for compatibility.

Regards

Carlo D.
Last edited by onekk on Wed Apr 28, 2021 5:48 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: QtViewportSize.

Post by openBrain »

onekk wrote: Wed Apr 28, 2021 5:40 pm
But I don't know the translation of Pyside in FreeCAD, fo the "names"
There's no translation. Pyside is just PySide. :)
Post Reply