QTableWidget: prevent collumns to be cropped when resized



  • Dear All,
    I'm trying to find out how to get proper column resizing when the parent (a QDockWidget) is resized itself.
    Here is what I currently get:
    !http://i.imgur.com/poSxkRg.png! !http://i.imgur.com/H03LQLf.png!

    I tried all the sizePolicies on both the QTableWidget and its layout, but I always get the same behavior.
    Any help will be much appreciated!



  • What would you want it to do?



  • I would like it to internally keep a minimumWidth reflecting current data.
    That would forbid the user to resize the width below the value where the cropping occurs.
    That is the standard behavior of other QWidgets. If you add a QLabel in a QDockWidget, you cannot crop it when resizing: here in that snapshot this is the minimum width the user can set:
    !http://i.imgur.com/70sjPwi.png!



  • I think I'd subclass QTableWidget, and reimplement the sizeHint. In your reimplementation, you can use QTabelView::sizeHintForColumn to get the optimal width for each of your columns. Sum them, and if needed, add the width of the scroll bar.



  • Subclassing just for this reason is a bit annoying.



  • Then set minimumSize with the results of the same calculation...



  • OK, I took the opportunity to learn how the "promote to" function (in Designer) works.

    I came up with that but it does not work. minimumSizeHint is not called when resizing ...
    EDIT: I had to reimplement resizeEvent to get it working. Not sure if it's the proper way to do it.

    @class MyTableWidget : public QTableWidget
    {
    public:
    MyTableWidget(QWidget* parent = NULL) : QTableWidget(parent) {}

    QSize minimumSizeHint() const
    {
        QSize size(QTableWidget::sizeHint());
    
        int width = verticalHeader()->isVisible() ? verticalHeader()->width() : 0;
    
        // Sum over all the columns width
        for( int c=0; c<columnCount(); ++c )
            width += isColumnHidden(c) ? 0 : sizeHintForColumn(c);
    
        // Handle vertical scroll bar
        width += verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0;
    
        size.setWidth(width);
        return size;
    }
    
    QSize sizeHint() const
    {
        return minimumSizeHint();
    }
    

    protected:
    void resizeEvent(QResizeEvent* e)
    {
    setMinimumSize(minimumSizeHint());
    QTableWidget::resizeEvent(e);
    }
    };@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.