Results 1 to 5 of 5

Thread: How to accelerate the loop speed ?

  1. #1
    Join Date
    Jun 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: How to accelerate the loop speed ?

    Hi ,all:
    I am working on a small program .This program is to show info on a QTableWidget , it may have thousand rows , but the main time waste section is a loop , code like below :
    Qt Code:
    1. int i=0;
    2. vector<Case>::iterator it = tmp.begin();
    3. for(;it!=tmp.end();it++, i++)
    4. {
    5. table->insertRow(i);
    6. table->setCellWidget( i, 0, new QCheckBox(QString("Y/N")));
    7. table->setItem(i, 1, new QTableWidgetItem(((*it).type)));
    8. table->setItem(i, 2, new QTableWidgetItem(QString((*it).name.c_str())));
    9. table->setItem(i, 3, new QTableWidgetItem(QString((*it).note.c_str())));
    10. }
    To copy to clipboard, switch view to plain text mode 
    If I delete the line "table->setCellWidget( i, 0, new QCheckBox(QString("Y/N")));" ,the program will go much more faster ,so that line take the most of the loop run time .What should I do to make the program go faster ?


    Added after 18 minutes:


    I need some suggestions
    Last edited by wshn13; 2nd May 2013 at 05:16.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How to accelerate the loop speed ?

    Don't. Thousands of widgets in a table will not be fast to use, even if you make them faster to create.

    QTableWidgetItem supports a check box without the overhead of a widget. Try adapting this:
    Qt Code:
    1. QTableWidget *table = new QTableWidget(1000, 4);
    2. for(int i = 0; i < 1000; ++i)
    3. {
    4. QTableWidgetItem *item = new QTableWidgetItem("Y/N");
    5. item->setCheckState(Qt::Unchecked);
    6. table->setItem(i, 0, item);
    7. table->setItem(i, 1, new QTableWidgetItem("A"));
    8. table->setItem(i, 2, new QTableWidgetItem("B"));
    9. table->setItem(i, 3, new QTableWidgetItem("C"));
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: How to accelerate the loop speed ?

    Quote Originally Posted by ChrisW67 View Post
    Don't. Thousands of widgets in a table will not be fast to use, even if you make them faster to create.

    QTableWidgetItem supports a check box without the overhead of a widget. Try adapting this:
    Qt Code:
    1. QTableWidget *table = new QTableWidget(1000, 4);
    2. for(int i = 0; i < 1000; ++i)
    3. {
    4. QTableWidgetItem *item = new QTableWidgetItem("Y/N");
    5. item->setCheckState(Qt::Unchecked);
    6. table->setItem(i, 0, item);
    7. table->setItem(i, 1, new QTableWidgetItem("A"));
    8. table->setItem(i, 2, new QTableWidgetItem("B"));
    9. table->setItem(i, 3, new QTableWidgetItem("C"));
    10. }
    To copy to clipboard, switch view to plain text mode 
    I change code like your advice ,but it work the same . Will QTableView have influence ? Do you have any idea ,thanks!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How to accelerate the loop speed ?

    It takes literally 15 milliseconds to execute the loop and create the 4000 items in my example.
    Post a complete example program that demonstrates the problem.

    BTW: You can resize the table widget once to match the item count of the vector rather than increase it row at a time: QTableWidget::setRowCount().
    Last edited by ChrisW67; 2nd May 2013 at 07:07.

  5. #5
    Join Date
    Jun 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: How to accelerate the loop speed ?

    I redo it ,it works ! thanks!

Similar Threads

  1. How to speed up the Qt GUI apps launch speed?
    By wshn13 in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2012, 09:37
  2. Replies: 4
    Last Post: 6th August 2011, 02:40
  3. How to accelerate the running speed of the qt program?
    By superwave in forum Qt Programming
    Replies: 5
    Last Post: 20th June 2011, 06:03
  4. how to accelerate the replot?
    By rambo83 in forum Qwt
    Replies: 6
    Last Post: 17th March 2010, 12:11
  5. Accelerate Widget Painting
    By jpujolf in forum Qt Programming
    Replies: 0
    Last Post: 23rd September 2008, 16:33

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.