PDA

View Full Version : QTableWidget not showing the last column: How to fix it?



robgeek
2nd April 2015, 00:32
Hello!

I would like to create a table but it is not showing the last column name. Why and how can i fix it?


DataWindow::DataWindow(QWidget *parent) : QDialog( parent ), ui(new Ui::DataWindow)
{
ui->setupUi( this );

QStringList colsnames; //It will be #, 1, 2, 3, ... 20.
colsnames << "#";
for(int i = 0; i < 20; i++)
colsnames << QString::number( (i + 1) );

ui->historicLM->setHorizontalHeaderLabels( colsnames );
//ui->label->setText( QString::number( colsnames.size( ) ) );

// Set the column size.
for(int i = 0; i < 21; i++)
ui->historicLM->setColumnWidth(i, 20);
}


My table:11053

jefftee
2nd April 2015, 01:47
Your two for loops are different, I assume you know that. You set column names for 20 columns with the first for loop and set the column width for 21 columns.

Which is correct?

robgeek
2nd April 2015, 03:01
Yes, i know that.
I want to show 20 number in the columns names with "#" being the first column name. "# 1 2 3 4 ... 20".
Thats why i'm setting 21 width columns in the second for loop.
But the problem is i'm getting only 19 numbers. Look at the picture in my original post, please. Is missing one, the last one, "20"!

jefftee
2nd April 2015, 03:15
What does ui->historicLM->columnCount() return?

Did you by chance set the columnCount property in designer? If so, what's it set to?

robgeek
2nd April 2015, 03:51
Is returning 20.

I didn't do anything in design mode but resizing.

jefftee
2nd April 2015, 03:58
Is returning 20.
Makes sense to me that you can't see 21 horizontal labels if the QTableWidget instance has 20 columns.

Try to set the number of columns to 21 using QTableWidget::setColumnCount and see if that helps.

robgeek
2nd April 2015, 04:12
Thank you, dude!

I don't know if i have to create another post, but, do you know how can i do to the columns width can automatically fit inside the table? As you can see after the column number 19 i have a huge blank space and i don't want that.

jefftee
2nd April 2015, 04:26
Start with something like this:



QHeaderView *header = ui->historicLM->horizontalHeader();
header->resizeSections(QHeaderView::Stretch);

May require that the QTableWidget has to be resized first, but since it's just been constructed and hasn't been shown yet, should work I would think.

robgeek
2nd April 2015, 14:53
It worked fine, thanks!

I deleted the following for loop:

for(int i = 0; i < 21; i++)
ui->historicLM->setColumnWidth(i, 20);