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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:56:32+00:00 2026-06-16T10:56:32+00:00

I have a custom widget which displays many items in rows: void update(){ //this

  • 0

I have a custom widget which displays many items in rows:

void update(){ //this is a SLOT which is connected to a button click
    QVBoxLayout *layout = this->layout();
    if (layout == NULL){
        layout = new QVBoxLayout;
        this->setLayout(layout);
    } else {
        QLayout_clear(layout); //this is a function that I wrote that deletes all of the items from a layout
    }
    ArrayList *results = generateData(); //this generates the data that I load from
    for (int i = 0; i < results->count; i++){
        layout->addWidget(new subWidget(results->array[i]));
    }
}

The problem is that there are about 900 items and a profile reveals that simply adding the child object to the layout takes 50% of the time (constructing takes the other 50%). Overall it takes about 3 seconds to load all of the items.

When I click on the button to load more data, the entire UI freezes for the 3 seconds and then all of the items appear together when everything is done. Is there a way to progressively load more items as they are being created?

  • 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-06-16T10:56:33+00:00Added an answer on June 16, 2026 at 10:56 am

    The first trick is, as Pavel Zdenek said, to process only some of the results. You want to process as many together so that the overhead (of what we’re going to do in the next step) is low, but you don’t want to do anything that would make the system seem unresponsive. Based on extensive research, Jakob Nielsen says that “0.1 seconds is about the limit for having the user feel that the system is reacting instantaneously”, so as a rough estimate you should cut your work into roughly 0.05 second chunks (leaving another 0.05 seconds for the system to actually react to the user’s interactions).

    The second trick is to use a QTimer with a timeout of 0. As the QTimer documentation says:

    As a special case, a QTimer with a timeout of 0 will time out as soon
    as all the events in the window system’s event queue have been
    processed. This can be used to do heavy work while providing a snappy
    user interface.

    So that means that a timer with a timeout of 0 will be executed next, unless there is something else in the event queue (for instance, a mouse click). Here’s the code:

    void update() {
        i = 0; // warning, this is causes a bug, see below
        updateChunk();
    }
    
    void updateChunk() {
        const int CHUNK_THRESHOLD = /* the number of things you can do before the user notices that you're doing something */;
        for (; i < results->count() && i < CHUNK_THRESHOLD; i++) {
             // add widget
        }
        // If there's more work to do, put it in the event queue.
        if (i < results->count()) {
            // This isn't true recursion, because this method will return before
            // it is called again.
            QTimer::singleShot(0, this, SLOT(updateChunk()));
        }
    }
    

    Finally, test this a little bit because there’s a gotcha: now the user can interact with the system in the “middle” of your loop. For instance, the user can click the update button while you’re still processing results (which in the above example means that you would reset the index to 0 and reprocess the first elements of the array). So a more robust solution would be to use a list instead of an array and pop each element off the front of the list as you process it. Then whatever adds results would just append to the list.

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

Sidebar

Related Questions

Suppose I have a MyWidget which contains a MySubWidget , e.g. a custom widget
i have this custom tab widget.. before i've customized it, by default i have
lets say i have a custom widget which has a ClickHandler. Here's the example:
I have a widget in which I can drop items into a container. I
I have a custom widget which has an overall layout of a QVBoxLayout. It
I need to create a custom push button which will have 3 different background
I have a custom ScrollView (extended android.widget.ScrollView) which I use in my layout. I
I have a custom Qt widget which I used to display disassembly and I
Basically I have written a custom widget, which is going to be displayed 5
I have created a custom widget which has a link to some ajax functionality.

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.