Qt setColumnWidth 不起作用

发布于 2024-09-08 02:23:20 字数 682 浏览 6 评论 0原文

已写出以下代码:

m_selectCategoryTableWidget = new QTableWidget;
m_selectCategoryTableWidget->setRowCount(0);
m_selectCategoryTableWidget->setColumnCount(2);

m_selectCategoryTableWidget->setHorizontalHeaderLabels(QStringList()<<tr("Category")<<tr("Number of items"));
m_selectCategoryTableWidget->verticalHeader()->setVisible(false);
m_selectCategoryTableWidget->horizontalHeader()->setStretchLastSection(true);
//m_selectCategoryTableWidget->setColumnWidth(0,400);
m_selectCategoryTableWidget->resizeColumnsToContents();
m_selectCategoryTableWidget->setColumnWidth(1,100); //this does not take effect

请帮忙。

Have written the following code:

m_selectCategoryTableWidget = new QTableWidget;
m_selectCategoryTableWidget->setRowCount(0);
m_selectCategoryTableWidget->setColumnCount(2);

m_selectCategoryTableWidget->setHorizontalHeaderLabels(QStringList()<<tr("Category")<<tr("Number of items"));
m_selectCategoryTableWidget->verticalHeader()->setVisible(false);
m_selectCategoryTableWidget->horizontalHeader()->setStretchLastSection(true);
//m_selectCategoryTableWidget->setColumnWidth(0,400);
m_selectCategoryTableWidget->resizeColumnsToContents();
m_selectCategoryTableWidget->setColumnWidth(1,100); //this does not take effect

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪梦之恋 2024-09-15 02:23:20

嗯,Qt 的逻辑是这样的,在调整列大小后,滚动条区域检查列如何适合它。如果所有列宽度的总和小于小部件的可见宽度,则调整最后一列的大小以填充空间,从而导致调用 setColumnWidth() 没有可见结果。实际上会发生两种大小调整:缩小和反向放大。

因此,教训是 - 获取控件的可见宽度,根据需要重新计算大小,然后调整除最后一列之外的所有列的大小。对于两列的情况,这非常简单:

int secondColumnWidth = 100;
int firstColumnWidth = m_selectCategoryTableWidget->width() - secondColumnWidth;

if (firstColumnWidth > 0)
{
    m_selectCategoryTableWidget->setColumnWidth(0, firstColumnWidth);
}
else
{
    m_selectCategoryTableWidget->resizeColumnsToContents();
}

祝你好运!

Well, Qt's logic is so, that after column resize, scroll bar area checks how columns fit into it. And if the sum of all columns' widths is less than the widget's visible width, then the last column gets resized to fill up the space leading to no visible result of calling setColumnWidth(). Actually two resizes happen - to shrink and reverse to enlarge.

So, the lesson is - get control's visible width, recalculate sizes as you want, and resize all but the last column. For two column case it's really simple:

int secondColumnWidth = 100;
int firstColumnWidth = m_selectCategoryTableWidget->width() - secondColumnWidth;

if (firstColumnWidth > 0)
{
    m_selectCategoryTableWidget->setColumnWidth(0, firstColumnWidth);
}
else
{
    m_selectCategoryTableWidget->resizeColumnsToContents();
}

Good luck!

简单 2024-09-15 02:23:20

还可以指定您希望第一列而不是最后一列填充剩余空间。不幸的是,这似乎确实阻止了用户手动调整列的大小。

int secondColumnWidth = 100;
m_selectCategoryTableWidget->header()->setStretchLastSection(false);
m_selectCategoryTableWidget->header()->setResizeMode(0, QHeaderView::Stretch);
m_selectCategoryTableWidget->setColumnWidth(1, secondColumnWidth);

It is also possible to specify that you want the first column to fill the remaining space instead of the last column. Unfortunately this does seem to prevent the user from being able to manually resize the columns.

int secondColumnWidth = 100;
m_selectCategoryTableWidget->header()->setStretchLastSection(false);
m_selectCategoryTableWidget->header()->setResizeMode(0, QHeaderView::Stretch);
m_selectCategoryTableWidget->setColumnWidth(1, secondColumnWidth);
黯然 2024-09-15 02:23:20

这将自动调整列的大小以适应(“视图”是 QTableView*,模型是 QSqlQueryModel*)。

static_cast<QTableView*>(view)->horizontalHeader()
        ->resizeSections(QHeaderView::ResizeToContents);

QFontMetrics fm(view->font());

for (int i = 0 ; i < model->record().count(); ++i)
{
    int maxLength = 0;

    for (int j = 0; j < model->rowCount(); ++j)
    {
        QString cell = model->record(j).value(i).toString();

        if (fm.width(cell) > maxLength)
        {
            maxLength = fm.width(cell);
        }
    }
    QHeaderView& hv = *static_cast<QTableView*>(view)->horizontalHeader();

    if (maxLength > hv.sectionSize(i))
    {
        hv.resizeSection(i, maxLength * 1.5);
    }
}

This will automatically resize the columns to fit ("view" is an QTableView* and model is a QSqlQueryModel*).

static_cast<QTableView*>(view)->horizontalHeader()
        ->resizeSections(QHeaderView::ResizeToContents);

QFontMetrics fm(view->font());

for (int i = 0 ; i < model->record().count(); ++i)
{
    int maxLength = 0;

    for (int j = 0; j < model->rowCount(); ++j)
    {
        QString cell = model->record(j).value(i).toString();

        if (fm.width(cell) > maxLength)
        {
            maxLength = fm.width(cell);
        }
    }
    QHeaderView& hv = *static_cast<QTableView*>(view)->horizontalHeader();

    if (maxLength > hv.sectionSize(i))
    {
        hv.resizeSection(i, maxLength * 1.5);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文