在QTableWidget的列标题下添加边框 [英] Add border under column headers in QTableWidget

查看:1246
本文介绍了在QTableWidget的列标题下添加边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个对话框中有一个带有两个列标题的表小部件,如下所示:

I have a table widget with two column header in a dialog that looks like this:

在名为"Index"的列标题之间存在分隔.和标签",但在这些标题和它们下面的行之间没有分隔边界.如何添加呢?

There is a separation between the column headers named "Index" and "Label", but there is no separating border between these header and the row below them. How can this be added?

说表实例化为:

table = QTableWidget(6, 2, self)

我知道我可以通过headerItem = table.horizontalHeaderItem(0)修改一些属性并将其设置回去,从而将第一个水平标题作为QTableWidgetItem获得,但是我不确定要设置哪些属性,或者不确定是否有更简单的方法.

I know that I can get the first horizontal header as a QTableWidgetItem by doing headerItem = table.horizontalHeaderItem(0) modify some properties and set it back, but I'm unsure of what properties to set or if there is a more straightforward approach.

如果我提取标题并通过以下方式设置其基础框架的样式,阴影和线宽属性:

If I extract the header and set the style, shadow, and line width properties of its underlying frame via:

header = table.horizontalHeader()
header.setFrameStyle(QFrame.Box | QFrame.Plain)
header.setLineWidth(1)
table.setHorizontalHeader(header)

我最终得到的是这样的东西:

I end up with something that looks like this:

这样,边框确实会在标题周围显示.可以分别为每个标题项目完成此操作,但是有两个问题:

This way a border does show up around the header. This could be done for each header item separately, but there are a couple of concerns:

  1. 边框超出了下面单元格的宽度
  2. 我仍然必须将框架的颜色与现有线条进行匹配,以使其看起来还不错.
  3. 看来分隔索引"的垂直线是垂直的.和标签"相对于分隔下面单元格的线发生了偏移(除非我的眼睛在耍弄我).

还有更多的内置"方式吗?

Is there a more 'built in' way to do this?

推荐答案

我遇到了同样的问题.首先要注意的是,这仅发生在Windows 10上.在其他OS上,无需修复,它可以正常工作.但是,在Windows 10上,绘制原语不会绘制底部边框(这是默认的Windows 10表标题样式-在Windows文件资源管理器中可以看到).不幸的是,可以使用以下样式表解决此问题:

I had the same problem. The first thing to note is that this only happens on Windows 10. On other OS, there is nothing to repair, it just works. On Windows 10 however, the painting primitives are not painting the bottom border (which is the default Windows 10 table header style - as can be seen in windows file explorer). This is unfortunate but can be solved with the following style sheet:

if(QSysInfo::windowsVersion()==QSysInfo::WV_WINDOWS10){
    setStyleSheet(
        "QHeaderView::section{"
            "border-top:0px solid #D8D8D8;"
            "border-left:0px solid #D8D8D8;"
            "border-right:1px solid #D8D8D8;"
            "border-bottom: 1px solid #D8D8D8;"
            "background-color:white;"
            "padding:4px;"
        "}"
        "QTableCornerButton::section{"
            "border-top:0px solid #D8D8D8;"
            "border-left:0px solid #D8D8D8;"
            "border-right:1px solid #D8D8D8;"
            "border-bottom: 1px solid #D8D8D8;"
            "background-color:white;"
        "}");}

很抱歉打扰您使用C ++代码,但是将其转换为有效的pyqt代码应该足够容易.

Sorry to bother you with C++ code but It should be easy enough to translate into valid pyqt code.

注意1:不幸的是背景颜色和填充,但是它们对于使我们的自定义渲染看起来像默认渲染是必不可少的.

Note1: The background-color and padding are unfortunate but they are necessary to make our custom rendering looks like the default one.

注2:边框的颜色是我系统上网格的颜色.我没有使用默认标题的颜色.

Note2: The color of the border is the color of the grid on my system. I did not use the color of the default header.

注意3:QTableCornerButton::section部分对于在左上角下方添加缺失的边框是必需的.如果垂直标题不可见,则缺少的行也将不可见.

Note3: The QTableCornerButton::section part is necessary to add the missing border below the top left corner. If the vertical header is not visible, the missing line is invisible too.

希望这会有所帮助.

这篇关于在QTableWidget的列标题下添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆