Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4031958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:39:42+00:00 2026-05-20T11:39:42+00:00

My question is somewhat related to Get a layout's widgets in PyQT but it’s

  • 0

My question is somewhat related to Get a layout's widgets in PyQT but it’s not a duplicate. Instead of looking for a high level strategic view of how to do it, I’m trying to understand what the most idiomatic and straightforward way to do it would be. Since PyQt is a pretty exact binding of the Qt C++ API, it presents a C-ish way to get at widgets in a layout. Here is the sort of idiom I have been using:

for i in range(layout.count()):
  item = layout.itemAt(i)
  if type(item) == QtGui.QLayoutItem:
    doSomeStuff(item.layout())
  if type(item) == QtGui.QWidgetItem:
doSomething(item.widget())

I’m not the most experienced Python guy, but this seems somewhat unpythonic. My intuition is telling me that in an ideal world, the Python code should look something more like:

for w in layout.widgets():
  doSomething(w)

Am I wrong? Am I missing a superior idiom? Is this the best possible way to iterate over widgets in PyQt? I tend to think in C++, so I sometimes miss out on “obvious” Python language features that make things better. Part of what I’m doing is recursively descending into widgets with layouts with widgets with layouts (etc…) to automatically wire up connections to UI’s made in Designer at run time. Add in QTabWidgets, and handling of the dynamic properties set in designer, and my code basically works, but it just feels horribly clunky.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:39:43+00:00Added an answer on May 20, 2026 at 11:39 am

    You could put the widgets into a generator like so:

    items = (layout.itemAt(i) for i in range(layout.count())) 
    for w in items:
       doSomething(w)
    

    If you end up using that a lot, you could sock that code into a generator function:

    def layout_widgets(layout):
       return (layout.itemAt(i) for i in range(layout.count()))
    
    
    for w in layout_widgets(layout):
       doSomething(w)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is somewhat related to this question , but not quite. I have two
[This question is somewhat related to this question , but the answers are not...]
Somewhat related to this question , but in the absence of any answer about
This is somewhat related to the question posed in this question but I'm trying
Somewhat related to my question about integers instead of decimals; my vendor provides a
This is somewhat related to a question I asked not too long ago today.
not sure if this question should be here or in serverfault, but it's java-related
This question is somewhat related to Hibernate Annotation Placement Question . But I want
This question is somewhat related to my previous question (not necessary to go through
I asked a somewhat related question but I want it to make it more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.