QTableWidget:如何用更少的垂直间距填充获得更紧的线条?

发布于 2024-08-20 01:38:09 字数 408 浏览 9 评论 0原文

QTableWdiget 非常适合简单的网格显示。更改颜色、字体等非常简单。

然而,我没有设法让网格看起来更“紧密”,垂直空白更少。我看到 Qt 文档讨论了(例如此处)关于

  • 边距
  • 边框
  • 填充

小部件周围的 ,但是当我设置这些时,我只能在整个网格小部件周围而不是内部进行更改。

如何直接设置它(使用样式表或硬编码选项)以使 QTableWidget 显示更紧密?

The QTableWdiget is fabulous for simple grid displays. Changing colors, fonts, etc is straightforward.

However, I did not manage to give the grid a 'tighter' look with less vertical whitespace. I see that the Qt documentation talks (eg here) about

  • margin
  • border
  • padding

around widgets, but when I set these I only get changes around the entire grid widget rather than inside.

How can I set this (with a style sheet, or hard-coded options) directly to make the QTableWidget display tighter?

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

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

发布评论

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

评论(5

独享拥抱 2024-08-27 01:38:09

获得“h”的代码可能不健全。这只是一个例子。复制&粘贴以下相当基本的代码。更改“setDefaultSectionSize()”中的值,重新编译并运行。您应该看到差异。将其设置为 10 或 50 会产生明显的结果。在上面的代码中,QFontMetrics 或 QFont 可能搞砸了。

您可以使用任何您想要的高度,但字体大小最有意义。

#include <QtGui>

int main( int argc, char* argv[] )
{
 QApplication app( argc, argv );

 QDialog* my_dialog  = new QDialog();
 QHBoxLayout* layout  = new QHBoxLayout();
 QTableWidget* my_table_widget = new QTableWidget( my_dialog );

 my_table_widget->setRowCount( 10 );
 my_table_widget->setColumnCount( 10 );
 my_table_widget->verticalHeader()->setDefaultSectionSize( 15 );
 layout->addWidget( my_table_widget );
 my_dialog->setLayout( layout );
 my_dialog->resize( 500, 200 );
 my_dialog->show();

 return app.exec();
}

编辑:我不知道如何在这里格式化代码块......请原谅我。 :)

编辑 2:我修复了这个问题,以及以下简单的 tighterTable.pro 文件
一起帮忙。

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

SOURCES += tighterTable.cpp    # if that is the filename

感谢一大群胖子为此。顺便说一句:编辑代码只是缩进四个空格,和/或点击格式行中带有小“101010”的按钮。

The code getting 'h' might be unsound. It was just an example. Copy & paste the following rather rudimentary code. Change the value in "setDefaultSectionSize()", recompile, and run. You should see the difference. Setting this to 10 or 50 yields visible results. In the code above, it is possible QFontMetrics or QFont is messing something up.

You can use whatever you want to get the height, but font size makes the most sense.

#include <QtGui>

int main( int argc, char* argv[] )
{
 QApplication app( argc, argv );

 QDialog* my_dialog  = new QDialog();
 QHBoxLayout* layout  = new QHBoxLayout();
 QTableWidget* my_table_widget = new QTableWidget( my_dialog );

 my_table_widget->setRowCount( 10 );
 my_table_widget->setColumnCount( 10 );
 my_table_widget->verticalHeader()->setDefaultSectionSize( 15 );
 layout->addWidget( my_table_widget );
 my_dialog->setLayout( layout );
 my_dialog->resize( 500, 200 );
 my_dialog->show();

 return app.exec();
}

EDIT: I don't know how to format a block of code here... forgive me. :)

Edit 2: I fixed that, and the following simple tighterTable.pro file
helps along.

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

SOURCES += tighterTable.cpp    # if that is the filename

Thanks a big fat bunch for this. BTW: Editing as code is just indenting by four spaces, and/or hitting the button with the little '101010' in the formatting row.

铁憨憨 2024-08-27 01:38:09

恕我直言,您正在寻找的解决方案有一个非常愚蠢的解决方案,但它有效。您需要设置标头的 defaultSectionSize() 成员。通过verticalHeader() & 访问水平标题()。我从来没有真正设置过列宽,因为我的大多数项目都涉及我添加行,而不是列,我只是调用 resizeColumnsToContents 或手动调整大小。然而,这些行是令人厌烦的。我通常使用 QFontMetrics 获取字体的高度,并添加 2。添加的任何后续行都应具有此高度,并且中提琴:外观更紧凑。

希望有帮助。

编辑:

未经测试的代码:

QFontMetrics fm( my_font );
int h = fm.height() + 2;
my_table->verticalHeader()->setDefaultSectionSize( h );

What you're looking for has a very silly solution IMHO, but it works. You need to set the headers' defaultSectionSize() members. Accessed via verticalHeader() & horizontalHeader(). I never really set the column width's w/ this b/c most of my projects involve me adding rows, not columns, and I just call resizeColumnsToContents or do a manual resizing. However, the rows is irksome. I generally get the height of the font using QFontMetrics, and add 2. Any subsequent row added should have this height, and viola: tighter look.

Hope that helps.

EDIT:

Untested code:

QFontMetrics fm( my_font );
int h = fm.height() + 2;
my_table->verticalHeader()->setDefaultSectionSize( h );
琉璃梦幻 2024-08-27 01:38:09

QTableWidget 是一个方便的模型和视图。通常,QAbstractItemModel 的 data() 方法提供 SizeHintRole 用于告诉视图每个单元格的大小应该是多少。

由于您使用的是 QTableWidget,我认为您无法执行任何操作来更改其内部模型返回的大小提示。甚至 Qt 样式表文档也没有提及该领域。

QTableWidget is a convenience model and view. Typically, QAbstractItemModel's data() method provides a SizeHintRole that is used to tell the view what size each cell should be.

Since you're using QTableWidget, I don't think there's anything that you can do to change the size hint being returned by its internal model. Even the Qt style sheet documentation mentions nothing in that area.

疯狂的代价 2024-08-27 01:38:09

QTableWidget->垂直标题-> setMinimumSectionSize() 是正确的方法,可以在 ui 中设置。

QTableWidget -> verticalHeader -> setMinimumSectionSize() is the right way, and it can be set in ui.

冬天旳寂寞 2024-08-27 01:38:09

我尝试了这里所有的答案但没有成功。不过对我有用的是设置以下两者

    table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents)
    table->verticalHeader()->setMaximumSectionSize(10)

I tried all the answers here without success. What worked for me though was setting both the following.

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