Results 1 to 5 of 5

Thread: [pyQt] Trying to understand the a piece of code

  1. #1
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Platforms
    Windows

    Default [pyQt] Trying to understand the a piece of code

    I am editing an texture exporter that somebody else coded, and there's a small segment I'm not grasping.
    The exporter has a QListView that get's filled with groups from photoshop.

    I had removed a part of the code that I thought was purely for debuging, but then I noticed that I started to get duplicate entries in the list. Here:
    Qt Code:
    1. if layerName in loadedNames:
    2. print layerName, " IS ALREADY LOADED"
    3. continue
    4.  
    5. if layerName.endswith("_alpha") or layerName.endswith("_a"):
    6. print "SUPPOSED TO MOVE INTO ALPHA"
    7. continue
    To copy to clipboard, switch view to plain text mode 
    All I see is it printing messages to the console. The if's don't do anything else... Perhaps the issue here is that I don't understand how continue works. I thought it was just something people wrote for clarity, and to avoid nesting if statements. Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [pyQt] Trying to understand the a piece of code

    The ifs do something else, they continue the loop they are in, i.e. they advance the loop and make it execute the loop body from the beginning again. Basically skipping the current loop iteration.

    If you take them out then the control flow will reach statements after those ifs, in your case most likely code that adds data to the list view.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: [pyQt] Trying to understand the a piece of code

    I see, thank you. So it's basically the same as writing "if not... " followed by a break.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [pyQt] Trying to understand the a piece of code

    A break ends the loop, so no.

    The equivalent would be an "if not" with the rest of the code in its "then" body

    Cheers,
    _

  5. #5
    Join Date
    Dec 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [pyQt] Trying to understand the a piece of code

    As mentioned by anda_skoa, break and continue are not the same.

    Both are used in loops, but do different things:

    break: Stops the iteration of the loop, and continutes to whatever code statement below the loop.
    continue: Continues the iteration of the loop, but will ignore any code statements below the 'continue' keyword.

    So basically what this means for this piece of code is:
    1. if statement: checks if the value of the 'layerName' variable already exists in the list 'loadedNames', and if so, it skips all further statements and continues the loop with the next 'layerName', otherwise it goes to the next if statement.
    2. if statement: checks if the value of the 'layerName' variable ends with "_alpha" or ends with"_a", and if so, it skips all further statements and continues the loop with the next 'layerName', otherwise it goes to whatever comes after this if statement.

    It's really quite easy to understand, turn your programmer mind off and read the code... 'endswith' 'continue' etc. will then probably make more sense


    -

Similar Threads

  1. execute a small piece of javascript code ?
    By cornucopia in forum Qt Webkit
    Replies: 2
    Last Post: 28th February 2012, 14:00
  2. Need a second opinion on that piece of code...
    By homerun4711 in forum Newbie
    Replies: 7
    Last Post: 18th February 2011, 20:35
  3. will this piece of code related to phonon works?
    By Lakshmi.Bollavaram in forum Installation and Deployment
    Replies: 1
    Last Post: 20th November 2009, 08:57
  4. How to execute a small piece of javascript code ?
    By cornucopia in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2009, 10:03
  5. Replies: 6
    Last Post: 25th February 2008, 10:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.