简体   繁体   中英

vtkOBJReader dosen't work inside qt main loop

I have a strange issue with reading OBJ file using VTK in Python. Code example below works fine for me.

reader = vtk.vtkOBJReader()
reader.SetFileName('cube.obj')
reader.Update()
inputP = reader.GetOutput()

app = QtGui.QApplication(sys.argv)

window = MainWindow(inputP)

sys.exit(app.exec_())

but if I first initialize QApplication , then vtkOBJReader throw an error message:

ERROR: In /build/vtk/src/VTK-6.1.0/IO/Geometry/vtkOBJReader.cxx, line 192 vtkOBJReader (0x56396fd14fa0): Error reading 'v' at line 5

Example code that does not work is shown below:

app = QtGui.QApplication(sys.argv)

reader = vtk.vtkOBJReader()
reader.SetFileName('cube.obj')
reader.Update()
inputP = reader.GetOutput()

window = MainWindow(inputP)

sys.exit(app.exec_())

I have the same issue if I wrote this program in C++. Have you any suggestions, how to force vtkOBJReader to work inside QT app?

i had the same exact problem and studying the code from vtkObjectReader (link: https://github.com/Kitware/VTK/blob/master/IO/Geometry/vtkOBJReader.cxx#L264 ) you can see the error message is produced inside this snippet:

// this is a vertex definition, expect three floats, separated by whitespace:
if (sscanf(pLine, "%f %f %f", xyz, xyz+1, xyz+2) == 3)
  {
    points->InsertNextPoint(xyz);
    numPoints++;
  }
  else
  {
    vtkErrorMacro(<<"Error reading 'v' at line " << lineNr);
    everything_ok = false;
  }

the problem is due to sscanf giving a different output if it is called before or after qt initialization. With this information i found the solution here: Why does Qt change behaviour of sscanf()?

With qt the locale used to parse the line is the same of your system (mine is italian so it was expecting "," as a decimal divider not a "." so it failed to recognize the float type).

if you change the locale after the definition of you QApplication it works as intended, ie:

QApplication a(argc, argv);
setlocale(LC_NUMERIC,"C");

Well, it may help someone. Load the object file before initialize the QtGui.QApplication(sys.argv).

In python, this problem can be solved by changing the locale after the definition of you QApplication:

app = QApplication(sys.argv)
locale.setlocale(locale.LC_NUMERIC, 'C')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())

This resource may help you: https://docs.python.org/2/library/locale.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM