开发者社区> 问答> 正文

QTableWidget修改表头颜色的代码为什么只在QMainWindow中有效?-服务报错

"<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"" />"

展开
收起
montos 2020-05-31 15:27:47 470 0
1 条回答
写回答
取消 提交回答
  • "<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的问题还真是没人回答啊"
    2020-05-31 15:27:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载