"<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;""> 最近做项目用到了QTableWidget,基于QWidget做的,QTableWidget的 <span style=""border-bottom:#fa891b 1px solid;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;cursor:pointer;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">背景色 <span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">,背景 <span style=""border-bottom:#fa891b 1px solid;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;cursor:pointer;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">图片 <span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">,文字 <span style=""border-bottom:#fa891b 1px solid;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;cursor:pointer;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">颜色 <span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">都可以改变,表头的 <span style=""border-bottom:#fa891b 1px solid;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;cursor:pointer;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">字体 <span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">颜色也可以改变,但是表头的颜色不能改变,在论坛看了很多高手的回答,无论是获取表头对象设置还是使用样式表设置都没效果。本来以为是不是代码无效,就做了三个demo来验证,分别是基于QMainWindow,QDialog,QWidget.只有QMainWindow中的代码实现了效果, <span style=""border-bottom:#fa891b 1px solid;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;cursor:pointer;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">其他 <span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">两个都无效,求解啊? <img src=""http://www.qtcn.org/bbs/images/post/smile/wangwang/20.gif"" />
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">QTableWidgetItem *columnHeaderItem0 = ui->tableWidget->horizontalHeaderItem(i); //获得水平方向表头的Item对象
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">columnHeaderItem0->setFont(QFont("Helvetica")); //设置字体
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">columnHeaderItem0->setBackgroundColor(QColor(13, 13, 13)); //设置 <span style=""border-bottom:#fa891b 1px solid;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;cursor:pointer;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">单元格 <span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">背景颜色
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">columnHeaderItem0->setTextColor(QColor(233, 233, 233)); //设置文字颜色
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:14px/21px Arial;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"">————————————————————————————————————————————————
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;""><span style=""outline-style:none;color:#333333;""><span style=""line-height:1.5em;outline-style:none;font-family:'Arial ';"">ui->tableWidget->horizontalHeader()->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color: rgb(170, 170, 255);font: 20pt '华文隶书';");
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;""><span style=""outline-style:none;color:#333333;""><span style=""line-height:1.5em;outline-style:none;font-family:'Arial ';"">
<span style=""widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;outline-style:none;font:14px/21px Arial;white-space:normal;orphans:2;letter-spacing:normal;color:#333333;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;""><span style=""outline-style:none;color:#333333;""><span style=""line-height:1.5em;outline-style:none;font-family:'Arial ';""><span style=""outline-style:none;font-size:12px;""><span style=""outline-style:none;display:inline-block;""><img src=""http://www.qtcn.org/bbs/attachment/Mon_1207/17_133462_35dd893c7f681d8.jpg?14"" /><span style=""outline-style:none;font-size:12px;""><span style=""outline-style:none;display:inline-block;""><img src=""http://www.qtcn.org/bbs/attachment/Mon_1207/17_133462_6d9a8d9eadbfa3b.jpg?13"" />"
"<pre class=""brush:cpp; toolbar: true; auto-links: false;"">ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{border:2px groove gray;border-radius:10px;padding:2px 4px;background-color: rgb(170, 170, 255);font: 20pt '华文隶书'}");
######自己结贴算了,在OSC问QT的问题还真是没人回答啊"版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。