Results 1 to 6 of 6

Thread: How to check if QTableWidgetItem exists ?

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to check if QTableWidgetItem exists ?

    I'm trying to give a network request based on a QTableWidget's cell data. If a QTableWidgetItem isn't set to a particular cell, then if I try to read the cell's text, the application crashes. Even after I check if the cell data isEmpty() or isNull() doesn't solve the issue as the item itself isn't present.
    Is there any way tht I can find the existence of a QTableWidgetItem in QTableWidget ?

    Qt Code:
    1. QString hRef = QTableWidget::item(nRow, nCol)->text(); // crashes here
    2. if( hRef.isEmpty() || hRef.isNull() )
    3. {
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check if QTableWidgetItem exists ?

    Why not just check the return value of item()?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,625
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to check if QTableWidgetItem exists ?

    QTableWidget::item() is not static either... It needs to be invoked on an object, which your code snippet is not doing.

  4. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to check if QTableWidgetItem exists ?

    Yep, I'm using an object only. I tried to show the api I'm using.
    This is how I'm doing.
    Qt Code:
    1. void CResultsTable::getCVEScore()
    2. {
    3. if(rowCount() == nLoadingRow)
    4. {
    5. bVuln_All = true;
    6. emit loadComplete();
    7. return;
    8. }
    9.  
    10. QString refStr = this->item(nLoadingRow, Reference_Col)->text(); // <<-----
    11.  
    12. if(refStr.isEmpty() || refStr.isNull())
    13. {
    14. qDebug() << "Cell is empty";
    15. return;
    16. }
    17. QUrl url;
    18. url.setUrl(QString("http://www.somewebsite.com/get?id=")+refStr);
    19. QNetworkReply *reply = cveNAM->get(QNetworkRequest(url));
    20. Q_UNUSED(reply);
    21. }
    To copy to clipboard, switch view to plain text mode 

    How do I check the return value of item ? I mean what do I compare it with ? NULL ?

    Thank you.
    Last edited by rawfool; 1st July 2013 at 07:13.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,625
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to check if QTableWidgetItem exists ?

    Read the docs for QTableWidget::item().

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check if QTableWidgetItem exists ?

    It returns a QTableWidgetItem*, a pointer, so yes, compare to 0

    Qt Code:
    1. QTableWidgetItem *cellItem = item(nLoadingRow, Reference_Col);
    2. if (cellItem == 0)
    3. return;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    rawfool (1st July 2013)

Similar Threads

  1. How to check the file to download from ftp site exists ?
    By nikhilqt in forum Qt Programming
    Replies: 29
    Last Post: 8th November 2014, 09:47
  2. How to check Primary key is Exists in Table ?
    By hohoanganh205 in forum Newbie
    Replies: 4
    Last Post: 28th December 2011, 07:54
  3. Check if item exists into listwidget
    By hakermania in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2011, 21:54
  4. Check if a table exists in a mysql database
    By graciano in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 02:44
  5. Check if window with certain title exists
    By devil in forum Qt Programming
    Replies: 5
    Last Post: 21st January 2009, 14:42

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