The general idea of what you are doing is correct. You have a more generalized 
SLOT function that can receive arguments telling it how to operate on different 
targets or whatnot.
There are different ways, I am sure, of doing the actual conditional logic 
within that SLOT. It could delegate out to other private methods if you want, 
or simply do the action right there in the code as you are doing it. The 
optimal thing would be to make sure it is DRY (Dont Repeat Yourself), so that 
you don't have 10 conditions all performing duplicate functionality. Though you 
are not doing wrong now, since each condition has different things to call, 
with different checks to made, with different widgets to set result values on.

Anyways. What you are doing now is fine. One way of making it a bit cleaner is 
to use some constants for your action types instead of plain literal strings:

class MyClass(QWidget):
    SCENE_FILE_TYPE = "scnFile" # or just 0
    RENDER_DIR_TYPE = "rdPath  # or just 1

    ...
    def foo(self):
        scnFile = partial(self.showFileDialog, self.SCENE_FILE_TYPE)
        rndPath = partial(self.showFileDialog, self.RENDER_DIR_TYPE)
    ...
    def showFileDialog(self, typ, *args, **kwargs):
        if typ == self.SCENE_FILE_TYPE:
            ...
        elif typ == self.RENDER_DIR_TYPE:
            ...
 
It is not necessary to do this, but you were asking for extra tips.



On Dec 8, 2012, at 9:55 AM, šãñ wrote:

> sorry i forgot to mention the question for my last post about slots..
> 
> I am calling same slots with different button by passing a value and checking 
> what value it is and executing the relevant part in the showFileDialog 
> method.. is it the right way to do becuase this does work but I guess their 
> is a better way of doing it ?
> 
> 
> On Sat, Dec 8, 2012 at 11:07 PM, Justin Israel <[email protected]> wrote:
> Unless you specifically change settings on SIP, a QString is returned for all 
> Qt methods that return a "string" results. You have to convert it to a python 
> string if you want to treat it as such:
> 
> self.scnFilePath.setText(os.path.normpath(str(fname)))
> 
> 
> On Dec 8, 2012, at 9:01 AM, šãñ wrote:
> 
>> I have two browse button in the GUI as uprobably say in last image atachment
>> 
>> so instead of creating two different slot for each button click i 
>> implemented it this way !!
>>           # assign slots to buttons
>>           scnFile=partial(self.showFileDialog,"scnFile")
>>           self.browseBtn.clicked.connect(scnFile)
>>           rndPath=partial(self.showFileDialog,"rdPath")
>>           self.rdBtn.clicked.connect(rndPath)
>> 
>> 
>> and the showFileDialog() method as as under:
>>     def showFileDialog(self,*args):
>>         directory=os.path.expanduser('~')
>>         if args[0]=="scnFile":
>>             fname=QtGui.QFileDialog.getOpenFileName(self,'Open 
>> File',directory)
>>             if os.path.isfile(fname):
>>                 self.scnFilePath.setText(fname)
>>         elif args[0]=="rdPath":
>>             selectedDir=QtGui.QFileDialog.getExistingDirectory(self,"Select 
>> Render Directory",directory)
>>             if selectedDir.isEmpty():
>>                 print "No directory selected"
>>             else:
>>                 self.renDir.setText(selectedDir)
>> 
>> also if i try to do self.scnFilePath.setText(os.path.normpath(fname))
>> 
>> i get error saying 
>> Traceback (most recent call last):
>>   File "RenderUI.py", line 203, in showFileDialog
>>     if os.path.isfile(os.path.normpath(fname)):
>>   File "D:\Python26\lib\ntpath.py", line 417, in normpath
>>     if path.startswith("\\"):
>> AttributeError: 'QString' object has no attribute 'startswith'
>> 
>> 
>> 
>> 
>> On Sat, Dec 8, 2012 at 4:12 AM, Justin Israel <[email protected]> wrote:
>> 1) Just set your label to have a fixed with (setFixedWidth), or make your 
>> other widgets size policies "expanding" so they take up any new space. 
>> Probably the first one though.
>> 
>> 2) If you want an asynchronous result from your filedialog, then instead of 
>> using my static getExistingDirectory() suggestion, just stick with creating 
>> the instance like you were. If you stay with calling the exec_() on the 
>> dialog, you are waiting for a response anyways so you can just set the value 
>> on to the text box. But for the async approach, you would show() instead of 
>> exec_() and maybe connect the accept() signal to a slot that will check the 
>> value and enter it into the textedit.
>> For more specific help, I would need to see an example of what you are 
>> trying.
>> 
>> 3) Why would so many people recommend not using list comprehension? Just 
>> flat out not to use it? They are a great way to get more performance than a 
>> normal for loop. I can see there are misuses of them though.
>> For your specific example, you would usually not want to use a list 
>> comprehension for side effects. That is, you shouldn't use them to append or 
>> call some method in a loop. They are used to produce results that you intend 
>> to keep.
>> There really isn't much you can do to change your example. Qt would have 
>> needed to provide an  layout.addWidgets(aList) to accept a list of widgets. 
>> So you just need to call them in order like you are doing.
>> 
>> 
>> On Fri, Dec 7, 2012 at 1:10 PM, šãñ <[email protected]> wrote:
>> 
>> thank you guys for pointing a solution but Now I really have a bunch of more 
>> questions these are pretty basic that i know ...
>> 
>> my first question is how do I keep the gridColumWidth fixed so that it 
>> should not change if the label text changes(see attached image)
>> 
>> my second question is how can i use a showFileDialog slot fill QLineEdit for 
>> scne file for selecting directory and similarly in second tab ..(as you can 
>> see in the attached screenshot I have so many browse buttons)
>> I remember seeing in the PyQt tutorial where you made use of partial but i 
>> guess that was for removeCallable from OM, but now how should i find 
>> QPushButton.objectName to be passed to slot so depending on what button is 
>> pressed i can perform some operation...
>> 
>> thirdly , so many people have recommended me not to use List comprehension 
>> but i find it convenient(at least here) to use while addWidget or 
>> addLayout...
>> like [p3_HBox2.addWidget(each) for each in 
>> [self.pylbl,self.pyEdt,self.pybrwBtn]]  
>> 
>> but I have a situation when I am using a combination of addWidget 
>> ,addStretch and addLayout 
>> 
>> like in this case
>>  
>>           p1_vertical.addLayout(gridLayout)
>>           p1_vertical.addLayout(gridLayout2)
>>           p1_vertical.addLayout(hbox)
>>           p1_vertical.addWidget(hLine)
>>           p1_vertical.addLayout(nxtbtnbox)
>> 
>> in that case how should i after adding hbox in LC i can add addWidget(hLine) 
>> then again nxtbtnbox, i feel like i can use it with enumerate but is their 
>> any better option? 
>> 
>>   
>> 
>> 
>> On Sat, Dec 8, 2012 at 12:30 AM, Justin Israel <[email protected]> 
>> wrote:
>> If you are using your own custom status bar in a widget, then you have to 
>> connect focus or hover events to something that sets the message on the 
>> status bar. You can set the setStatusTip("") on each widget and then just 
>> read that.
>> 
>> When you use a QMainWindow, the status bar is already wired up for you so 
>> that any time you hover a widget, the statusBar will automatically read the 
>> widgets statusTip():
>> 
>> app = QtGui.QApplication([])
>> 
>> w = QtGui.QMainWindow()
>> button = QtGui.QPushButton("clicky", w)
>> button.setStatusTip("I am a clicky")
>> w.setCentralWidget(button)
>> 
>> status = w.statusBar()
>> 
>> w.show()
>> w.raise_()
>> 
>> status.showMessage("Welcome to the app!", 5000)
>> 
>> app.exec_()
>> 
>> For the file dialog, the way you are using it, there is a convenience static 
>> method you can call:
>> http://qt-project.org/doc/qt-4.8/qfiledialog.html#getExistingDirectory
>> 
>> It will return an empty string if they cancel:
>> 
>> directory=os.path.expanduser('~')
>> selectedDir = QtGui.QFileDialog.getExistingDirectory(w, 
>>                 "Select Render Directory", directory)
>> 
>> if selectedDir.isEmpty():
>>     print "No directory selected"
>> else:
>>     print selectedDir
>> 
>> 
>> On Fri, Dec 7, 2012 at 9:56 AM, Kurian O.S <[email protected]> wrote:
>> For Directory you want to use exec so you need to change your code like this
>> 
>>     def showDirDialog(self):
>>         directory=os.path.expanduser('~')
>>         dirPath=QtGui.QFileDialog(self,"Select Render Directory",directory)
>>         dirPath.setFileMode(QtGui.QFileDialog.DirectoryOnly)
>>         if dirPath.exec_() == 1:
>>             fname_list = str(dirPath.selectedFiles()[0])
>>             print fname_list
>> 
>> For changing the statusBar text you need to implement focusIn and out Event 
>> and Justin already mention about that in reimplement QPlainText’s 
>> focusInEvent in another source file ,how to ? thread.
>> 
>> 
>> On Fri, Dec 7, 2012 at 6:25 AM, san <[email protected]> wrote:
>> as a part of my learning PyQt4 I started making a gui(see attachment) where 
>> I implement all the examples I learn from video tuts or book I am referring,
>> 
>> however I count figure out how should I implement the method to show text 
>> message on the status bar when user hover mouse over a widget[link to 
>> pastebin ]
>> 
>> and secondly I dont understand how come this method to select Directory is 
>> not working?
>> 
>>     def showDirDialog(self):
>>         directory=os.path.expanduser('~')
>>         dirPath=QtGui.QFileDialog(self,"Select Render Directory",directory)
>>         dirPath.setFileMode(QtGui.QFileDialog.DirectoryOnly)
>> 
>> 
>> -- 
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings: 
>> http://groups.google.com/group/python_inside_maya/subscribe
>> 
>> 
>> 
>> -- 
>> --:: Kurian ::--
>> 
>> 
>> -- 
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings: 
>> http://groups.google.com/group/python_inside_maya/subscribe
>> 
>> 
>> -- 
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings: 
>> http://groups.google.com/group/python_inside_maya/subscribe
>> 
>> 
>> 
>> -- 
>>  
>> 
>> 
>> 
>> 
>> 
>> -- 
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings: 
>> http://groups.google.com/group/python_inside_maya/subscribe
>> 
>> 
>> -- 
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings: 
>> http://groups.google.com/group/python_inside_maya/subscribe
>> 
>> 
>> 
>> -- 
>> 
>> 
>> 
>> 
>> 
>> 
>> -- 
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings: 
>> http://groups.google.com/group/python_inside_maya/subscribe
> 
> 
> -- 
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings: 
> http://groups.google.com/group/python_inside_maya/subscribe
> 
> 
> 
> -- 
> 
> 
> 
> 
> 
> 
> -- 
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings: 
> http://groups.google.com/group/python_inside_maya/subscribe

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to