0

I am adding a items to listwidget but strangely the first block of if adds icon while the second one doesnt i also checked that the icon is in place in the specified location

def addToRenderQueue(self):
    ext=os.path.splitext(str(self.scnFilePath.text()))[-1]
    if self.mayachkBox.isChecked() and (ext=='.ma'):
       img_mIcon=QtGui.QPixmap("images\icon_maya-small.png")
       ntask=self.makeBatTask()
       self.itemTask=QtGui.QListWidgetItem(ntask)
       self.itemTask.setIcon(QtGui.QIcon(img_mIcon))
       self.listWidget.insertItem(0,self.itemTask)
    elif self.nukechkBox.isChecked() and (ext=='.nk'):
         img_nIcon=QtGui.QPixmap("images\nuke.png")
         ntask=self.makeBatTask()
         self.itemTask=QtGui.QListWidgetItem(ntask)
         self.itemTask.setIcon(QtGui.QIcon(img_nIcon))
         self.listWidget.insertItem(0,self.itemTask)

for elif block ntask contains this kind of string: Nuke6.1.exe -t E:\Dropbox\Research_Study\myprojects\Batch\nukeRender.py Write2 E:/Dropbox/Research_Study/myprojects/Batch/test_project_nuke/sign_board.nk 1 16 1 test

1 Answers1

1

In Python strings, \ is escape character that is used for special characters like new-line (\n), tab (\t), etc. In your second path, "images\nuke.png", you have \n so it is parsed as "images<new-line>uke.png" and PyQt can't find that file.

You have a couple of options:

  • You can escape the \ itself: "images\\nuke.png"
  • You can use a raw string, which disables escaping: r"images\nuke.png"
  • You can use / for folder separator: "images/nuke.png"

I prefer third option. Windows accepts both \ and / for folder separator. Besides in Qt, using / is the preferred way since it is automatically translated to the appropriate system separator:

Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.

Avaris
  • 35,883
  • 7
  • 81
  • 72
  • no man i thought this should fix but i tried all the options and icon still didnt showed up... –  Dec 14 '12 at 21:01
  • forgot to mention even if use the icon used in the first if block in the lower else block that too doesnt shows up though if "if" block is executed it shows up for it. –  Dec 14 '12 at 21:02
  • @san: Are you sure the `elif` block is executed? – Avaris Dec 15 '12 at 03:24
  • 1
    yes 100 % because the text generated by elif block gets added..see this [link](http://www.youtube.com/watch?v=RgjbHz5o68w&t=3m5s&hd=1) –  Dec 15 '12 at 11:04