Set height of row of table
-
I want to create a table, my code in QtCreator:
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->tableWidget->setColumnCount(5); ui->tableWidget->setHorizontalHeaderLabels(QStringList() << tr("First") << tr("Second") << tr("Third") << tr("Fourth") << tr("Fifth")); ui->tableWidget->setColumnWidth(0, 80); ui->tableWidget->setColumnWidth(1, 140); ui->tableWidget->setColumnWidth(2, 80); ui->tableWidget->setColumnWidth(3, 140); ui->tableWidget->setStyleSheet("QHeaderView::section { background-color: rgb(217,217,217)}"); ui->tableWidget->horizontalHeader()->setStretchLastSection(true); for (int c = 0; c < ui->tableWidget->horizontalHeader()->count(); c++) { ui->tableWidget->horizontalHeaderItem(c)->setSizeHint(QSize(0, 50)); } for (int j = 0; j < ui->tableWidget->columnCount(); j++) { QTableWidgetItem *item = new QTableWidgetItem; if ((j == 0) || (j == 3) || (j == 4)) { item->setFlags(item->flags() & ~Qt::ItemIsEditable); } if (j!=0) { item->setTextAlignment(Qt::AlignCenter); } ui->tableWidget->setItem(0, j, item); }
There is only one row in the table, I do it with Designer, so there is no line for the number of row in my code above.
Result:With the same code above, I do in Visual Studio and receive this table:
The height of row with VS is the height of the table, which is too large. How can I receive the small row as in QtCreator ?
Best Regards.
-
Hi and welcome to devnet,
From the looks of it, you simply have an empty table. Add a row to it and you should see what you are looking for.
-
@SGaist thanks for your reply. Actually, this table is not empty, it has 1 row, as you can see in the result with Qt above. If I add one more row to it, the result with VS looks like this.
I tried to set the height of row, but it only works with QtCreator, with VS it is still the same. I am so headache with this problem. :(
-
Ok, got it.
Here's your guilty line:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
The horizontal header is the one for the rows. So you are asking that the last row takes as much vertical space as possible.
-
Thanks @SGaist, actually in my opinion, you mixed the horizontal header with verical header already. I thinks this line makes the last column (not the row) to stretch to fit the width of the table. If I remove this line, the new table will look like this.
I am so confused because never ever had this problem :(. I still don't think there is problem with my code, because it works well with QtCreator, but in VS problem appears.
-
Argle… Even after all these years I manage to get this one wrong…
One thing that I haven't seen in your code, where do you set the row count in your widget ?
-
Can you provide a minimal compilable example that reproduces that ?
-
@SGaist : I tested right on my code. If i add the line with RowCount in, in Qt Designer I dont set 1 row any more, the result still does not change. I still receive the table above.
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->tableWidget->setRowCount(1); ui->tableWidget->setColumnCount(5); ui->tableWidget->setHorizontalHeaderLabels(QStringList() << tr("First") << tr("Second") << tr("Third") << tr("Fourth") << tr("Fifth"));
-
Which OS are you running ?
Testing with:
QApplication app(argc, argv); QTableWidget tableWidget; tableWidget.setSelectionBehavior(QAbstractItemView::SelectRows); tableWidget.setSelectionMode(QAbstractItemView::SingleSelection); tableWidget.setRowCount(1); tableWidget.setColumnCount(5); tableWidget.setHorizontalHeaderLabels(QStringList() << QObject::tr("First") << QObject::tr("Second") << QObject::tr("Third") << QObject::tr("Fourth") << QObject::tr("Fifth")); tableWidget.show(); return app.exec();
shows the table widget as you want it.
-
@SGaist : I am using Windows 8.1, Visual Studio 2013, add-in Qt 5.5.1. If you use this code with just Qt Creator, it works. But i am coding with VS, the result is not good. If you can test it, please test with VS and shows me if you have the same result.
-
Sorry, I don't have that OS nor that version of Visual Studio at hand.
But I find it pretty surprising that the exact same code yields different result using the same compiler with only the IDE changing.
-
QTableWidget already has an internal model otherwise it's a QTableView.