B
B
becks2015-01-30 18:56:51
Qt
becks, 2015-01-30 18:56:51

How to make QPushButton a custom form in Qt?

Any shape, for example, a triangle or an arrow.
I redefined paintEvent, but for some reason other properties are lost (highlighting when hovering the mouse, deepening when clicking, etc.), it looks like the figure is drawn on the canvas.
I did setMask, but it turns out a strongly "non-aliased" button outline.
QML cannot be used. It is desirable to do without styles.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Danilevsky, 2015-01-31
@danilevskiy

There are a couple of options, but it all depends on the task.
1) Use ready-made images for each state and describe in styles. For example,

/*----------------------------------------------------------*/
QPushButton#buttonMoveUp{
background-image: url(:/style/images/upfocusnormal.png);
background-position: center;
border: none;
width: 30px;
height: 30px;
}

QPushButton#buttonMoveUp::hover {
background-image:url(:/style/images/upfocuspressed.png);
}

QPushButton#buttonMoveUp::pressed {
background-image:url(:/style/images/upfocuspressed.png);
}

QPushButton#buttonMoveUp::!enabled {
background-image:url(:/style/images/upnormal.png);
}

/*----------------------------------------------------------*/

2) Either redefine paintEvent and draw whatever you want. The button's state can be obtained via QStyleOptionButton.
//---------------------------------------------------------------
void Button::paintEvent( QPaintEvent *ev )
{

    QPainter painter(this);

    QStyleOptionButton option;
    initStyleOption(&option);
.....
}

//---------------------------------------------------------------
const QString Button::getIconSource( QStyleOption& option ) const
{
    //draw icon
    QString iconSource = getIconNormal();

    //hover
    if( option.state & QStyle::State_MouseOver)
    {
        iconSource = getIconHover();
    }

    //pressed
    if( option.state & QStyle::State_Sunken )
    {
        iconSource = getIconPressed();
    }

    //checked
    if( option.state & QStyle::State_On )
    {
        iconSource = getIconChecked();
    }

    //disabled
    if( !(option.state & QStyle::State_Enabled) )
    {
        if( !getIconDisabled().isEmpty() )
        {
            iconSource = getIconDisabled();
        }
    }

    return iconSource;
}

Here is a link to a custom button: https://yadi.sk/d/fWC_VeDyeMu4o

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question