如何将 QTreeWidget 中的列大小调整为所需的最小空间

发布于 2024-12-28 13:43:40 字数 995 浏览 0 评论 0原文

我是一名学生程序员,我正在使用 Qt 构建 GUI 界面来工作。我有一个总共有 8 列的 QTreeWidget。需要输入到每一列中的输入相当小。在这方面,我试图找到一种方法来将所有列的大小调整为所需的最少空间。与单击分隔线时执行的操作非常相似。我浏览了几个网站和 Qt 文档,但没有找到解决方案。查看文档后,似乎有很多方法可以做到这一点,但我无法让其中一种方法工作。我开始认为我需要在某个地方启用/禁用某些东西。就好像我写入构建函数的更改一样(此函数清除表格;从向量中读取数据,在表格​​中显示某些数据,然后假设将标题/列的大小调整为足够的大小以适应该列的内容)甚至没有应用。我已经尝试了以下操作,没有进行任何更改:

        ui->treeWidgetInjections->setColumnWidth(i, minimumWidth());
        ui->treeWidgetInjections->resizeColumnToContents(i);
        ui->treeWidgetInjections->header()->ResizeToContents;
        ui->treeWidgetInjections->header()->resizeSection(i, minimumWidth());

所有这些都在我的构建功能的末尾(删除了不相关的信息):

ui->treeWidgetInjections->clear();
    for (int i=0; i < qTreeInjectionData.size(); i++)
        {
            //fill Qtree with items;
            //fill QTree items with data;
            //re size function here;
        }

请只留下富有成效的反馈,因为我只对学习和克服这一挑战感兴趣。提前致谢!

I am a student programmer and I am using Qt to build a GUI interface for work. I have a QTreeWidget that has a total of 8 Columns. The input that needs to be entered into each one of these columns is fairly small. In that regard, I was trying to find a way to re-size all of the columns to the least amount of space required. much like the action that is performed when you click on the dividers. I have looked over several websites and the Qt documentation and haven't found a solution. After looking over the documentation it seems that there are a bunch of ways to do this but I cant get one to work. I am beginning to think that I need to enable/disable something somewhere. It's as if the changes I write in to my build function (This function clears the table; Reads data from my vector, displays certain data in the table, and then is suppose to re-size the headers/columns to just enough size to fit the contents of the column) are not even applied. I have tried the following with no changes:

        ui->treeWidgetInjections->setColumnWidth(i, minimumWidth());
        ui->treeWidgetInjections->resizeColumnToContents(i);
        ui->treeWidgetInjections->header()->ResizeToContents;
        ui->treeWidgetInjections->header()->resizeSection(i, minimumWidth());

All of these are in the end of of my build Function (unrelated info removed):

ui->treeWidgetInjections->clear();
    for (int i=0; i < qTreeInjectionData.size(); i++)
        {
            //fill Qtree with items;
            //fill QTree items with data;
            //re size function here;
        }

Please only leave productive feedback as I am only interested in learning and overcoming this challenge. Thanks in advance!

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

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

发布评论

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

评论(2

得不到的就毁灭 2025-01-04 13:43:40

这个最小的示例对我有用:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QVBoxLayout *layout = new QVBoxLayout(centralWidget());
    QTreeWidget *tree = new QTreeWidget(this);
    layout->addWidget(tree);

    tree->setColumnCount(3);

    QStringList first, second, third;
    first  << "xxxxxxxxx"      << "xxxxxxxxxxxxx"     << "xxxxxxxx";
    second << "xxxxx"          << "xxxxxxxxxxxxxxxxx" << "xxxxx";
    third  << "xxxxxxxxxxxxxx" << "xxxxxxxxxx"        << "xxxxxxxx";

    tree->insertTopLevelItem(0, new QTreeWidgetItem(first));
    tree->insertTopLevelItem(0, new QTreeWidgetItem(second));
    tree->insertTopLevelItem(0, new QTreeWidgetItem(third));

    for(int i = 0; i < 3; i++)
        tree->resizeColumnToContents(i);
}

您所需要做的就是在填充视图后为每列调用一次 resizeColumnToContents() 方法。如果问题仍然存在,请告诉我。

This minimal example works for me:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QVBoxLayout *layout = new QVBoxLayout(centralWidget());
    QTreeWidget *tree = new QTreeWidget(this);
    layout->addWidget(tree);

    tree->setColumnCount(3);

    QStringList first, second, third;
    first  << "xxxxxxxxx"      << "xxxxxxxxxxxxx"     << "xxxxxxxx";
    second << "xxxxx"          << "xxxxxxxxxxxxxxxxx" << "xxxxx";
    third  << "xxxxxxxxxxxxxx" << "xxxxxxxxxx"        << "xxxxxxxx";

    tree->insertTopLevelItem(0, new QTreeWidgetItem(first));
    tree->insertTopLevelItem(0, new QTreeWidgetItem(second));
    tree->insertTopLevelItem(0, new QTreeWidgetItem(third));

    for(int i = 0; i < 3; i++)
        tree->resizeColumnToContents(i);
}

All you need to do is call the resizeColumnToContents() method once for every column after you populate the view. Let me know if the problem persists.

笑饮青盏花 2025-01-04 13:43:40

对我来说,问题是 StretchLastSection 设置为 true,如果该值设置为 true,则此属性将覆盖标题中最后一个部分设置的调整大小模式。

解决方案:

_treeWidget = new UIWidgetSceneTree();
_treeWidget->header()->setStretchLastSection(false);
_treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

The problem for me was that StretchLastSection was set to true and if this value is set to true, this property will override the resize mode set on the last section in the header.

Solution:

_treeWidget = new UIWidgetSceneTree();
_treeWidget->header()->setStretchLastSection(false);
_treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文