Re: [Tutor] Fwd: QT Python: How to re-use the return value of fileDialog.openFileName() ?
Thank you, for the details. On Mon, May 27, 2013 at 10:13 AM, eryksun wrote: > On Mon, May 27, 2013 at 9:45 AM, Matthew Ngaha > wrote: > > On Mon, May 27, 2013 at 2:14 PM, SM wrote: > > > >>But then I also had to use self.fileDialog from within the function. Not > >> sure how I could avoid using fileDialog. > >> > > > > No problem. to do it without the instance variable, you access its > > method directly. so replace: > > > > path = self.fileDialog.getOpenFileName() > > > > with > > > > path = QFileDialog.getOpenFileName(self) > > > > which is basically the same thing except you're going to the method > > directly instead of creating a class instance. The self argument may > > or may not be needed, its been a while since ive used Qt. > > getOpenFileName is a static method. The first parameter is the > [optional] parent widget. The documentation says the dialog should be > centered over the parent, but it probably won't be if you let it use > the default native dialog. This can be disabled with the option > QFileDialog.DontUseNativeDialog. For example: > > path = QtGui.QFileDialog.getOpenFileName( > parent=self, > caption='File Dialog', > directory='path/to/open', > filter='type1 (*.ext1);;type2 (*.ext2)', > selectedFilter='type2 (*.ext2)', > options=QFileDialog.DontUseNativeDialog, > ) > > http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#getOpenFileName > http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#Option-enum > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: QT Python: How to re-use the return value of fileDialog.openFileName() ?
On Mon, May 27, 2013 at 9:45 AM, Matthew Ngaha wrote: > On Mon, May 27, 2013 at 2:14 PM, SM wrote: > >>But then I also had to use self.fileDialog from within the function. Not >> sure how I could avoid using fileDialog. >> > > No problem. to do it without the instance variable, you access its > method directly. so replace: > > path = self.fileDialog.getOpenFileName() > > with > > path = QFileDialog.getOpenFileName(self) > > which is basically the same thing except you're going to the method > directly instead of creating a class instance. The self argument may > or may not be needed, its been a while since ive used Qt. getOpenFileName is a static method. The first parameter is the [optional] parent widget. The documentation says the dialog should be centered over the parent, but it probably won't be if you let it use the default native dialog. This can be disabled with the option QFileDialog.DontUseNativeDialog. For example: path = QtGui.QFileDialog.getOpenFileName( parent=self, caption='File Dialog', directory='path/to/open', filter='type1 (*.ext1);;type2 (*.ext2)', selectedFilter='type2 (*.ext2)', options=QFileDialog.DontUseNativeDialog, ) http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#getOpenFileName http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#Option-enum ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: QT Python: How to re-use the return value of fileDialog.openFileName() ?
Yes, the following works. path = QtGui.QFileDialog.getOpenFileName() Thanks! On Mon, May 27, 2013 at 9:45 AM, Matthew Ngaha wrote: > On Mon, May 27, 2013 at 2:14 PM, SM wrote: > > >But then I also had to use self.fileDialog from within the function. Not > > sure how I could avoid using fileDialog. > > > > Thanks. > > No problem. to do it without the instance variable, you access its > method directly. so replace: > > path = self.fileDialog.getOpenFileName() > > with > > path = QFileDialog.getOpenFileName(self) > > which is basically the same thing except you're going to the method > directly instead of creating a class instance. The self argument may > or may not be needed, its been a while since ive used Qt. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: QT Python: How to re-use the return value of fileDialog.openFileName() ?
On Mon, May 27, 2013 at 2:14 PM, SM wrote: >But then I also had to use self.fileDialog from within the function. Not > sure how I could avoid using fileDialog. > > Thanks. No problem. to do it without the instance variable, you access its method directly. so replace: path = self.fileDialog.getOpenFileName() with path = QFileDialog.getOpenFileName(self) which is basically the same thing except you're going to the method directly instead of creating a class instance. The self argument may or may not be needed, its been a while since ive used Qt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: QT Python: How to re-use the return value of fileDialog.openFileName() ?
Thanks! I defined a function, as you suggested, to call when the button was clicked. But then I also had to use self.fileDialog from within the function. Not sure how I could avoid using fileDialog. I also defined global variable (under the class) and assigned it to the filename, so I could access it from within the main class. The following works: QtCore.QObject.connect(self.toolButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.getFileName) def getFileName(self): self.fileDialog = QtGui.QFileDialog() path = self.fileDialog.getOpenFileName() print(" File Selected: ", path) self.lineEdit.setText(path) self.XmlFileName = path Thanks. On Mon, May 27, 2013 at 6:01 AM, Matthew Ngaha wrote: > Sorry for the forward, i forgot to reply to tutor > > On Mon, May 27, 2013 at 3:53 AM, Sunitha Misra > wrote: > > > self.fileDialog = QtGui.QFileDialog() > > > > QtCore.QObject.connect(self.toolButton, > > QtCore.SIGNAL(_fromUtf8("clicked()")), self.fileDialog.getOpenFileName) > > > > i think the way youre doing it won't allow you to get the filename as > you are calling it through a signal. > > Things you can do: > > 1) dont use self.fileDialog.getOpenFileName as a slot like you are > doing. Create a function and call it when the toolbar's button is > clicked, then in that function assign the filename to the return value > of self.fileDialog.getOpenFileName. > > 2) if the only thing your self.fileDialog is doing is responding to > the toolbar's button, don't create it at all. do what i said in #1 and > assign a filename variable to the full method of the dialog. What im > saying is there might be no need to create the self.fileDialog > instance variable at all. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: QT Python: How to re-use the return value of fileDialog.openFileName() ?
Sorry for the forward, i forgot to reply to tutor On Mon, May 27, 2013 at 3:53 AM, Sunitha Misra wrote: > self.fileDialog = QtGui.QFileDialog() > > QtCore.QObject.connect(self.toolButton, > QtCore.SIGNAL(_fromUtf8("clicked()")), self.fileDialog.getOpenFileName) > i think the way youre doing it won't allow you to get the filename as you are calling it through a signal. Things you can do: 1) dont use self.fileDialog.getOpenFileName as a slot like you are doing. Create a function and call it when the toolbar's button is clicked, then in that function assign the filename to the return value of self.fileDialog.getOpenFileName. 2) if the only thing your self.fileDialog is doing is responding to the toolbar's button, don't create it at all. do what i said in #1 and assign a filename variable to the full method of the dialog. What im saying is there might be no need to create the self.fileDialog instance variable at all. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor