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. setColumnWidth() not working

setColumnWidth() not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 7.3k Views 2 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    This is just a test:

        ui->tableView->setColumnWidth(18, 60);
        for (int i = 0; i <= 20; ++i)
        {
            int j = ui->tableView->columnWidth(i);
            if (j > 0)
            {
                qDebug() << i << j;
            }
        }
    

    Output is:

    7 105
    8 105
    12 104
    18 104
    

    (Four columns are visible.) Can someone see what I'm doing wrong? Thanks.

    kshegunovK 1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, if you're on Qt 5.11 or later it could be because of this feature

      1 Reply Last reply
      5
      • mzimmersM mzimmers

        This is just a test:

            ui->tableView->setColumnWidth(18, 60);
            for (int i = 0; i <= 20; ++i)
            {
                int j = ui->tableView->columnWidth(i);
                if (j > 0)
                {
                    qDebug() << i << j;
                }
            }
        

        Output is:

        7 105
        8 105
        12 104
        18 104
        

        (Four columns are visible.) Can someone see what I'm doing wrong? Thanks.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        Try doing that after you've populated the view with the data. At least that's what an old thread on QtCentre claims to have worked.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          I tried the workaround mentioned in the article that hskoglund referenced; that didn't change anything. I've also already set the model on the table view prior to this.

          This line appears to have been the culprit:

              ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
          

          Removing it allows the column resizing, but...then I don't get the stretch. I guess I can't have everything. I just wanted to make the first column a little wider, as its data contains more characters.

          kshegunovK 1 Reply Last reply
          0
          • mzimmersM mzimmers

            I tried the workaround mentioned in the article that hskoglund referenced; that didn't change anything. I've also already set the model on the table view prior to this.

            This line appears to have been the culprit:

                ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
            

            Removing it allows the column resizing, but...then I don't get the stretch. I guess I can't have everything. I just wanted to make the first column a little wider, as its data contains more characters.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @mzimmers said in setColumnWidth() not working:

            Removing it allows the column resizing, but...then I don't get the stretch. I guess I can't have everything. I just wanted to make the first column a little wider, as its data contains more characters.

            Then you may want QHeaderView::setResizeMode with a QHeaderView::ResizeToContents argument, or QTableView::resizeColumnToContents.

            Read and abide by the Qt Code of Conduct

            mzimmersM 1 Reply Last reply
            2
            • kshegunovK kshegunov

              @mzimmers said in setColumnWidth() not working:

              Removing it allows the column resizing, but...then I don't get the stretch. I guess I can't have everything. I just wanted to make the first column a little wider, as its data contains more characters.

              Then you may want QHeaderView::setResizeMode with a QHeaderView::ResizeToContents argument, or QTableView::resizeColumnToContents.

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              @kshegunov that didn't work quite as I'd hoped:

              0_1542756101043_wifi.PNG

              I'm guessing that the problem now is that when the UI starts, the first row (not the header row) is unpopulated, so it uses the length of the header fields to set the column widths. Not sure what to do about this -- maybe call the resize routine again in a slot that is invoked when the model emits a dataChanged() signal?

              kshegunovK 1 Reply Last reply
              0
              • S Offline
                S Offline
                Slawomir_Piernikowski
                wrote on last edited by Slawomir_Piernikowski
                #7

                If you want the first column wider set this column to be wider:
                ui->tableView->setColumnWidth(nfirst column is 0, expected width of the column);

                if you want to resize all the columns to customised size:

                auto model = new QStandardItemModel();
                ui->tableView->setModel(model);
                this ->setCentralWidget(ui->tableView);
                QHeaderView *headerView = new QHeaderView(Qt::Horizontal, this);
                ui->tableView->setHorizontalHeader(headerView);

                for(int i = 0; i < 20; i++)
                {
                    model->setHorizontalHeaderItem(i, new QStandardItem(QString("Column %1").arg(i)));
                
                    ui->tableView->setColumnWidth(i, 80);
                    int j = ui->tableView->columnWidth(i);
                    qDebug() << i << j;
                }
                

                Debuger data: column number width of the column
                0 80
                1 80
                2 80
                3 80
                4 80
                5 80
                6 80
                7 80
                8 80
                9 80
                10 80
                11 80
                12 80
                13 80
                14 80
                15 80
                16 80
                17 80
                18 80
                19 80
                ...and the view:

                0_1542757362096_Tab.png

                You can also to do only some column to be the same size.

                1 Reply Last reply
                2
                • mzimmersM mzimmers

                  @kshegunov that didn't work quite as I'd hoped:

                  0_1542756101043_wifi.PNG

                  I'm guessing that the problem now is that when the UI starts, the first row (not the header row) is unpopulated, so it uses the length of the header fields to set the column widths. Not sure what to do about this -- maybe call the resize routine again in a slot that is invoked when the model emits a dataChanged() signal?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @mzimmers said in setColumnWidth() not working:

                  maybe call the resize routine again in a slot that is invoked when the model emits a dataChanged() signal?

                  Yes, that can be done, but it'd depend on how your model is structured. If it reloads itself every time you could connect it to the QAbstractItemModel::modelReset signal. That usually isn't the case though. Connecting to the data changed signal is possible, but if your table is large this may be rather expensive.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #9

                    The table isn't too big (~30 columns, and probably at most 30 rows), but maybe this isn't a good idea. I can live with statically setting the widths. I realize that's not a good idea generally, but since I know with a high degree of precision what the contents will be, in this case it's probably OK.

                    1 Reply Last reply
                    0

                    • Login

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