QTableWidget: smaller text when it doesn't fit it a column



  • Hey guys,

    When text doesn't fit in a column it automatically gets smaller and three dots are appended at the end. Like this:
    alt text

    Now I would like to understand why the text actually gets smaller and how can I control how small (or not) it gets.
    Also, as you can see on the image, since it gets smaller it ends up not using the whole width of that column which makes no sense at all.



  • I see no reason (looking in QCommonStylePrivate::viewItemDrawText) why it should get smaller, it might be a bug in your platform's style.

    EDIT

    the font getting smaller is a reported bug. tableWidget->setFont(QApplication::font()); seams to be enough to fix it

    To get rid of eliding altogether just subclass QStyledItemDelegate and reimplement paint like:

    #include <QStyledItemDelegate>
    class NoElideDelegate : public QStyledItemDelegate{
    Q_OBJECT
    Q_DISABLE_COPY(NoElideDelegate)
    public:
    explicit NoElideDelegate(Qobject* parent = Q_NULLPTR):QStyledItemDelegate(parent){}
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
    {
        Q_ASSERT(index.isValid());
    
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);
    
        //This is the change that prevents elision
        opt.textElideMode = Qt::ElideNone;
    
        const QWidget *widget = option.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    }
    };
    

    and then use it like
    tableWidget->setItemDelegate(new NoElideDelegate(tableWidget));



  • Oh great! That indeed solves the problem. Thanks @VRonin !


Log in to reply