Problem resizeing Qtablerow row hight



  • i have Qtableview with QSqlQueryModel as the model ( fatching data from sqlight)
    and QStyledItemDelegate sub class as delegate for the items ,
    i set the table like this : ( looking in the web for few suggestions ) .
    but the method sizeHint never called ...

    @//did this also but its not working
    //QHeaderView* headerView = ui->PList_tableView->horizontalHeader();
    //headerView->setResizeMode(QHeaderView::ResizeToContents);

    m_pMiniItemDelegate =new MiniItemDelegate(this);
    ui->PList_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->PList_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->PList_tableView->setItemDelegate(m_pMiniItemDelegate);
    ui->PList_tableView->setAlternatingRowColors(true);
    ui->PList_tableView->setModel(ListMiniSqlModel::instance());
    //also not work
    ui->PList_tableView->resizeColumnsToContents();
    ui->PList_tableView->resizeRowsToContents();

    // MiniItemDelegate sizeHint
    // this method never called if i set break point

    QSize MiniItemDelegate::sizeHint(const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    //just for testing
    return QSize( 256,256);

    // return QStyledItemDelegate::sizeHint(option, index);

    }@



  • If I right - resizeColumnsToContents() and resizeRowsToContents() work with role SizeHintRole in model, and I suggest You may redefine data () for model.



  • i sub class QStyledItemDelegate , there is no data() method to inherit
    how can i set the Qt::SizeHintRole?



  • I may mistakes but delegate is not it! I use delegate too, but for resize I set my model:

    @
    class CDrugInfoModel : public QAbstractTableModel;

    .....

    QVariant CDrugInfoModel::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant ();
    if (role == Qt::EditRole || role == Qt::DisplayRole)
    {
    // if (index.column() == 0)
    return catalog->getData(index.row(), index.column());

        //return catalog->getAttr(catalog->getNameAttrs(index.row()));
    }
    else if (role == Qt::FontRole)
    {
        return  CProgSettings::getFont();//QFont ("Tahoma", 15);
    }
    else if (role == Qt::ToolTipRole)
    {
        return catalog->getData(index.row(), index.column());
    }
    

    // else if (role == Qt::BackgroundColorRole)
    // return QColor(255, 255, 255);
    if (role == Qt::SizeHintRole)
    {
    / / Calculate the best size
    / / max length of 200, if more, then transfer the
    //QFont f ("Tahoma", 15);
    if (index.column() == 0)
    return QSize(300, 35);
    double maxWidth = 800.00;
    // if (index.column() == 2 )
    // maxWidth = 50;
    // else if ( index.column() == 7)
    // maxWidth = 170;

        QFontMetrics fm (CProgSettings::getFont());
        QString text = catalog->getData(index.row(), index.column()).toString(); //listItem->getVal(index.row(), index.column()).toString();
        //qDebug()<<text;
        bool singleWord = false;
        if (text.indexOf(" ") == -1)
            singleWord  = true;
    
        int w = fm.width (text) + 20 ;
        int h = fm.height();
        if (w > maxWidth && w < (maxWidth + 5))
            maxWidth = w;
    

    // if (index.column() == 1)
    // return QSize (100, h2);
    // if (w > 400)
    // text = "";
    if ( (w/maxWidth) > 1 && !singleWord)
    {
    double ost = w / maxWidth;
    h = h
    (ost + 2); //(w/200);
    w = maxWidth;
    }
    return QSize (w + 50, h);
    }
    return QVariant ();
    }@



  • Thanks for the fast reply but ..
    when i set simple test just to see if the role == Qt::SizeHintRole ever getting there
    (using break point ) it never gets there . never entring to the if (role == Qt::SizeHintRole)
    this is how it looks only for testing ....

    @QVariant ListMiniSqlModel::data(const QModelIndex &index, int role) const
    {
    QVariant value;
    value = QSqlQueryModel::data(index, role);

    if (role == Qt::SizeHintRole)
    {
    return QSize (100,100); // never gets here ....
    }
    return value;
    }@

    by the way the data is presented great so its working


Log in to reply
 

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