Capture inputs from PyQt dialog

88 views
Skip to first unread message

kiteh

unread,
Aug 23, 2018, 5:06:25 PM8/23/18
to Python Programming for Autodesk Maya
I have created my tool using PyQt. Things are working correctly and I am encountering 2 issues.

1. This is more of an implementation - I am trying to capture the input state of the widgets that User has made within the tool. Eg. User has written something in one of the many QLineEdits, checked some options in the checkboxes etc. How can I 'save' and load these inputs?

2. Since converting my tool to use PyQt, I came to notice that 'undo-ing' functionality does not works, only the actions that my tool has executed on. Is this a known issue if one changes from using maya.cmds gui to pyqt?


Justin Israel

unread,
Aug 23, 2018, 5:17:11 PM8/23/18
to python_in...@googlegroups.com
On Fri, Aug 24, 2018 at 9:06 AM kiteh <kiteh...@gmail.com> wrote:
I have created my tool using PyQt. Things are working correctly and I am encountering 2 issues.

1. This is more of an implementation - I am trying to capture the input state of the widgets that User has made within the tool. Eg. User has written something in one of the many QLineEdits, checked some options in the checkboxes etc. How can I 'save' and load these inputs?

Use QSettings to save and load the state of your application.
This means you have some point in your app, either as things change or once on a close or hide event, where you save the state to your QSettings (can be created on the fly). And then you have a point when you first initialize your app that you load them back from the QSettings.
 

2. Since converting my tool to use PyQt, I came to notice that 'undo-ing' functionality does not works, only the actions that my tool has executed on. Is this a known issue if one changes from using maya.cmds gui to pyqt?

Maya has no way to know what you are doing within your Qt widgets. It only knows what you do when you call into its cmds API. So you would need to make use of cmds.undoInfo or the api MDGModifier if you are talking about controlling Maya operations. But if you are talking about undoing operations to your custom UI, then you would have to look at having your own undo:
http://doc.qt.io/archives/qt-4.8/qundo.html
 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9b86a5e5-2b34-4a98-be14-9cb18929615a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kiteh

unread,
Aug 23, 2018, 6:04:14 PM8/23/18
to Python Programming for Autodesk Maya
Hi Justin, glad to know that using QSettings is the way to go... As I am not getting the expected output, I must have done something wrong..
I was able to save the data, but when it comes to the loading part, no values are loaded despite it being present in the file contents if I am running the whole command. And hence, I thought I may have did something wrong..

This is a quick UI code that I have written up:
class SampleUI(QtGui.QWidget):
   
def __init__(self, parent=None):
       
super(SampleUI, self).__init__(parent)
       
self.init_ui()
       
self.settings = QtCore.QSettings()
       
print(self.settings.fileName())
       
# Load the settings into the UI
       
self.load()

   
def init_ui(self):
        layout
= QtGui.QVBoxLayout(self)
        lineEdit1
= QtGui.QLabel("Input Field")
       
self.lineEdit2 = QtGui.QLineEdit()
       
self.lineEdit2.setObjectName("lineEdit2")

        layout
.addWidget(lineEdit1)
        layout
.addWidget(self.lineEdit2)

   
def load(self):
        settings
= self.settings
       
print '>>> ', settings.value('lineEdit2')

   
def closeEvent(self, event):
       
print 'UI is closed'
        settings
= self.settings
        settings
.setValue('lineEdit2', self.lineEdit2.text())

And this is how I have coded my shelf button:
import sample
my_code
= sample.SampleUI()
my_code
.show()

If I am running the shelf code the first time round, eg. inputting in "for testing" and closing the application, it saves that string.
If I am relaunching the tool the second time round, with the shelf code, while it is printing out the string values in the Script Editor, it is not showcase in the field.
However, if I only run the code `my_code.show()` (not via the shelf code), it will then showcase the string in the field.

Justin Israel

unread,
Aug 23, 2018, 6:59:55 PM8/23/18
to python_in...@googlegroups.com
Firstly, make sure you are setting an organization and an application name, so that you don't collide with another Qt application:
http://doc.qt.io/archives/qt-4.8/qsettings.html#QSettings

The reason you aren't seeing your saved text being loaded to your UI is because you forgot to actually setText the value:

def load(self):
        settings = self.settings
        print '>>> ', settings.value('lineEdit2')
        self.lineEdit2.setText(settings.value('lineEdit2', ''))

Note the second arguments to value() is the default to use if the setting wasn't not previously set.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

kiteh

unread,
Aug 23, 2018, 9:24:21 PM8/23/18
to Python Programming for Autodesk Maya
Got it, thanks! Had thought that there may not be a need to perform another `setText()
I have got another question.. sorry for the mass spam.

I tried following what you have mentioned and do the same for my other widgets - checkbox and radiobuttons.. 
eg. self.checkbox01.setChecked(settings.value('check_value', False))

As soon as I tried relaunching my tool in a new session (no previous saved states), it errors out and I was presented with the error 
self.checkbox01.setChecked(settings.value('checking', False))
# TypeError: 'PySide2.QtWidgets.QAbstractButton.setChecked' called with wrong argument types:
#   PySide2.QtWidgets.QAbstractButton.setChecked(unicode)
# Supported signatures:
#   PySide2.QtWidgets.QAbstractButton.setChecked(bool) #

Going by the error, I have tried using `bool(0)`, `bool(False)` etc, but the error persists.
Is PyQt handling this differently?

Justin Israel

unread,
Aug 23, 2018, 10:09:35 PM8/23/18
to python_in...@googlegroups.com
PyQt has always had a 'bug' in how it serialized bool values. It writes them as a string and reads them back that way. PySide seems to do it correctly. What you should do is:

s.setValue('field', int(val))
bool(s.value('field', False))

If you coerce them to and from int, you will be fine.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

kiteh

unread,
Aug 24, 2018, 7:18:10 PM8/24/18
to Python Programming for Autodesk Maya
Noted with thanks!
Reply all
Reply to author
Forward
0 new messages