Qt开发之路——解决背景颜色与图标背景颜色冲突问题

为什么按钮的特效失效了???
经过1个小时之久的研究终于发现了问题!!!

即背景颜色background-image与图标的特效背景颜色冲突了。

样式表设置:
*{
background-image:url(":/skin/21");
}
QPushButton#addid{
border-style:outset;
font: 75 18pt “Times New Roman”;
font-weight:bold;
color:rgb(255, 170, 0);
background-color:rgba(225, 225, 225, 0);
}
}

QPushButton#addid:hover{
background-color:rgba(225, 225, 225,200);
}
}

QPushButton#addid:pressed{
background-color:rgba(225, 225, 225, 200);
}
}

所以我把
*{
background-image:url(":/skin/21");
}
删掉了

取而代之的使用QPainter设置背景的方式

void Widget::paintEvent(QPaintEvent *){
QPainter p;
p.begin(this);
p.drawPixmap(rect(),QPixmap(":/skin/21"));
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt中,设置QTableView的表头字体颜色背景颜色可以通过继承自`QStyledItemDelegate`并重写其相关方法来实现。这里是一个简单的示例: 首先,你需要创建一个自定义的样式委托(CustomDelegate),例如`MyDelegate`: ```cpp class MyDelegate : public QStyledItemDelegate { public: explicit MyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {} // 重写 paintSection() 方法设置背景颜色 void paintSection(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { if (index.column() == 0) { // 假设你想要改变第一列的样式 painter->fillRect(option.rect, QColor(Qt::lightGray)); // 设置背景色 } // 重写 paintText() 方法设置文本颜色 QStyledItemDelegate::paintText(painter, option, index); } // 可选:如果想设置特定文字的颜色,可以在paintText()中进行修改 void paintText(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int column, int row) const override { if (column == 0) painter->setPen(Qt::red); // 设置字体颜色为红色 QStyledItemDelegate::paintText(painter, option, text, column, row); } }; ``` 然后,在你的`QMainWindow`或`QWidget`的实例中使用这个自定义委托: ```cpp QTableView *tableView = new QTableView(this); MyDelegate *delegate = new MyDelegate(this); tableView->setItemDelegate(delegate); // 如果你想改变所有表头单元格的样式 tableView->horizontalHeader()->setDefaultSectionSize(100); // 设置宽度 tableView->horizontalHeader()->setFont(QFont("Arial", 12)); // 设置字体 tableView->horizontalHeader()->setStyleSheet("background-color: lightgray; color: red;"); // 直接设置样式 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂java杰尼龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值