Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My First Qt App
#1
Hi all,

I am just starting to look into how to use Qt4 with Python 3.

I found an example that gives a skeleton Python file that I can use, but i'm getting a strange error Huh . Can anyone help?

PS. I'm using a Raspberry Pi2B, running Python 3.4.2 with Qt4. I tried adding an attachment of the Qt form but it doesn't seem to working on this browser.


import sys
from PyQt4 import QtCore, QtGui, uic

qtCreatorFile = "TestForm.ui" # Enter file here.

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)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
Error:
Traceback (most recent call last): File "/home/pidev/Projects/Qt/PyQt_Test.py", line 16, in <module> window = MyApp() File "/home/pidev/Projects/Qt/PyQt_Test.py", line 12, in __init__ self.setupUi(self) File "<string>", line 34, in setupUi AttributeError: 'MyApp' object has no attribute 'accept'
Reply
#2
Hi there,
We can't tell about the error because your script is calling a function from another file. However, I highly suggest you start off by using PyQt5, as Qt4 is quite old. Here is an example similar to what you're trying to achieve: https://gitlab.com/snippets/1743613

For now you can skip the foo stuff entierely, like so:
#!/usr/bin/python3
import os
import sys

from PyQt5 import QtCore, QtGui, QtWidgets , uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "untitled.ui", self)
        self.show()

if __name__== '__main__':
    app = QtWidgets.QApplication(sys.argv)
    gui = Main()
    sys.exit(app.exec_())
Where untitled.ui is the template you made in QtCreator.
Reply
#3
Any idea how big is Qt5?

I was installing an application that required the installation of Qt4, ended up installing all the bell and whistles Rolleyes (and ran out of space on my 16Gb drive, and it still wanted more room to install other stuff Shocked Doh ). Its seems like a shame to have to kiss it all goodbye now Cry , especially when it seems to be working, haven't really had a chance to use it.

Thanks for the tip .

I might just do that and try to install Qt5 Think , but i'm going to hold out for a while incase someone else comes across this and remember how to solve this.
Reply
#4
We would need the content of TestForm.ui to help you, but my bet is that you made a QDialog in QtCreator and you're using a QMainWindow in your script, which does not have an 'accept' method. Therefore you may try replacing QMainWindow for QDialog.
Reply
#5
You called it right. I changed it to a main window and it works great. Big Grin

Thanks, Alfalfa, really appreciate going the extra mile.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020