Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved scrollbar not reappearing when a tableWidget is stretched then shrinked

    General and Desktop
    qtablewidget scrollbar resize
    2
    4
    446
    Loading More Posts
    • 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.
    • T
      torea last edited by

      Hi,
      I've been stuck for few hours on this problem and haven't found anything related on internet.

      I've made a derived class from QTableWidget in order to customize the resize event and stretch/shrink cells automatically as required. The cells can also be resized by the user and the soft tries to keep the size ratio.

      It works relatively correctly although I have a problem with the scrollbars.
      As expected, they disappear when the parent widget is stretched enough to display all the cells. But when the parent widget is shrinked, the scrollbars do not become visible automatically. For example, I have to play with the widget width so that the vertical scrollbar reappears.

      It's a small part of an old project, so unfortunately I am forced to use QtCreator 5.4.2.
      Could this be a limitation of this version or is my code the culprit:

      class TITableWidget : public QTableWidget{
      public:
          TITableWidget( int rows, int columns, QWidget * parent = 0 ) : QTableWidget( rows, columns, parent ){}
      
      protected:
          virtual void resizeEvent( QResizeEvent *event ){
      
              if( event->oldSize().width() == -1 && event->oldSize().height() == -1 ){
                  // first display
      
                  int colWidth = event->size().width() / columnCount();
                  if( colWidth < 100 ) colWidth = 100;
                  for( int i=0; i<columnCount(); ++i ) setColumnWidth( i, colWidth );
      
                  int colHeight = ( event->size().height() - horizontalHeader()->height() ) / rowCount();
                  if( colHeight < 50 ) colHeight = 50;
                  for( int i=0; i<rowCount(); ++i ) setRowHeight( i, colHeight );
      
                  return;
              }
      
              int totalWidth = 0;
              for( int i=0; i<columnCount(); ++i ) totalWidth += columnWidth(i);
              float availableWidth = (float)(event->size().width());
              if( verticalScrollBar()->isVisible() ) availableWidth -= (float)(verticalScrollBar()->geometry().width());   // 22
      
              if( totalWidth < availableWidth+50 && totalWidth > availableWidth-50 ){
                  // modify columns width only if the last one is already closed to the limit
                  float widthMul = availableWidth / (float)totalWidth;
                  for( int i=0; i<columnCount(); ++i ) {
                      int nlarg = (int)( (float)columnWidth(i) * widthMul );
                      if( nlarg < 100 ) nlarg = 100;
                      setColumnWidth( i, nlarg );
                  }
              }
              
              int totalHeight = 0;
              for( int i=0; i<rowCount(); ++i ) totalHeight += rowHeight( i );
              float availableHeight = (float)(event->size().height() - horizontalHeader()->height());
              if( horizontalScrollBar()->isVisible() ) availableHeight -= (float)(horizontalScrollBar()->geometry().height());
      
              if( totalHeight < availableHeight+50 && totalHeight > availableHeight-50 ){
                  float hautMul = availableHeight / (float)totalHeight;
                  for( int i=0; i<rowCount(); ++i ) {
                      int nhaut = (int)( (float)rowHeight(i) * hautMul );
                      if( nhaut < 50 ) nhaut = 50;
                      setRowHeight( i, nhaut );
                  }
              }
          }
      };
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        You should call the base class implementation.

        That said, isn't that a bit overkill ?
        You can use the automatic resize mode of the header with QHeaderView::ResizeToContents and switch to Interactive on user demand.

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

        T 1 Reply Last reply Reply Quote 2
        • T
          torea @SGaist last edited by

          Hi @SGaist
          Thank you for your comment!

          I'll try calling the base class implementation, and also the "less overkill" option.
          I'm on this project only at the end of the week so I'll post updates on the results in few days.

          1 Reply Last reply Reply Quote 0
          • T
            torea last edited by

            Calling the base class implementation did the trick. Thank you again!
            I didn't try the resizeToContents as some cells shouldn't be sized to their content in some context.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post