【问题标题】:Qt4 QTableWidget make Colums resized to contents, interactive and aligned to tableborderQt4 QTableWidget 使列调整为内容,交互并与表格边框对齐
【发布时间】:2013-08-15 08:45:43
【问题描述】:

这段代码

horizontalHeader()->setResizeMode(QHeaderView::Stretch);

拉伸qtablewidget 的列。我希望它们被拉伸,这意味着与qtablewidget 的边界对齐,不管它有多大。

我还希望它们不小于其内容并且可以由用户调整大小。

这意味着,我必须使用

horizontalHeader()->setResizeMode(QHeaderView::Stretch);
horizontalHeader()->setResizeMode(QHeaderView::Interactive);
horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);

一次,这是不可能的。

我知道我可以给每一列另一个视图,比如

horizontalHeader()->setResizeMode(0, QHeaderView::Interactive);
horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents);

但这不是我想要的。我希望列是

  • 不小于其内容
  • 可由用户调整大小
  • 对齐到 qtablewidget 的边框

有什么想法吗?

【问题讨论】:

    标签: c++ qt4 qtablewidget


    【解决方案1】:

    我认为你应该重新实现 sizeHintForColumn。下面的代码会给你一个开始。

    int TableWidget::sizeHintForColumn(int column) const  // to get resize on all rows in the column, i.e. not only visible rows.
    {
       if(d_resizeColumnsOnVisibleRowsOnly)
          return QTableView::sizeHintForColumn(column);
       if(!model())
          return -1;
       QStyleOptionViewItem option(viewOptions());
       int hint(0);
       QModelIndex index;
       QWidget* w(0);
       for(int row(0);row<rowCount();++row)
       {
           index=model()->index(row,column);
           w=cellWidget(row,column);
           int hint_for_row(qMax(itemDelegate(index)->sizeHint(option,index).width(),(w?w->sizeHint().width():0)));
           hint=qMax(hint,hint_for_row);
       }
       return showGrid()?hint+1:hint;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-30
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2013-05-06
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多