PDA

View Full Version : Aligning text in QHeaderView



ShamusVW
20th April 2016, 08:21
I have a QTableWidget with 2 columns, with a QHeaderView.
I left align all QTableWidgetItems in the left column, center align the right column QTableWidgetItems.
How do I now do this aligning of text to my QHeaderView? Currently both columns are center aligned, I want the first column header to be left aligned, the right column header to be center aligned.
This is my code for adding the QHeaderView in my class:



setRowCount(0);
setColumnCount(2);
setColumnWidth(0, 400); //set width of 1st column, 2nd column will auto-fit to remaining space

QHeaderView *header = this->horizontalHeader();
header->setSectionResizeMode(1, QHeaderView::Stretch); //let last column stretch to fit the table
QStringList headerLabels;
headerLabels << "Start of Stoppage" << "Duration";
setHorizontalHeaderLabels(headerLabels);
QFont *font = new QFont("Helvetica",20);
horizontalHeader()->setFont(*font);

Thank you, I appreciate any help on this.

d_stranz
20th April 2016, 15:58
QTableWidgetItem * pItem = pTableWidget->horizontalHeaderItem( 0 );
pItem->setTextAlignment( Qt::AlignLeft | Qt::AlignVCenter );

pItem = pTableWidget->horizontalHeaderItem( 1 );
pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

ShamusVW
21st April 2016, 09:18
That is 100%, thank you. Worked perfectly.