PDA

View Full Version : get QTableWidget Horizontal Label Text?



schinkengott
15th June 2015, 17:55
hey guys (again :D)
now, im just trying to access the text of a horizontal header, of my current Cell, to display it in a label, but i dont really get to know how to do this..
i tried this:


int buf;
buf = table->currentColumn();
actualCol->setText(QString::number(table->horizontalHeaderItem(buf);

can someone help me?


EDIT: Got it!


QString buf;
buf = table->horizontalHeaderItem(table->currentItem()->column())->text(); // get the current items header
actualCol->setText(buf);

schinkengott
15th June 2015, 19:55
ok, i need your help again. i want to print out my whole qtable, with headers.
but im only able to print out the rows/cols without header, using a QTextDocument.
how can i put the headers in?


for(int row=0;row<getMaxRow();row++)
{
for(int col=0;col<getMaxCol();col++)
{
//QTextTableCell hHeader = docTable->cellAt(0,col);
//hHeader.setFormat(cellFormat);
//hHeader.firstCursorPosition().insertText(table->horizontalHeaderItem(table->columnAt(col))->text());
QTextTableCell cell = docTable->cellAt(row+1,col+1);
cell.setFormat(cellFormat);
cell.firstCursorPosition().insertText(table->item(row+1,col+1)->text());
}
}

this doesnt work

ChrisW67
15th June 2015, 21:16
It may help if you shared what "doesn't work" about it?

d_stranz
17th June 2015, 05:02
how can i put the headers in?

First question is, why are you using row+1 and col+1? Tables start at row = 0 and col = 0, so as soon as your row or col variables reach their maximum values, you'll be trying to access table items that don't exist because they are beyond the range of getMaxRow() or getMaxCol().

Second, if what you mean by "Horizontal Label Text" are the column headers, then you simply retrieve the QTableItem from the table using QTableWidget::horizontalHeaderItem() for each column in a loop over all the columns, and add that to your QTextDocument before retrieving the contents of the table's cells.