这段代码
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/