Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Better way to adding Large amount's of rows & columns to QTableWidget?
Forum Updated to NodeBB v4.3 + New Features

Better way to adding Large amount's of rows & columns to QTableWidget?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CAD_coding
    wrote on 18 Apr 2013, 08:44 last edited by
    #1

    Hi,
    I have a QTableWidget with some random no of rows & columns, & I have created QTableWidgetItem for each cell.
    Now I am asking the user for how many rows & columns must be increased.
    Now the problem is that the rows & columns are added in the middle of table & not at the end. But they are contiguously added.
    There are 2 options (just showing the logic, not the exact syntax!) :
    1.
    for(no of columns/rows to add)
    {
    table->insertColumn();
    table->insertRow();
    //in 1 more loop create QTableWidgetItem & set that for cells of corresponding row/column
    }

    delete table;
    table = new QTableWidget();
    table->setColumnCount();
    table->setRowCount();
    //create QTableWidgetItem for each cell & set that as item.

    Currently I am getting less time for 2nd method. But I am not sure as to which method is better.
    Any suggestions?
    Thank You.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      prady_80
      wrote on 18 Apr 2013, 10:02 last edited by
      #2

      Is it a necessity to use QTableWidget?
      Why don't you use a QtableView with a custom model? that way it should be easier for you to insert data in between and then emit the model changed events which would refresh the view automatically?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mario84
        wrote on 18 Apr 2013, 11:39 last edited by
        #3

        Are you sure the 2nd method is the faster one? Sounds strange...

        A small hint: If you want to use method 2, you don't have to delete the whole TableWidget, just call QTableWidget::clear()...
        But you should really consider using a TableView with your own model (Model/View programming isn't that difficult)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CAD_coding
          wrote on 18 Apr 2013, 17:21 last edited by
          #4

          @prady_80 I dont know how mode/view framework works. I tried understanding once but it was too much for me to understand in one go. I think I will try understanding it again.

          @Mr. Universe the docs for QTableWidget::clear() dont say much. Does it automatically delete the QTableWidgetItem's for all the cell?
          What is the difference between ::clear() & ::clearcontents(). Both seem to do the same.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            prady_80
            wrote on 18 Apr 2013, 18:24 last edited by
            #5

            To get you started "this":http://doc.qt.digia.com/4.7/model-view-programming.html will give you a very clear understanding about QModelIndex which is the main object around which everything revolves. And as an example to modify and do your self stuff you can look at "Address Book example":http://qt-project.org/doc/qt-4.8/itemviews-addressbook.htmlfrom Qt examples.

            EDIT: If you wish to insert items in between
            you can create a new empty row using QTableWidget::insertRow.
            Get the inserted row using QTableWidget::item and modify it.
            The view should automatically show it as it model underlying QTableWidget will emit A signal to refresh the view

            1 Reply Last reply
            0

            1/5

            18 Apr 2013, 08:44

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved