1
votes

I need to create a button which may contain two icons and two text label to display (on screenshots - every line such as "Email" or "Connection" is a standalone button).

enter image description here enter image description here

From what I understood - I can't override QPushButton or QAbstractButton and I have to implement my own custom widget in order to achieve such layout. So far I'm trying to make such buttons with Qt Designer and change style of the widget using QSS and dynamic properties but for some reason stylesheet is not changing when I press the button. My code:

namespace ui
{

ViewEntryPushButton::ViewEntryPushButton(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::ViewEntryPushButton)
{
    ui->setupUi(this);
}

ViewEntryPushButton::~ViewEntryPushButton()
{
    delete ui;
}

void ViewEntryPushButton::mousePressEvent(QMouseEvent* event)
{
    this->setProperty("pressed", true);
    this->style()->unpolish(this);
    this->style()->polish(this);
    this->update();
}

void ViewEntryPushButton::mouseReleaseEvent(QMouseEvent *event)
{
    this->setProperty("pressed", false);
    this->style()->unpolish(this);
    this->style()->polish(this);
    this->update();
}

}   // namespace ui

And my style sheet which I apply to the highest object in Qt Designer object view:

#ViewEntryPushButton {
        background-color: transparent;
        padding: 5px;
        margin-left: 5px;
}

#ViewEntryPushButton[pressed="true"]{
        background-color:rgb(51, 0, 255);
        border-width: 3px;
        border-color: rgba(255, 255, 255, 20);
        border-style: inset;
}

enter image description here

So my questions: 1. Is my approach right or it is possible to subclass QAbstractButton and implement it like this? 2. What am I doing wrong with style sheets in this case and why style of the widget is not changing when I press my mouse?

2

2 Answers

0
votes

Did you tried you change stylesheet using the setstylesheet function like this in the constructor. Well this is just another way of doing thing.

Button.setstylesheet("") 
0
votes

Ended up using custom widget with QStackedLayout and transparent button above all other layers, I think it is the best approach.

I created necessary labels on "low" layers of the layout and just added a button above them all with background:transparent; property in style sheets.

Also StackingMode should be set to StackAll in the layout.