如何将QTreeWidget中的列调整为所需的最小空间 [英] How to resize columns in QTreeWidget to the minimum space required
问题描述
我是一名学生程序员,我正在使用Qt构建工作的GUI界面。我有一个总共8列的 QTreeWidget
。需要输入到这些列中的每一列的输入都很小。在这方面,我试图找到一种方法来重新调整所有列的大小以满足所需的最小空间。很像您点击分隔线时执行的操作。我浏览过几个网站和Qt文档,但没有找到解决方案。看过文档后,似乎有很多方法可以做到这一点,但我无法让其工作。我开始认为我需要在某处启用/禁用某些内容。就好像我写入到构建函数中的更改(此函数清除表;从我的向量读取数据,在表中显示某些数据,然后假设重新调整标题/列的大小以适合大小以适合该列的内容)甚至不适用。我已经尝试了以下内容,不加改变:
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 ++)
{
//用项目填充Qtree;
//用数据填充QTree项目;
// re size函数在这里;
}
请仅留下有用的反馈意见,因为我只想学习和克服这个挑战。
这个最简单的例子适用于我:
MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
UI-> setupUi(本);
QVBoxLayout * layout = new QVBoxLayout(centralWidget());
QTreeWidget * tree = new QTreeWidget(this);
layout-> addWidget(tree);
tree-> setColumnCount(3);
QStringList第一,第二,第三;先
<< xxxxxxxxx<< xxxxxxxxxxxxx<< XXXXXXXX;
秒<< xxxxx<< xxxxxxxxxxxxxxxxx<< XXXXX;
第三<< xxxxxxxxxxxxxx<< xxxxxxxxxx<< XXXXXXXX;
tree-> insertTopLevelItem(0,new QTreeWidgetItem(first));
tree-> insertTopLevelItem(0,new QTreeWidgetItem(second));
tree-> insertTopLevelItem(0,new QTreeWidgetItem(third)); (int i = 0; i <3; i ++)
tree-> resizeColumnToContents(i);
$ b你只需要调用resizeColumnToContents( )填充视图后,为每一列添加一次
方法。让我知道问题是否仍然存在。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!
解决方案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.这篇关于如何将QTreeWidget中的列调整为所需的最小空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!