Can't draw simple rectangle qt

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
stefankorisnik3
Posts: 101
Joined: Sun Jul 24, 2022 12:49 pm

Can't draw simple rectangle qt

Post by stefankorisnik3 »

I want to draw simple rectangle with qt. I'm using following code but no rectangle. Can you tell me what is wrong about this code?

Code: Select all

from PySide import QtGui, QtCore
from PySide.QtGui import QPainter, QBrush, QPen
from PySide.QtGui import Qt

painter = QPainter(self)
painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
painter.setBrush(QBrush(Qt.green, Qt.DiagCrossPattern))
painter.drawRect(QtCore.QRect(0, 0, 50,50))

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United Kingdom (en_GB)
Installed mods: 
  * CurvedShapes 1.0.3
  * Curves 0.5.2
chrisb
Veteran
Posts: 53785
Joined: Tue Mar 17, 2015 9:14 am

Re: Can't draw simple rectangle qt

Post by chrisb »

I'm sorry not being able to answer your question, but may I ask what you need such a rectangle for? It won't be available for modeling.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
stefankorisnik3
Posts: 101
Joined: Sun Jul 24, 2022 12:49 pm

Re: Can't draw simple rectangle qt

Post by stefankorisnik3 »

For the GUI
User avatar
wandererfan
Veteran
Posts: 6238
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Can't draw simple rectangle qt

Post by wandererfan »

stefankorisnik3 wrote: Thu Aug 11, 2022 8:39 pm I want to draw simple rectangle with qt. I'm using following code but no rectangle. Can you tell me what is wrong about this code?
Where do you expect to see your rectangle? You're drawing with an orphan painter.

This sort of code would normally be in the paint event of a custom widget that has been added to the main window or another higher level widget.
stefankorisnik3
Posts: 101
Joined: Sun Jul 24, 2022 12:49 pm

Re: Can't draw simple rectangle qt

Post by stefankorisnik3 »

On the GUI window.
User avatar
wandererfan
Veteran
Posts: 6238
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Can't draw simple rectangle qt

Post by wandererfan »

stefankorisnik3 wrote: Fri Aug 12, 2022 2:17 am On the GUI window.
If you mean the 3d view, this isn't the way to do it. You'd want to use something like

Code: Select all

import Draft
r = Draft.makeRectangle(100, 100)
r.recompute()
chrisb
Veteran
Posts: 53785
Joined: Tue Mar 17, 2015 9:14 am

Re: Can't draw simple rectangle qt

Post by chrisb »

stefankorisnik3 wrote: Fri Aug 12, 2022 2:17 am On the GUI window.
Do you mean to create a dialog on the fly and the rectangle should be some frame on it?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
gbParametric
Posts: 23
Joined: Wed Apr 20, 2022 9:06 pm

Re: Can't draw simple rectangle qt

Post by gbParametric »

What do you mean by drawing a rectangle ?
Something like this ?
https://doc.qt.io/qt-6/qtwidgets-painti ... ample.html
edit (Added another link)
https://likegeeks.com/pyqt5-drawing-tutorial/
stefankorisnik3
Posts: 101
Joined: Sun Jul 24, 2022 12:49 pm

Re: Can't draw simple rectangle qt

Post by stefankorisnik3 »

Yes i meant to create a dialog on the fly and the rectangle should be some frame on it.
User avatar
onekk
Veteran
Posts: 6097
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Can't draw simple rectangle qt

Post by onekk »

stefankorisnik3 wrote: Sat Aug 13, 2022 8:02 pm Yes i meant to create a dialog on the fly and the rectangle should be some frame on it.
It is not clear where you want to make the rectangle appear?

The extrapolate code you posted is using

Code: Select all

painter = QPainter(self)
and this usually say that is used in a Class, but a Widgest has to have a Parent, and generally to have them appear on the FC at least the container must be a a child of FC MainWindow, maybe a dilalog, maybe some other part of the interface.

Probably some Qt background is required, sadly Qt documentation is usually addressing C++, and PySide2 example are not very usual, so some guessing and try are required for non trivial things.

Plus use the Qt5 documentation as Qt6 is not implemented for now, and something has changed from Qt5 to Qt6 so better to stay on Qt5 for now if you are not used to Qt mechanics, that is not very linear sometimes.

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/
Post Reply