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 542725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:28:37+00:00 2026-05-13T10:28:37+00:00

For one of my projects I have a tree of QObject derived objects, which

  • 0

For one of my projects I have a tree of QObject derived objects, which utilize QObject’s parent/child functionality to build the tree.

This is very useful, since I make use of signals and slots, use Qt’s guarded pointers and expect parent objects to delete children when they are deleted.

So far so good. Unfortunately now my project requires me to manage/change the order of children. QObject does not provide any means of changing the order of its children (exception: QWidget’s raise() function – but that’s useless in this case). So now I’m looking for a strategy of controlling the order of children. I had a few ideas, but I’m not sure about their pros & cons:

Option A: Custom sort index member variable

Use a int m_orderIndex member variable as a sort key and provide a sortedChildren() method which returns a list of QObjects sorted by this key.

  • Easy to implement into existing object structure.
  • Problematic when QObject::children() method is overriden – will lead to problems during loops when items’ order is changed, also is more expensive than default implementation.
  • Should fall back to QObject object order if all sort keys are equal or 0/default.

Option B: Redundant list of children

Maintain a redundant list of children in a QList, and add children to it when they are created and destroyed.

  • Requires expensive tracking of added/deleted objects. This basically leads to a second child/parent tracking and lot of signals/slots. QObject does all of this internally already, so it might not be a good idea to do it again. Also feels like a lot of bloat is added for a simple thing like changing the order of children.
  • Good flexibility, since a QList of children can be modified as needed.
  • Allows a child to be in the QList more than one time, or not at all (even though it might be still a child of the QObject)

Option C: …?

Any ideas or feedback, especially from people who already solved this in their own projects, is highly appreciated. Happy new year!

  • 1 1 Answer
  • 6 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-13T10:28:37+00:00Added an answer on May 13, 2026 at 10:28 am

    I spent a lot of time going through all these options in the past days and discussed them carefully with some other programmers. We decided to go for Option A.

    Each of the objects we are managing is a child of a parent object. Since Qt does not provide any means of re-ordering these objects, we decided to add a int m_orderIndex property to each object, which defaults to 0.

    Each object has an accessor function sortedChildren() which returns a QObjectList of the children. What we do in that function is:

    1. Use the normal QObject::chilren() function to get a list of all available child objects.
    2. dynamic_cast all objects to our “base class”, which provides the m_orderIndex property.
    3. If the object is castable, add it to a temporary object list.
    4. use qSort with a custom LessThan function to find out if qSort needs to change the order of two objects.
    5. Return the temporary object list.

    We did this for the following reasons:

    • Existing code (especially Qt’s own code) can continue using children() without having to worry about side effects.
    • We can use the normal children() function in places where the order does not matter, without having any performance loss.
    • In the places where we need the ordered list of children, we simply replace children() by sortedChildren() and get the desired effect.

    One of the good things about this approach is, that the order of children does not change if all sort indices are set to zero.

    Sorry for answering my own question, hope that enlightens people with the same problem. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 projects. One project is a core and contains the GUI which
In my project, I have TreeView, which contains a tree of objects of various
I have a solution with 3 projects.One of UI (contains web pages) and one
I have the digdog MapKitDragAndDrop in one my projects to achieve drag and drop
I have a Visual Studio C++ solution with 3 projects: one contains all classes
I'm working in VS 2008 and have three projects in one solution. I'm debugging
I have upgraded one of my projects in c# from visual studio 2005 to
In one of my projects, I have an application that manages several clients (or
In one of my projects, I have some classes that represent entities that cannot
I have the following code in one of our projects webpages: XmlDocument xDoc =

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.