Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set height of row of table
QtWS25 Last Chance

Set height of row of table

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 5.5visual studioqtablewidgetqtableview
14 Posts 2 Posters 7.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nberN Offline
    nberN Offline
    nber
    wrote on last edited by nber
    #1

    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:

    Qt

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      nberN 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        nberN Offline
        nberN Offline
        nber
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          nberN 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            nberN Offline
            nberN Offline
            nber
            wrote on last edited by
            #5

            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.

            Qt

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • nberN Offline
                nberN Offline
                nber
                wrote on last edited by nber
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Can you provide a minimal compilable example that reproduces that ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  nberN 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Can you provide a minimal compilable example that reproduces that ?

                    nberN Offline
                    nberN Offline
                    nber
                    wrote on last edited by nber
                    #9

                    @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"));
                    
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      nberN 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        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.

                        nberN Offline
                        nberN Offline
                        nber
                        wrote on last edited by nber
                        #11

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          nberN 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            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.

                            nberN Offline
                            nberN Offline
                            nber
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved