[PySide] sys.exit(app.exec_())

806 views
Skip to first unread message

Algis Kabaila

unread,
Jan 1, 2011, 1:12:24 AM1/1/11
to pys...@lists.openbossa.org
Hi and a Happy New Year!

1. Strictly speaking my question is a straight Python query. In
PySide GUI program it is common to use "app" as the instance of
QApplication. I do that regularly.

The last statement of the script is often
"sys.exit(app.exec_())"
I usually start the loop with simple "app.exec_()", because it
is shorter and seems more meaningful to me.

I have seen simple Python scripts of the structure:

program
import sys
-- some code --
def main()
-- code --
if __name__ == "__main__":
sys.exit(main())

What is the reason for using sys.exit(main()) when simple and
clear main() would do just as well?

2. The Qt Designer produced files have many statements similar
to this: menubar.setObjectName("menubar")
What practical effect has the "setObjectName" ?
I can not see it as the program runs as well with statements
with "setObjectName" removed.


--
Algis
http://akabaila.pcug.org.au
_______________________________________________
PySide mailing list
PyS...@lists.openbossa.org
http://lists.openbossa.org/listinfo/pyside

anatoly techtonik

unread,
Jan 1, 2011, 10:03:10 AM1/1/11
to Algis Kabaila, pys...@lists.openbossa.org
On Sat, Jan 1, 2011 at 8:12 AM, Algis Kabaila <akab...@pcug.org.au> wrote:
> What is the reason for using sys.exit(main()) when simple and
> clear main() would do just as well?

Return exit code to the system?
--
anatoly t.

Thomas Perl

unread,
Jan 1, 2011, 1:30:02 PM1/1/11
to Algis Kabaila, pys...@lists.openbossa.org
Hi!

2011/1/1 Algis Kabaila <akab...@pcug.org.au>:


> 2. The Qt Designer produced files have many statements similar
> to this: menubar.setObjectName("menubar")
> What practical effect has the "setObjectName" ?
> I can not see it as the program runs as well with statements
> with  "setObjectName" removed.

The object name can be retrieved by other objects (probably using
"objectName()"), whereas the name in Python (i.e. "menubar") is just a
reference to it (like a pointer if you like) - the object is "bound"
to a name in the local (or global) namespace, but that name is not
unique (you could say "menubar2 = menubar", and both names would refer
to the same object).

HTH.
Thomas

Algis Kabaila

unread,
Jan 1, 2011, 9:04:48 PM1/1/11
to anatoly techtonik, pys...@lists.openbossa.org
On Sunday 02 January 2011 02:03:10 anatoly techtonik wrote:
> On Sat, Jan 1, 2011 at 8:12 AM, Algis Kabaila
<akab...@pcug.org.au> wrote:
> > What is the reason for using sys.exit(main()) when simple
> > and clear main() would do just as well?
>
> Return exit code to the system?
> --
> anatoly t.

I guess that may well be so, but is it really necessary? My
question comes from the fact that a simpler form is used in my
favourite text book on PyQt, a book that arguably every aspiring
PyQt or PySide programmer could be expected to read.

So why is sys.exit(app.exec_()) more attractive to so many
people then simpler app.exec_() ? After all, it is not the
failure that we are interested in, but a functioning program,
no? And how would sys.exit help(app.exec_()) help us in case of
sys.exit() being invoked by a program bug?

Whilst I feel happy to follow my favourite book and author and
use app.exec_() to invoke the event loop, I do not want to
mislead other newbies if sys.exit(app.exec_()) is really
superior.

Is it?

Thanks a lot for partly answering this question.. I would like
to see a deeper look into it. Be assured that the quesion is
not being asked frivolously.

Al aka OldAl.

--
Algis
http://akabaila.pcug.org.au

Algis Kabaila

unread,
Jan 1, 2011, 9:17:14 PM1/1/11
to Thomas Perl, pys...@lists.openbossa.org
On Sunday 02 January 2011 05:30:02 Thomas Perl wrote:
> Hi!
>
> 2011/1/1 Algis Kabaila <akab...@pcug.org.au>:
> > 2. The Qt Designer produced files have many statements
> > similar to this: menubar.setObjectName("menubar")
> > What practical effect has the "setObjectName" ?
> > I can not see it as the program runs as well with
> > statements with "setObjectName" removed.
>
> The object name can be retrieved by other objects (probably
> using "objectName()"), whereas the name in Python (i.e.
> "menubar") is just a reference to it (like a pointer if you
> like) - the object is "bound" to a name in the local (or
> global) namespace, but that name is not unique (you could
> say "menubar2 = menubar", and both names would refer to the
> same object).
>
> HTH.
> Thomas

Thomas,

As always, your answer do help, at least in part. I thank you
for it!

However, as our concern is Python and PySide programs why should
you, I or anyone else care about calling other objects in any
other way then Python?

I just fear that it is possible that one object will attempt to
call another object without telling the Python (limited to
Python!) programmer that it wants to do that and then fail
miserably. And I, doing a newbie to newbie tutorial stuff, will
be partly responsible for the failure.

To put it briefly, if it is a good practice to use setObjectName
on all objects, I would like to know and tell others to do that.

Thanks again and the best for the New Year.
--
Algis
http://akabaila.pcug.org.au

Thomas Perl

unread,
Jan 2, 2011, 12:14:41 PM1/2/11
to Algis Kabaila, pys...@lists.openbossa.org
2011/1/2 Algis Kabaila <akab...@pcug.org.au>:

> To put it briefly, if it is a good practice to use setObjectName
> on all objects, I would like to know and tell others to do that.

Check out the documentation about the "objectName" property:

http://doc.qt.nokia.com/latest/qobject.html#objectName-prop

It turns out that you can use "findChild" to search for children by name:

http://doc.qt.nokia.com/latest/qobject.html#findChild

So if you only have a reference to your top-level window, you could
get a reference to the child with the specific name using the
findChild method on the top-level window.

My guess would be that for auto-generated code (from .ui files), you
can just leave it in (and maybe take advantage of findChild if you
want to get a specific object), and for code that you write yourself,
you would only set an object name if you want to use findChild later
to get a reference to that object from a higher-level (direct or
indirect parent) widget/object.

This could also be useful if you set the same name on multiple widgets
and then interate over all those widgets with findChildren, e.g. you
have several buttons sprinkled throughout one window, and all "belong"
together somehow. If you name them all "fooButton", you could use "for
button in window.findChildren('fooButton'): ..." to iterate over all
buttons. I haven't used this myself, so maybe there are some other
more obvious use cases for object names :)

HTH.
Thomas

Algis Kabaila

unread,
Jan 3, 2011, 12:42:17 AM1/3/11
to Thomas Perl, pys...@lists.openbossa.org

Thomas,

Thanks for the contribution. I did have a look that the
reference. All I can deduce is that potentially the x in
x = setObjectName(<object>)
might be useful in tracing those elusive "segmentation faults"
when the destruction of the objects by the trash collector is
out of sync..
With my current confusion it would seem reasonable to leave
those statements in a commented out form. It is a kind of
compromise - the statement is not used, so it should not be
there, so whe make it inactive. But perhaps the statement might
be useful in some circumstances, so its 'image' is left in.

There are quite a few of these inactive statements in a small
example - would you care to look a it:
http://developer.qt.nokia.com/wiki/PySideSimplicissimusCombineAllIn1
How does it strike you?
Al.
--
Algis
http://akabaila.pcug.org.au

Fars- mo

unread,
Jan 11, 2011, 1:14:45 PM1/11/11
to pys...@srvrec006.openbossa.org

Algis Kabaila wrote:
> On Sunday 02 January 2011 02:03:10 anatoly techtonik wrote:
> > On Sat, Jan 1, 2011 at 8:12 AM, Algis Kabaila
> wrote:
> > > What is the reason for using sys.exit(main()) when simple
> > > and clear main() would do just as well?
> >
> > Return exit code to the system?
> > --
> > anatoly t.
>
> I guess that may well be so, but is it really necessary? My question
> comes from the fact that a simpler form is used in my favourite text
> book on PyQt, a book that arguably every aspiring PyQt or PySide
> programmer could be expected to read.
>
> So why is sys.exit(app.exec_()) more attractive to so many people then
> simpler app.exec_() ? After all, it is not the failure that we are
> interested in, but a functioning program, no? And how would sys.exit
> help(app.exec_()) help us in case of sys.exit() being invoked by a
> program bug?
>
> Whilst I feel happy to follow my favourite book and author and use
> app.exec_() to invoke the event loop, I do not want to mislead other
> newbies if sys.exit(app.exec_()) is really superior.
> Is it?
>
> Thanks a lot for partly answering this question.. I would like to see a
> deeper look into it. Be assured that the quesion is not being asked
> frivolously.

If the application quits with QApplication.exit(), then sys.exit() is necessary to pass the error code back to the system. Of course if you're absolutely sure that QApplication.exit() will never be called (at least not with a non-zero code), you can avoid sys.exit().

It's true that passing error codes to the system is less critical for a GUI application than for a command-line application, but if you do have an unexpected failure that forces the application to exit it's a very good idea to communicate that to the system so that the program that launched your application can act appropriately if needed.

So I wouldn't recommend dropping sys.exit().


Cheers,
Farsmo

Algis Kabaila

unread,
Jan 11, 2011, 8:59:06 PM1/11/11
to pys...@lists.openbossa.org
On Wednesday 12 January 2011 05:14:45 Fars- mo wrote:
> Algis Kabaila wrote:
> > On Sunday 02 January 2011 02:03:10 anatoly techtonik wrote:
> > > On Sat, Jan 1, 2011 at 8:12 AM, Algis Kabaila
(snip...)
>
> If the application quits with QApplication.exit(), then
> sys.exit() is necessary to pass the error code back to the
> system. Of course if you're absolutely sure that
> QApplication.exit() will never be called (at least not with
> a non-zero code), you can avoid sys.exit().
>
> It's true that passing error codes to the system is less
> critical for a GUI application than for a command-line
> application, but if you do have an unexpected failure that
> forces the application to exit it's a very good idea to
> communicate that to the system so that the program that
> launched your application can act appropriately if needed.
>
> So I wouldn't recommend dropping sys.exit().
> Cheers,
> Farsmo
>
Thank you - I am almost convinced :). Actually, I am not
dropping anything, as I learned to use exec_() from my favourite
book on PyQt. AFAIK there only is *one* book worth mention on
the topic. It covers great many subjects in great detail, but
never uses or AFAIR mentions sys.exit(exec_() ).

So rather than dropping, I am considering adding sys.exit :)

I thin that it is not as easy to understand as plain exec_() and
that is why the author of that book chose to use the shorter
form. He is a great programmer and a good teacher, but he is
only human, after all...

I greatly appreciate your reply.

Bye for now,

Al.


> _______________________________________________
> PySide mailing list
> PyS...@lists.openbossa.org
> http://lists.openbossa.org/listinfo/pyside

--
Algis
http://akabaila.pcug.org.au

Reply all
Reply to author
Forward
0 new messages