Results 1 to 7 of 7

Thread: how to read null value from QTableWidget?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to read null value from QTableWidget?

    In my QTableWidget, there exist some cells whose values are Null(it contains nothing)
    But when i'm trying to read table widget, if it encounters null cell, program crashes...
    How can i get rid of this problem?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to read null value from QTableWidget?

    But when i'm trying to read table widget, if it encounters null cell, program crashes...
    Can you show some code relevant to this ?

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to read null value from QTableWidget?

    Quote Originally Posted by stampede View Post
    Can you show some code relevant to this ?
    Ya sure...
    Qt Code:
    1. for(int r=0;r<rowCounter;r++)
    2. {
    3.  
    4. rwPtr=SearchTable->item(r,j);
    5. QString PrCell=rwPtr->text();
    6.  
    7. if(PrCell==Val)
    8. {
    9. int NewRowCount=TableOutput->rowCount();
    10. TableOutput->insertRow(NewRowCount);
    11. for(int m=0;m<ColCounter;m++)
    12. {
    13. cellPtr=SearchTable->item(r,m); <<<-- here program crashing, if null presents
    14. std::cout<<cellPtr->text().toStdString()<<" ";
    15. QTableWidgetItem *newCell=new QTableWidgetItem(cellPtr->text());
    16. TableOutput->setItem(NewRowCount,m,newCell);
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to read null value from QTableWidget?

    First of all, you are leaking memory:
    Qt Code:
    1. QTableWidgetItem *rwPtr= new QTableWidgetItem(); // rwPtr holds new object now
    2. rwPtr=SearchTable->item(r,j); // rwPtr value is overwritten, address of the object allocated above is lost and its impossible to release the memory now
    3.  
    4. // this is how it should look:
    5. QTableWidgetItem *rwPtr = SearchTable->item(r,j);
    To copy to clipboard, switch view to plain text mode 
    You can't do that. I mean, you can, but you shouldn't.
    Anyway, it crashes, because you never check if the pointer is valid before dereferencing:
    Qt Code:
    1. cellPtr=SearchTable->item(r,m);
    2. std::cout<<cellPtr->text().toStdString()<<" "; // what if cellPtr == NULL here ?
    To copy to clipboard, switch view to plain text mode 
    Fix the above problems and let us know if it still crashes.

  5. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to read null value from QTableWidget?

    ok thank u stampede....
    I want put "NULL" string in those cells, where item is not present...
    How can i do that?

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to read null value from QTableWidget?

    I don't know if I understood you right, but try this:
    Qt Code:
    1. QTableWidgetItem *cellPtr = SearchTable->item(r,m);
    2. QTableWidgetItem *newCell=new QTableWidgetItem(cellPtr ? cellPtr->text() : "NULL"); // remember to check your pointers !
    3. TableOutput->setItem(NewRowCount,m,newCell);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to stampede for this useful post:

    aurora (25th January 2012)

  8. #7
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to read null value from QTableWidget?

    Thanks alot Sampede...
    Thats what i wanted...

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. Replies: 5
    Last Post: 11th January 2011, 10:52
  3. read QTableWidget cells
    By navid in forum Newbie
    Replies: 8
    Last Post: 4th April 2010, 10:40
  4. Replies: 3
    Last Post: 17th March 2010, 10:31
  5. Null for QDateEdit
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2009, 21:54

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.