signal emit in preferences pages

A forum for research and development of the user interface of FreeCAD
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
ebrahim raeyat
Posts: 619
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

signal emit in preferences pages

Post by ebrahim raeyat »

Is there a way to emit signal when we change a widget on preferences pages? for example when we uncheck a checkbox, we able to disable some widget? I mean before press apply. Thanks.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: signal emit in preferences pages

Post by openBrain »

A checkbox natively emit signals when changes, such as 'stateChanged', 'clicked' or 'toggled'. ;)
User avatar
ebrahim raeyat
Posts: 619
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit in preferences pages

Post by ebrahim raeyat »

openBrain wrote: Sun Jul 31, 2022 6:01 am A checkbox natively emit signals when changes, such as 'stateChanged', 'clicked' or 'toggled'. ;)
I know, but how can I use it with preferences dialog? I did not find any resource. I can do it with regular widget.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: signal emit in preferences pages

Post by openBrain »

ebrahim raeyat wrote: Sun Jul 31, 2022 4:51 pm I know, but how can I use it with preferences dialog? I did not find any resource. I can do it with regular widget.
Maybe you should try to tell what you're functionnally trying to achieve, not technically. A checkbox is a checkbox, being it a single widget or enclosed in the preferences dialog.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: signal emit in preferences pages

Post by heda »

"I did not find any resource."

you just have to go on a treasure hunt for the actual checkbox widget, starting from the mainwindow...

there are some examples on the forum of how to get hold of dialogue widgets, that's will take you quite far in narrowing things down.
User avatar
ebrahim raeyat
Posts: 619
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit in preferences pages

Post by ebrahim raeyat »

openBrain wrote: Sun Jul 31, 2022 7:29 pm
Maybe you should try to tell what you're functionnally trying to achieve, not technically. A checkbox is a checkbox, being it a single widget or enclosed in the preferences dialog.
For example I have a checkbox in my widget. when I create my own widget, I set signal & slot in the __init__ of my class:

Code: Select all

self.form.checkbox.clicked.connect(self.checkbox_clicked)
but for preferences page, I added my preferences page to preferences dialog. user run the dialog from Edit/preferences and change the state of the checkbox. how can I found when user click on checkbox? I don't access to the preferences page? where can I set signal & slot for preferences page.

for example an observer like this. but this works when user click apply or ok in preferences page, I want signal when user click on checkbox:

https://forum.freecadweb.org/viewtopic. ... 14#p565614
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: signal emit in preferences pages

Post by openBrain »

ebrahim raeyat wrote: Mon Aug 01, 2022 1:03 am for example an observer like this. but this works when user click apply or ok in preferences page, I want signal when user click on checkbox:

https://forum.freecadweb.org/viewtopic. ... 14#p565614
This is Python. Are you also talking about using Python?
User avatar
ebrahim raeyat
Posts: 619
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit in preferences pages

Post by ebrahim raeyat »

openBrain wrote: Mon Aug 01, 2022 6:45 am
This is Python. Are you also talking about using Python?
Yes. suppose you have two combobox, one for country and one for city. I want to able to when user change the country, I fill the city combobox with respect to country, then user select the city. How can I do it with python in preferences dialog? Thanks.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: signal emit in preferences pages

Post by heda »

hm, the only difference with you making a widget and attaching a signal to it, and the pref widget is that you have the reference to your own widget...

so, treasure hunting for the correct widget (which is somewhere under mainwindow) is a way forward (the only?)

pref d is modal, so if the dialogue is up it has to be done through a thread, cannot remember if the widgets are persistent or not (if not then the widgets are simply not there if the dialogue is not up...)

this is hooking into the pref d and simulates clicking, so you can easily start from there and find your widget - and it is a treasure hunt..., printing widgets is your friend when drilling down the w hierarchy

https://forum.freecadweb.org/viewtopic. ... 12#p480412
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: signal emit in preferences pages

Post by openBrain »

ebrahim raeyat wrote: Mon Aug 01, 2022 7:12 am Yes. suppose you have two combobox, one for country and one for city. I want to able to when user change the country, I fill the city combobox with respect to country, then user select the city. How can I do it with python in preferences dialog? Thanks.
Here are the basics. Obviously "self.form" can be initialized from a UI file.
This snippet will add a page (called "TestPage") in FC preferences with 2 linked comboboxes.

Code: Select all

import FreeCADGui
from PySide2 import QtWidgets

class TestPrefPage:

	def __init__(self, parent=None):
		self.form = QtWidgets.QWidget(parent)
		lay = QtWidgets.QVBoxLayout()
		self.source = QtWidgets.QComboBox(self.form)
		self.target = QtWidgets.QComboBox(self.form)
		self.source.currentIndexChanged.connect(self.sourceChanged)
		self.source.addItems(["Letters", "Numbers"])
		lay.addWidget(self.source)
		lay.addWidget(self.target)
		self.form.setLayout(lay)

	def sourceChanged(self, index):
		strList = []
		self.target.clear()
		if index == 0:
			strList = ["A", "B", "C"]
		elif index == 1:
			strList = ["1", "2", "3"]
		self.target.addItems(strList)

	def saveSettings(self):
		pass

	def loadSettings(self):
		pass

FreeCADGui.addPreferencePage(TestPrefPage, "TestPage")
HTH
Post Reply