Results 1 to 7 of 7

Thread: QTableWidget row insertion and set width of each column?

  1. #1
    Join Date
    Mar 2006
    Posts
    47
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default QTableWidget row insertion and set width of each column?

    Hi,
    I placed a QTableWidget on a form and i created three columns in this table widget through QT Designer and now want to add rows in this table widget through coding.

    My problems are:-
    (1) How to add rows in this table widget through coding.
    (2) How to set width of each column through QT Designer and through coding.

    Thanks and regards

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget row insertion and set width of each column?

    Hi,
    1) Try insertRow(),
    2) If I'm not wrong you have to set the width of the horizontal header to have all the column to resize.

    Simone

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget row insertion and set width of each column?

    Quote Originally Posted by darpan View Post
    (1) How to add rows in this table widget through coding.
    Something like this:
    Qt Code:
    1. int row = tableWidget->rowCount(); // current row count
    2. tableWidget->setRowCount(row+1); // add one row
    3. // create items in all added cells
    4. for (int col = 0; col < tableWidget->columnCount(); ++c)
    5. {
    6. QTableWidgetItem* newItem = new QTableWidgetItem("blaa");
    7. tableWidget->setItem(row, col, newItem);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by darpan View Post
    (2) How to set width of each column through QT Designer and through coding.
    to access headers:

    to resize:
    J-P Nurmi

  4. #4
    Join Date
    Mar 2006
    Posts
    47
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QTableWidget row insertion and set width of each column?

    Hi,
    I read the following functions but can't get how to set width of each column in QTreeWidget through QT Designer and through coding.

    to access headers:
    QTableView::horizontalHeader()
    QTableView::verticalHeader()

    to resize:
    QHeaderView::resizeSection()
    QHeaderView::setStretchLastSection()
    QHeaderView::setResizeMode()
    ...

    Please give some code example to set the width of QTreeWidget Column.

    Thanks and Regards

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget row insertion and set width of each column?

    I thought it was QTableWidget..

    Qt Code:
    1. #include <QHeaderView>
    2. QTreeWidget* treeWidget = ...;
    3. treeWidget->header()->resizeSection(0, 20); // column 0, width 20
    4.  
    5. QTableWidget* tableWidget = ...;
    6. tableWidget->horizontalHeader()->resizeSection(1, 50); // column 1, width 50
    7. tableWidget->verticalHeader()->resizeSection(2, 15); // row 2, height 15
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Mar 2006
    Posts
    47
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QTableWidget row insertion and set width of each column?

    Hi,
    First of all sorry , you are right, it was QTableWidget. I had to set width of Each column of QTable Widget.
    I also have requirement to delete all rows created through following lines:-
    row = tableWidget->rowCount(); // current row count
    tableWidget->setRowCount(row+1); // add one row
    I am able to clear the whole QTableWidget but how i can delete all the rows created through setRowCount(row+1).


    Thanks and Regards

  7. #7
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableWidget row insertion and set width of each column?

    You can always use QTableView::setColumnWidth() and call this both when your widget class is initialized and/or after your data loads.

    For removing the rows, tableWidget->removeRow(row+1) ??

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.