Results 1 to 7 of 7

Thread: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

  1. #1
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    Hi,

    I use a QTableWidget to display a matrix. This matrix is subject to change over time and consequently so is the display.

    I always use the same function to update the display: "updateTable"

    The latter takes a list of names and a SquareMatrix object (that I created my self) as arguments.

    My problem is the following:

    everything goes fine when the display is updated for the first time, but the application crashes on subsequent updates. I checked my function and the problems seems to come from the setHorizontalHeaderItem/setVerticalHeaderItem functions.

    Here is the code of "updateTable":

    Qt Code:
    1. void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
    2. int dimension = matrix.getDimension();
    3. double value;
    4.  
    5. this->mapleEdit->setText(matrix.toMapleFormat());
    6. this->mapleEdit->setCursorPosition(0);
    7. this->matlabEdit->setText(matrix.toMatlabFormat());
    8. this->matlabEdit->setCursorPosition(0);
    9.  
    10. this->matrixTable->clear();
    11. this->matrixTable->setRowCount(matrix.getRows());
    12. this->matrixTable->setColumnCount(matrix.getColumns());
    13.  
    14. for(int row = 0; row < dimension; ++row) {
    15. QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
    16. matrixTable->setHorizontalHeaderItem(row, headerItem);
    17. matrixTable->setVerticalHeaderItem(row, headerItem);
    18. for(int column = 0; column < dimension; ++column) {
    19. matrix.getElement(row, column, value);
    20. QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
    21. item->setTextAlignment(Qt::AlignCenter);
    22. this->matrixTable->setItem(row, column, item);
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    If I comment the calls to these functions, the application doesn't crash anymore and the values are correctly updated.

    Qt Code:
    1. void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
    2. int dimension = matrix.getDimension();
    3. double value;
    4.  
    5. this->mapleEdit->setText(matrix.toMapleFormat());
    6. this->mapleEdit->setCursorPosition(0);
    7. this->matlabEdit->setText(matrix.toMatlabFormat());
    8. this->matlabEdit->setCursorPosition(0);
    9.  
    10. this->matrixTable->clear();
    11. this->matrixTable->setRowCount(matrix.getRows());
    12. this->matrixTable->setColumnCount(matrix.getColumns());
    13.  
    14. for(int row = 0; row < dimension; ++row) {
    15. QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
    16. // matrixTable->setHorizontalHeaderItem(row, headerItem);
    17. // matrixTable->setVerticalHeaderItem(row, headerItem);
    18. for(int column = 0; column < dimension; ++column) {
    19. matrix.getElement(row, column, value);
    20. QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
    21. item->setTextAlignment(Qt::AlignCenter);
    22. this->matrixTable->setItem(row, column, item);
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    This might be related to
    Qt Code:
    1. this->matrixTable->clear();
    To copy to clipboard, switch view to plain text mode 
    but I'm not sure. Could someone explain to me what's going on and what should I do to prevent these crashes from happening ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    If your matrix don't have different sizes and names you could use QTableWidget::clearContents().

    What crashes your app is that if dimension is lesser than matrix.getColumns() or matrix.getRows(). That the only reason I can see right now.

  3. #3
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    Unfortunately the matrix's size may vary over time... And dimension points to the same value as rows and columns in SquareMatrix... I feel sad

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    Quote Originally Posted by ouekah View Post
    And dimension points to the same value as rows and columns in SquareMatrix...
    I thought so because of the name. But you never know here... So only thing I can guess of (without proving it) is, that you cant reuse one item for vertical and horizontal header. So try:
    Qt Code:
    1. matrixTable->setHorizontalHeaderItem(row, new QTableWidgetItem(names.at(row)));
    2. matrixTable->setVerticalHeaderItem(row, new QTableWidgetItem(names.at(row)));
    To copy to clipboard, switch view to plain text mode 

    If that is also not helping, debug and backtrace to see where it exactly crashes.

  5. The following user says thank you to Lykurg for this useful post:

    ouekah (30th March 2010)

  6. #5
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    I read the documentation again and I noticed this function: "QTableWidget::setHorizontalHeaderLabels".

    It's even easier with that one and there is no more crash...

  7. #6
    Join Date
    Oct 2015
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    Quote Originally Posted by ouekah View Post
    I read the documentation again and I noticed this function: "QTableWidget::setHorizontalHeaderLabels".

    It's even easier with that one and there is no more crash...
    Row/column indexes start from 1. You should use next code:

    Qt Code:
    1. matrixTable->setHorizontalHeaderItem(row+1, headerItem);
    2. matrixTable->setVerticalHeaderItem(row+1, headerItem);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions

    Row/column indexes start from 1.
    No, no they don't.

    I also note that if the names list ever contains fewer names than the dimension of the matrix, deliberately or by accident, then the calls to QStringList::at() will fail.

Similar Threads

  1. C++ Static Functions
    By weaver4 in forum Newbie
    Replies: 7
    Last Post: 17th November 2009, 10:42
  2. Regards CallBack Functions
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2009, 16:16
  3. using functions in different forms
    By vishnu5 in forum General Programming
    Replies: 1
    Last Post: 4th February 2008, 19:37
  4. Using a frame for different functions
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 29th January 2008, 13:45
  5. Mathematical Functions
    By krishbhala in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2008, 13:55

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.