Python Forum
What does QApplication do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does QApplication do?
#1
OK, if this is out of python subject just ignore it .

I am beginning to understand strange "indented" philosophy of python syntax.
I even got where __main__ came from.
I picked this sample code and trying to understand the
purpose of QApplication and
WHY is it run in main function .
( I am not to happy with so many "mains" in the example. )

After "cut and paste" another function into the sample code it simply runs ONLY the "main" function.

I need to understand the purpose of QApplication before I venture to use classes instead
of functions only.




import sys
from PyQt5.QtWidgets import QApplication, QWidget
# main function 
def main():
#?? 
    app = QApplication(sys.argv)
# build main window 
    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('CAT BT KISS Main window ')
    w.show()
    #?? run QApplicatio 
    app.exec_()          # no exit 

    #sys.exit(app.exec_()) # process application and exit 
# next function 
def function ():
    application = QApplication(sys.argv)
    w = QWidget()
    w.resize(650, 150)
    w.move(500, 500)
    w.setWindowTitle('CAT BT KISS Main (function) window ')
    w.show()
#?  
    application.exec_()

    #sys.exit(app.exec_()) # process application and exit 
# default __name__
if __name__ == '__main__':
# process 
    main()
    function()
Reply
#2
QApplication initializes the Qt framework. I think (though I have not verified by testing all Qt objects and functions) that it must be called before doing anything else Qt related. Much like you need to call Tk() before you can do anything tkinter related. I do not call QApplication from inside a class. QApplication should only be called once per application and I don't like writing classes that cannot be used with other classes.

I think you'll get over your dislike of __main__. When writing a GUI with multiple windows I use if __name__ == __main__: to let me run the module by itself. For example, this code is at the bottom of my console.py module:
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    console = Console()
    console.setWindowTitle('Console')
    console.setfont(QtGui.QFont('Lucida Sans Typewriter', 10))

    # Redirect stdout to console.write and stderr to console.errorwrite
    redirect = Redirect(console.errorwrite)
    with redirect_stdout(console), redirect_stderr(redirect):
        console.show()
        sys.exit(app.exec_())
Console is designed to mimic a simple Python terminal session that can be embedded in a Qt GUI application. My main GUI window has a "Service" menu and if you select "Console" this is what pops up. In the console I can type python commands and see the results. Very useful for debugging.

By adding this code to the bottom of my console.py file I don't need to write a GUI application just to test my Console class. At the command prompt I type python console.py and I get a python interactive console.

There is nothing special about the function main() in Python. Not only does your application not need a function called "main", but it doesn't need to use functions at all. The main(), __main__, __name__ thing is a convention for having conditionally executed code. When you run a Python module it sets __name__ == '__main__'. By testing if __name__ == '__main__': your can prevent code from running when a module is imported. In my console.py file I don't want Consoles popping up each time I import console.py, but I do want to see a Console window when I run python console.py. It is a convention to put this conditionally executed code inside a function called main(), but as you see in my example it is not required.

Another thing, there is no need to include the if __name__ == '__main__': if you don't have conditionally executed code. This code appears at the bottom of may "main" file for an application that actually uses Console.
# Create application
app = QtWidgets.QApplication(sys.argv)
vm = VirtualMachine()
mainpanel = MyPanel(vm)
mainpanel.setWindowTitle('COOPS Demo')
mainpanel.show()
Node.notify_link_changed(FunctionCallback(mainpanel.createvm, vm, None))
event_timer = QtCore.QTimer(None)
event_timer.timeout.connect(Event.timer_tic)
event_timer.start(1)
mainpanel.console.setcontext(
    {'vm': vm, 'Event': Event, 'mainpanel': mainpanel, 'Node': Node}
    )
redirect = Redirect(mainpanel.console.errorwrite)
with redirect_stdout(mainpanel.console), redirect_stderr(redirect):
    sys.exit(app.exec_())
I will never import this module so there is no reason to hide the startup code inside an if statement.
Reply
#3
Allow me to summarize

1. python does not NEED "main" in any format
2. in my sample /example the "application " is Qt5 , not python , hence
when referring to the python x.py file I should use another term

I cannot wait to see what other convoluted scheme I will see when I use real OOP.

PS
Thanks for the redirection examples, my next code is to send errors to window stratus bar.
Reply
#4
Python is flexible. You can do OOP when that is the best approach. You can do procedural if that is best, and notebooks add a whole 'nother approach.

And, there is no single GUI platform that is "the" Python GUI. You will see various approaches in the GUI section of this board. I am partial to WX. But, in any of these platforms there will be some class that you will need to invoke to set up the whole GUI paradigm. That's what the QApplication is. And, being able to run the same code as a stand alone or as a module called from elsewhere is a nice feature.
Reply
#5
I prefer every GUI toolkit I've tried in Python, even Tk, over any GUI toolkit I've tried to use with Visual Studio.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QApplication: Invalid Display* argument iFunKtion 0 3,505 Feb-15-2017, 11:00 AM
Last Post: iFunKtion

Forum Jump:

User Panel Messages

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