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. Problem resizeing Qtablerow row hight
Forum Updated to NodeBB v4.3 + New Features

Problem resizeing Qtablerow row hight

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.7k Views 1 Watching
  • 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.
  • U Offline
    U Offline
    umen242
    wrote on 20 Mar 2012, 06:58 last edited by
    #1

    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);

    }@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kunashir
      wrote on 20 Mar 2012, 07:11 last edited by
      #2

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

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on 20 Mar 2012, 09:12 last edited by
        #3

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kunashir
          wrote on 20 Mar 2012, 09:24 last edited by
          #4

          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 ();
          }@

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on 20 Mar 2012, 09:43 last edited by
            #5

            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

            1 Reply Last reply
            0

            5/5

            20 Mar 2012, 09:43

            • Login

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