无法更改button的颜色和buttontext的颜色,使用qstyle自己画pushbutton,在风格选项中设置的palette

lovemyliwu 2011-12-15 05:51:53
我要自己画一个button。

使用的qstyle的drawControl函数

button是画出来了的,可是我在风格选项中设置的调色板没用,无法更改button的颜色和buttontext的颜色。

请大家帮帮我。

下面是paintevent()函数中画button的代码


void CommentWidget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.save();
painter.setRenderHint(QPainter::Antialiasing);//开启边缘反锯齿
QSize size=this->size();
qDebug()<<size;

QPalette palette=QPalette(Qt::white);

//画刷的活动颜色组
palette.setBrush(QPalette::Window,Qt::lightGray);//窗口背景颜色
palette.setBrush(QPalette::WindowText,Qt::black);//窗口前景颜色

palette.setBrush(QPalette::Base,Qt::white);
palette.setBrush(QPalette::AlternateBase,Qt::lightGray);

palette.setBrush(QPalette::Text,Qt::black);
palette.setBrush(QPalette::Button,Qt::white);//按钮颜色
palette.setBrush(QPalette::ButtonText,Qt::red);//按钮文字颜色

//画底部按钮
QStyleOptionButton opt;
opt.initFrom(this);
opt.text = "Reply";
opt.rect=QRect(size.width()-180,size.height()-70+10,80,50);
opt.state=QStyle::State_Enabled;
opt.fontMetrics=QApplication::fontMetrics();
opt.palette=palette;

this->style()->drawControl(QStyle::CE_PushButton,&opt,&painter,this);

painter.restore();

}



...全文
450 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
中才德创 2011-12-17
  • 打赏
  • 举报
回复
从未DIY过按钮。CommentWidget父类是QWidget吧?
念茜 2011-12-17
  • 打赏
  • 举报
回复
同时可以方便的利用Qss来实现你想实现的 更改颜色、和text颜色
念茜 2011-12-17
  • 打赏
  • 举报
回复
子类化QPushButton来实现自定义比较简单:http://blog.csdn.net/yiyaaixuexi/article/details/6706729
三条猫 2011-12-17
  • 打赏
  • 举报
回复
既然都自绘按钮了,自己绘边框,背景,字体就行了吧,想怎么画就怎么画,何必拘泥于style()->drawControl这个函数??

16,240

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧