Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top