PDA

View Full Version : Present parts of string in table (QTableWidget)



imperator
23rd November 2009, 11:13
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é

Lykurg
23rd November 2009, 11:22
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!

imperator
23rd November 2009, 13:29
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?


QString s = "Some text";

// Label for trimmed text
QLabel* label = new QLabel(s.section(" ", 0, 0));

QVariant v = QVariant(s);

QTableWidgetItem* item = new QTableWidgetItem();
item->setData(Qt::TextDontPrint, v.toString());

// assume a table is available through the pointer tw
tw->setCellWidget(row, column, label);
tw->setItem(row, column, item);


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?



QString s = twi->data(some role).toString();


Not sure about roles.

Regards,

André

Lykurg
23rd November 2009, 14:41
QString str = "Some very long text...";
QTableWidgetItem* item = new QTableWidgetItem();
item->setData(Qt::DisplayRole, str.left(4)); // display only the 4 first letters
item->setData(Qt::UserRole, str); // store the whole text, but hidden
// use item normally and add it to the table

// at some point you have a pointer to any item
item->data(Qt::DisplayRole); // returns the first 4 letters
item->data(Qt::UserRole); // returns the whole string.