[PyQt] Question about the segmentation fault induced by uic.loadUiTyp

Baz Walter bazwal at gmail.com
Mon Mar 19 18:41:15 GMT 2018


On 19/03/18 09:35, lzf neu wrote:
> The whole script that I referenced from the website is as follows:
> http://pythonforengineers.com/your-first-gui-app-with-python-and-pyqt/
> ######################################################
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
> 
> import sys
> from PyQt4 import QtCore, QtGui, uic
> 
> qtCreatorFile = "a.ui"  # a.ui is in my case
> 
> Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
> 
> class MyApp(QtGui.QMainWindow, Ui_MainWindow):
>      def __init__(self):
>          QtGui.QMainWindow.__init__(self)
>          Ui_MainWindow.__init__(self)
>          self.setupUi(self)
> 
>      def CalculateTax(self):
>          price=int(self.price_box.toPlainText())
>          tax=(self.tax_rate.value())
>          total_price=price+((tax/100)*price)
>          total_price_string="The total price with tax is: " + str(total_price)
>          self.results_window.setText(total_price_string)
> 
> if __name__ == "__main__":
>      app = QtGui.QApplication(sys.argv)
>      window = MyApp()
>      window.show()
>      self.calc_tax_button.clicked.connect(self.CalculateTax)
>      sys.exit(app.exec_())
> ###########################################################
> 
> I run the whole example script and the terminal still shows:  Segmentation fault (core dumped)
> 
> Then I run the script line by line and found that the error occured at the line:  Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

The only thing wrong with that script is the line that attempts to make 
a signal/slot connection. That will obviously raise a NameError because 
self is not defined in that scope. So it needs to go in the __init__ 
method of the MyApp class.

When I fix that, the script runs perfectly fine for me (using qt 4.8.7 
and pyqt 4.12.1, with either python 3.6.4 or python 2.7.14). Contrary to 
what others have said, it is not necessary to create a QApplication 
before calling uic.loadUiType. All that does is use exec to load a 
module and return a class object - it does not create an instance of 
anything.

Are you using the *official* CentOS qt4, pyqt4 and python2 packages? Or 
did you install some or all of them in some other way? And how exactly 
are you running the script? Are you doing it via some kind of IDE, or 
just using a normal console?


More information about the PyQt mailing list