How to override only one property: a pair of values ​​in Qt StyleSheet

I am writing a newbie Qt5 on OSX Mavericks and would like to redefine only one pair of properties: the StyleSheet value for this widget.

If I run the following standalone demo code:

#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QPushButton>

int
main( int argc, char *argv[] ) {
    QApplication app( argc, argv );

    QMainWindow* mw = new QMainWindow();

    QPushButton* AButton = new QPushButton( "A Button", mw );

    mw->show();

    return app.exec();
}

I get a nice button with default settings for Macintosh - rounded corners, standard OSX blue when pressed, etc .:

QPushButton with OSX defaults

If I set the stylesheet so that the background button is red, it seems that I am losing all other OSX-style defaults in this process - more rounded corners, standard margins, etc.:

#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QPushButton>

int
main( int argc, char *argv[] ) {
    QApplication app( argc, argv );

    QMainWindow* mw = new QMainWindow();

    QPushButton* AButton = new QPushButton( "A Button", mw );

    AButton->setStyleSheet("QPushButton { background-color: red; }");

    mw->show();

    return app.exec();
}

Here is the result:

QPushButton style override, with loss of other OSX defaults

property: value, , . , , .. ?

+10
2

, QApplication:: setStyleSheet() . , QStyleSheetStyle. , QStyleSheetStyle , Mac. QStyleSheetStyle, , baseStyle()->drawControl().

, . , ? QStyleSheetStyle Windows, . , , . .

, , . , QStyleSheetStyle:: drawControl(). :

case CE_PushButton:
    if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
        if (rule.hasDrawable() || rule.hasBox() || rule.hasPosition() || rule.hasPalette() ||
                ((btn->features & QStyleOptionButton::HasMenu) && hasStyleRule(w, PseudoElement_PushButtonMenuIndicator))) {
            ParentStyle::drawControl(ce, opt, p, w);
            return;
        }
    }

ParentSyle - Windows. background-color rule.hasPalette() true, .

+4

Qt StyleSheet QMacStyle, .

Qt QStyle(). Qt- OSX ( ), Qt QStyle() QMacStyle() ( , -, QProxyStyle(), QMacStyle - ).

setStyleSheet(), , QStyleSheetStyle(), , QStyle - QMacStyle, . , . qtbase/src/widgets/kernel/qapplication.cpp QApplication::setStyleSheet(). QStyleSheetStyle() qtbase/src/widgets/styles/qstylesheetstyle.cpp. , QStyleSheetStyle() QWindowsStyle(), windows-ish .

, Qt StyleSheets QStyle(). setStyleSheet() , QStyleSheetStyle() QMacStyle().

- , , . , QStyle() . Mac, QMacStyle(). QStyle() . setStyleSheet(), QStyleSheetStyle(), QWindowsStyle(). stylesheet, , .

. , , QStyle(). , . - , Mac , setStyleSheet().

- - qtbase/src/widgets/styles/qmacstyle_mac.mm - , , - , , . , QStyleSheetStyle QMacStyle QWindowsStyle, - (, setStyleSheet (QStyle * baseStyle, QString styleSheet)). , , Qt, .

+2

All Articles