Results 1 to 4 of 4

Thread: Present parts of string in table (QTableWidget)

  1. #1
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Present parts of string in table (QTableWidget)

    Hi

    I've got a couple of questions regarding QTableWidget/QTableWidgetItem.

    I've got a table I want to populate with data from a string. My program reads strings from a file and I use QTableWidgetItem to pass them onto the table.

    This works as intended, but each populated cell contains the whole string. I'd like a cell to hold the information in the string so I can present a message box when the user clicks a cell.

    My question is if it's possible to present only the header from the string in the table cell, and keep the rest of the data hidden?

    Regards,

    André

  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: Present parts of string in table (QTableWidget)

    Instead of setting the whole text just set the trimmed one. Set the whole text by using QTableWidgetItem::setData() with Qt::UserRole. Get the text back via QTableWidgetItem::data().


    EDIT: And of course:

    Salve imperator, vivat princeps optime!

  3. #3
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Present parts of string in table (QTableWidget)

    Hi and thank you for the answer. I'm quite new in this game, and have to ask a bit more.

    Set the whole text by using QTableWidgetItem::setData() with Qt::UserRole.
    Is this the correct way to do the above mentioned?
    Qt Code:
    1. QString s = "Some text";
    2.  
    3. // Label for trimmed text
    4. QLabel* label = new QLabel(s.section(" ", 0, 0));
    5.  
    6.  
    7. item->setData(Qt::TextDontPrint, v.toString());
    8.  
    9. // assume a table is available through the pointer tw
    10. tw->setCellWidget(row, column, label);
    11. tw->setItem(row, column, item);
    To copy to clipboard, switch view to plain text mode 

    And then this part:
    Get the text back via QTableWidgetItem::data().
    .

    If I've got a pointer to a TableWidgetItem, twi, how can I make the text visible again?

    Qt Code:
    1. QString s = twi->data(some role).toString();
    To copy to clipboard, switch view to plain text mode 

    Not sure about roles.

    Regards,

    André

  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: Present parts of string in table (QTableWidget)

    Qt Code:
    1. QString str = "Some very long text...";
    2. item->setData(Qt::DisplayRole, str.left(4)); // display only the 4 first letters
    3. item->setData(Qt::UserRole, str); // store the whole text, but hidden
    4. // use item normally and add it to the table
    5.  
    6. // at some point you have a pointer to any item
    7. item->data(Qt::DisplayRole); // returns the first 4 letters
    8. item->data(Qt::UserRole); // returns the whole string.
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Insert the string into QTableWidget
    By suresh in forum Newbie
    Replies: 4
    Last Post: 5th August 2012, 14:06
  2. Replies: 3
    Last Post: 20th January 2011, 13:24
  3. QTableWidget - scrolling to bottom of the table
    By mkhoang in forum Qt Programming
    Replies: 4
    Last Post: 12th November 2008, 17:21

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.