Styled top-level QPushButton widget does not render properly

本文介绍如何为Qt中的QPushButton设置自定义样式,包括边框、背景等,并解决样式应用后按钮渲染不正常的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I've successfully made a QPushButton the top-level widget/window of an application and am attempting to style the button like so:

#include <QPushButton>
#include <QApplication>

class MyButton : public QPushButton
{
public:
    MyButton() : QPushButton( "Button" )
    {
        setFixedSize( 250 , 65 );
        setStyleSheet( "border-radius: 10px;" ); // style
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyButton b;
    b.setWindowFlags( Qt::FramelessWindowHint | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint );
    b.setAttribute(Qt::WA_TranslucentBackground); // Fixes opaque BG
    b.show();

    return a.exec();
}

Unfortunately, as the following image shows, the button is no longer rendered properly when the style is applied. I'd appreciate help getting the button to render the style properly.

enter image description here

Edit

Following Kuber Obas answer, I'd appreciate help styling the edges of the widget, i.e. those that are outside the rounded corner, to transparent as shown below

enter image description here

share improve this question
 
 
+1 for self-contained code! Bravo! –  Kuba Ober  Dec 17 '13 at 21:02
 
You need to show your code, otherwise I won't know how to help you. I've edited the code below to show that the background is handled properly around the edges. –  Kuba Ober  Dec 18 '13 at 14:36
1 
Problem solved. All I had to do was to set the attribute Qt::WA_TranslucentBackground) on the button. (Code shown above.) –  Olumide  Dec 18 '13 at 15:23

1 Answer

up vote 4 down vote accepted

In most native styles, the border is drawn with the rest of the button using native functionality and its elements cannot be replaced one-by-one. Once you introduce your own styling, the monolithic native styling is gone. So, you'll need to replace all of the functionality provided by the native style, including the border, the background gradient, etc. You will need to tweak it to "match" native style if you so need.

Here, you need to redefine the border completely: at the minimum provide the pen (border:). The radius only makes sense with the pen. You also need to redefine the background, if you care for one, redefine all of the button's state selectors, etc. You start with an unstyled button!

The screenshot below demonstrates it well. On the left you have a Mac-styled native button, on the right you have a button with just its border defined anew. It's obvious that the default state background should also be adjusted to match that of the platform in this case, and some margin needs to be added.

Qt doesn't really re-do modern native styles entirely from scratch, that's why you can't tweak their individual elements. It'd be too much work and a constantly moving target. It used to be done for the old Windows-95/NT style. Starting with the XP style, it was decided to let the platform APIs provide the visual style bitmaps. Similar thing presumably happens on OS X. That's also the reason why you can't use the fancier XP/Aqua/Mac Qt styles outside of their native platform: the relevant native APIs are not present and thus the style is disabled.

screenshot

// https://github.com/KubaO/stackoverflown/tree/master/questions/styledbutton-20642553
#include <QPushButton>
#include <QHBoxLayout>
#include <QApplication>

int main(int argc, char *argv[])
{
   QApplication a{argc, argv};
   QWidget w;
   QHBoxLayout layout{&w};
   QPushButton button1{"Default"};
   button1.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
   layout.addWidget(&button1);
   QPushButton button2{"Styled"};
   button2.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
   button2.setStyleSheet(
      "* { border: 2px solid #8f8f91; border-radius: 12px; background-color: #d02020; }"
      "*:pressed { background-color: #f6f7fa; }");
   layout.addWidget(&button2);
   auto pal = w.palette();
   pal.setBrush(QPalette::Background, Qt::darkBlue);
   w.setPalette(pal);
   w.show();
   return a.exec();
}

转载:http://stackoverflow.com/questions/20642553/styled-top-level-qpushbutton-widget-does-not-render-properly
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值