在单个列标题的边缘上进行双重调整时,会调整该列的大小以适合内容。 如果选择多列,当双击任何所选列标题的边缘时,如何调整所有选定列的大小?
答案 0 :(得分:1)
QHeaderView::sectionHandleDoubleClicked
信号QHeaderView::setResizeMode
和QHeaderView::ResizeToContents
QHeaderView::Interactive
或其他任何内容。QHeaderView::resizeSection
设置新的绝对列大小。一个例子:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QStandardItemModel *model = new QStandardItemModel(this);
for (int i = 0; i < 10; ++i)
{
QList<QStandardItem*> items;
for (int j = 0; j < 4; ++j)
{
QStandardItem *item = new QStandardItem;
QChar ch = QString::number(j).at(0);
item->setText(QString().fill(ch, 20));
items << item;
}
model->appendRow(items);
}
view = new QTableView;
view->setModel(model);
connect(view->horizontalHeader(), SIGNAL(sectionHandleDoubleClicked(int)),
this, SLOT(sectionHandleDoubleClicked(int)));
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(view);
}
void Widget::sectionHandleDoubleClicked(int logicalIndex)
{
QHeaderView *header = view->horizontalHeader();
QModelIndexList list = view->selectionModel()->selectedIndexes();
QSet<int> columns;
Q_FOREACH (QModelIndex index, list)
{
if (index.isValid())
{
columns.insert(index.column());
}
}
Q_FOREACH (int column, columns)
{
header->setResizeMode(column, QHeaderView::ResizeToContents);
int newWidth = header->sectionSize(column);
header->setResizeMode(column, QHeaderView::Interactive);
header->resizeSection(column, newWidth);
}
}