Set height of row of table

  • I want to create a table, my code in QtCreator:

    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)}");
    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) {
        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.


    With the same code above, I do in Visual Studio and receive this table:

    visual studio

    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.

  • Lifetime Qt Champion

    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. Qt

    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. :(

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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 ?

  • @SGaist : actually as I said, I did not do it in the code, I do it with QtDesigner, I set only 1 row. But I don't think it will create the difference between code and Qt Designer.

  • Lifetime Qt Champion

    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->setHorizontalHeaderLabels(QStringList() << tr("First") << tr("Second") << tr("Third") << tr("Fourth") << tr("Fifth"));

  • Lifetime Qt Champion

    Which OS are you running ?

    Testing with:

        QApplication app(argc, argv);
        QTableWidget tableWidget;
        tableWidget.setHorizontalHeaderLabels(QStringList() << QObject::tr("First") <<  QObject::tr("Second") <<  QObject::tr("Third") <<  QObject::tr("Fourth") <<  QObject::tr("Fifth"));;
        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.

  • Lifetime Qt Champion

    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.

  • @SGaist : until now I still dont know why. Maybe I have to create a model and using it...

  • Lifetime Qt Champion

    QTableWidget already has an internal model otherwise it's a QTableView.