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. QTableWidget: Prioritize horizontal space for a specific column?
Forum Updated to NodeBB v4.3 + New Features

QTableWidget: Prioritize horizontal space for a specific column?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 839 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.
  • P Offline
    P Offline
    pearse
    wrote on 9 Mar 2021, 00:08 last edited by
    #1

    I have a QTableWidget that has a column (#3) that needs more space than others. I want to resize all columns to their contents, and give priority to column #3. If column #3 pushes the table's width past what's available, I want column #3 to be truncated with '...' without a horizontal scrollBar.

    The screenshot below is the simplest example of the behavior I'm chasing, but I've had to manually adjust the column widths. I want the table to do this automatically.

    alt text

    Note: These functions do not achieve what I'm aiming for. I want to stretch column 3 to take up the most horizontal space, but if it takes up too much space, then truncate it with elipses (aka "..."), and do not expand the table so that it has a horizontal scrollbar.

    table->horizontalHeader()->setStretchLastSection(true);
    table->resizeColumnsToContents();
    

    The following code shows examples of what I've tried on QTableWidget, but none have worked. Thank you to anyone who volunteers your time to help me with this.

    #include <QApplication>
    #include <QTableWidget>
    #include <QStringList>
    #include <QRect>
    #include <QLayout>
    #include <QDialog>
    #include <QHeaderView>
    
    #include <iostream>
    
    int main( int argc, char* argv[] )
    {
        QApplication a(argc, argv);
    
        QDialog* d = new QDialog();
        d->setLayout( new QVBoxLayout() );
    
        QTableWidget* table = new QTableWidget(1,4);
        QStringList headers = {"1", "2", "3", "4"};
        table->setHorizontalHeaderLabels(headers);
    
        table->setItem(0, 0, new QTableWidgetItem("1"));
        table->setItem(0, 1, new QTableWidgetItem("22222"));
        table->setItem(0, 2, new QTableWidgetItem("33333333333333333333333333333"));
        table->setItem(0, 3, new QTableWidgetItem("4"));
    
        // Do nothing
        //
        //   The table exceeds the dimensions of the dialog,
        //   and we get a horizontal scrollbar
    
        // This also results in a horizontal scrollbar
        //
        //  table->horizontalHeader()->setStretchLastSection(true);
    
        // Resizing the columns introduces a horizontal scrollbar, and
        // prevents the user from from changing column width
        //
        //  table->resizeColumnsToContents();
    
        // The table fits, but all columns are equally spaced.
        // (We want column 3 to take up as much space as possible)
        //
        // table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    
        // Columns are resized to their contents,
        // but column 3 is not truncated and we get a horizontal scrollBar
        //
        //  table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    
        d->layout()->addWidget( table );
        d->show();
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
    • N Offline
      N Offline
      nagesh
      wrote on 9 Mar 2021, 00:31 last edited by
      #2

      @pearse I hope this works for you

       table->horizontalScrollBar()->setEnabled(false);
      
       table->resizeColumnsToContents();
       
       QHeaderView *headerView = table->horizontalHeader();
       headerView->setSectionResizeMode(2, QHeaderView::Stretch);
      
      1 Reply Last reply
      0
      • P Offline
        P Offline
        pearse
        wrote on 10 Mar 2021, 14:11 last edited by
        #3

        @nagesh said in QTableWidget: Prioritize horizontal space for a specific column?:

        table->horizontalScrollBar()->setEnabled(false);

        table->resizeColumnsToContents();

        QHeaderView *headerView = table->horizontalHeader();
        headerView->setSectionResizeMode(2, QHeaderView::Stretch);

        Thank you

        1 Reply Last reply
        0

        1/3

        9 Mar 2021, 00:08

        • Login

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