c++ - Qt4 QTableWidget 使 Column resize to contents, interactive and aligned to table border

标签 c++ qt4 qtablewidget

这段代码

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

拉伸(stretch) qtablewidget 的列。我希望它们被拉伸(stretch),这意味着与 qtablewidget 的边界对齐,无论它有多大。

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

这意味着,我将不得不使用

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

一次,这是不可能的。

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

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

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

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

有什么想法吗?

最佳答案

我认为您应该重新实现 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;
}

关于c++ - Qt4 QTableWidget 使 Column resize to contents, interactive and aligned to table border,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18249194/

相关文章:

python - 检索选定的记录

c++ - 运行简单的源外 LLVM pass 的问题

python - 如何反转 warpPerspective()

c++ - 使用 C++ 访问 iOS 上的文档文件夹

ruby - QtRuby 使用参数/参数连接信号和槽

python - 如果大小写与现有项目不同,QComboBox 会替换编辑文本

python - 右键单击QTableWidget获取标题列

c++ - 将二维数组作为参数传递

c++ - 包含另一个 CMake 项目作为库

c++ - 删除第一行时 selectionChanged() 的行为