Center a QWidget window in the FreeCAD window ?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
wingman
Posts: 36
Joined: Sun Feb 09, 2020 12:28 pm

Center a QWidget window in the FreeCAD window ?

Post by wingman »

I want to open a QWidget window for a macro I am working on.

When I open the window, it appears in the upper left hand corner of my desktop. How do I make it open in the center of the FreeCAD application window ?

Thanks

Code: Select all

# import the QtGui via PySide
# FYI, PySide handles Python2 and Python3
from PySide.QtGui import *

# create a new class for this macro
# inherit everything from QWidget 
class MyWindow(QWidget):
	
	# init the class - set up our window
	def  __init__(self):
		#initialize the QWidget class for use here
		super(MyWindow, self).__init__()

		#clear the console message for this run
		# App.Console.Clear()

		# print a console debug message		
		App.Console.PrintMessage("\nQWidget initialized\n")
		
		#create a button		
		self.btnLoad = QPushButton("Load Points")

		#connect the clicked method
		#self.btnLoad.clicked.connect(self.btnClick)

		#create a layout			
		layout = QVBoxLayout()

		#add the button to the layout
		layout.addWidget(self.btnLoad)

		#add the layout to the window
		self.setLayout(layout)

		#make the window stay on top
		self.setWindowFlags(Qt.WindowStaysOnTopHint)	

		#center the window
		#????
		
		#show the window
		self.show()		
		
		
# create an instance of the new class
# MyWindow.__init__ will run, starting the application
app = MyWindow()	

wingman
Posts: 36
Joined: Sun Feb 09, 2020 12:28 pm

Re: Center a QWidget window in the FreeCAD window ?

Post by wingman »

I solved it.

Code: Select all

		#center the window
		mainWin = FreeCAD.Gui.getMainWindow()
		#App.Console.PrintMessage(mainWin.geometry().center())
		self.move(mainWin.geometry().center())
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Center a QWidget window in the FreeCAD window ?

Post by openBrain »

wingman wrote: Fri Oct 01, 2021 11:20 pm I solved it.
Please do not think it as being insulting, but this is horrible code. :)
You can have a look here to properly set a non modal widget on top of FreeCAD main windows : https://github.com/0penBrain/FreeCAD-ma ... og.FCMacro
wingman
Posts: 36
Joined: Sun Feb 09, 2020 12:28 pm

Re: Center a QWidget window in the FreeCAD window ?

Post by wingman »

I get that your code is more "correct", but I'm building a quick and dirty macro. If it runs 10x properly, I'm happy.

My code is loosely based upon a FreeCAD macro YouTube video. https://www.youtube.com/watch?v=asLTXzkFjLY

Maybe you should make a video that explains your code decisions ?

That example is a very interesting piece of code. Way more rigorous than what a lot of the code I've seen in FreeCAD macros. What does it gain over the quick and dirty methods in my code ?

What is the functional difference between using slots, messages and observers and using this:

Code: Select all

	self.btnLoad.clicked.connect(self.btnClick)
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Center a QWidget window in the FreeCAD window ?

Post by openBrain »

wingman wrote: Sun Oct 03, 2021 7:59 pm Maybe you should make a video that explains your code decisions ?
I actually plan to have a wiki page, but not managed to get time up to now.
That example is a very interesting piece of code. Way more rigorous than what a lot of the code I've seen in FreeCAD macros. What does it gain over the quick and dirty methods in my code ?
* The QMainWindow creates another window which appears in your task bar, which my method doesn't. Looks more proper. :) Especially it will automatically minimize with the FreeCAD main window.
* The QMainWindow creates another event loop, which can run you into troubles if main window is closed or other events
* The StayOnTopHint makes your window stay over all windows of your desktop (not only FreeCAD). Can be a pain.
* My method specifically tells Qt that the dialog is a child of the FreeCAD main window, so it will ensure it appears correctly centered without having to play with geometry (which isn't a good idea generally).
What is the functional difference between using slots, messages and observers and using this:

Code: Select all

	self.btnLoad.clicked.connect(self.btnClick)
* Signal/slot is a internal mechanism of notification managed by Qt (Boost uses a similar concept). It has the advantage that it is very versatile and safe (several slots can subscribe to same signal, Qt will ensure that connection is removed if slot object is deleted, signals can be altered and delayed, ...)
* Callback is a very simple and inexpensive method, but it is limited and you have to pay attention to not call a deleted callback. Works well in simple cases.
* Observers are a king of enhanced callbacks. While callbacks are single functions, when you register an observer you actually register a class instance where the caller will be able to call different methods according the context. Unlike signals, method names and signatures are fixed.
Post Reply