-1

Inspired by Label on QToolBar, possible? I'm trying to develop it. But my button do not blink on click, why?

I see in the other app looks like the button color gets gray during the click (while pressed) and the normal button gets blue. But my button which is just all an image don't have any behavior during the click.

enter image description here

On the left side is the toolbar using QAction (question linked above) and on the right side is the code below.

QApplication a(argc, argv);
QMainWindow w;

QToolBar barA;
barA.setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

QWidget widget;
QVBoxLayout vLayout(&widget);

QHBoxLayout hLayout;
vLayout.addLayout(&hLayout);

QPixmap pixmap("../../../on.png");
QIcon ButtonIcon(pixmap);

QPushButton bt;
bt.setIcon(ButtonIcon);
bt.setIconSize(QSize(32,32));
bt.setFixedSize(QSize(32,32));
bt.show();
hLayout.addWidget(&bt);

barA.addWidget(&widget);

QPushButton bt2("clickc");
barA.addWidget(&bt2);

w.addToolBar(&barA);

w.show();
return a.exec();
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • Where have implemented the "off" behavior for the click? I don't see it in your code, but it's clearly there in the example that you've linked. – PlinyTheElder Sep 19 '19 at 21:09
  • There is not `Off`on the right side app which refers to the code shown in this question. The app on the left side refers to the other question. I try to compare the buttom behavior during click here. – KcFnMi Sep 19 '19 at 22:12
  • Does this answer your question? [QGraphicsEffect on pyqt, blinking a button](https://stackoverflow.com/questions/70896744/qgraphicseffect-on-pyqt-blinking-a-button) – mahkitah Apr 17 '23 at 08:54

4 Answers4

0

do something like:

auto b = new QPushButton(this);
b->setText("hello");
connect(b, &QPushButton::clicked, [](){qDebug()<< "ok...";});
ui->statusBar->addWidget(b);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I understand this should show the button is receiving the clicks. Though I'm wondering why it is not blinking, the visual feedback is missing I would perhaps say. – KcFnMi Sep 19 '19 at 14:54
  • ummm... is the click signal getting triggered right?? – ΦXocę 웃 Пepeúpa ツ Sep 19 '19 at 14:57
  • 1
    @KcFnMi you didn't implemented it. Original question did, assigning images for both states through QAction. It's possible to do though QSS instead, by assigning images for two different state selectors in style sheet – Swift - Friday Pie Sep 19 '19 at 22:34
0

Still a shy blink though, not as good as the one from left app.

enter image description here

button.h

#include <QPushButton>

class Button : public QPushButton {
    Q_OBJECT
    QPixmap m_pixmap;
public:
    explicit Button(QString img, QWidget *parent = nullptr);
public slots:
    void enable();
    void disable();
};

#endif // BUTTON_H

button.cpp

#include "button.h"

Button::Button(QString img, QWidget *parent) : QPushButton(parent) {
    setIcon(QIcon(img));
    setIconSize(QSize(32,32));
    setFixedSize(QSize(32,32));

    connect(this, &QPushButton::pressed, this, &Button::disable);
    connect(this, &QPushButton::released, this, &Button::enable);
}

void Button::enable(){
    setIcon(m_pixmap);
}

void Button::disable(){
    m_pixmap = icon().pixmap(32,32,QIcon::Normal);
    QPixmap pixmap = icon().pixmap(32,32,QIcon::Selected);
    setIcon(pixmap);
}
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • Does your `QIcon` actually have a `Selected` mode file added? Because I don't see where you do that (with `QIcon::addFile()`). So swapping the icon shouldn't be having any effect at all. I think you're just seeing default Mac style behavior on the button. – Maxim Paperno Sep 24 '19 at 04:03
0

You should check out the docs: https://doc.qt.io/qt-5/stylesheet-examples.html

You probably just need to set the style sheet on it.

QToolButton:pressed { }

I am making an assumption here, but the tool bar probably already has a style sheet that handles the color change you are observing.

Mason
  • 71
  • 4
  • The given reason to stop using connections do no stands here (and it isn't enabling /disabling the widget, just tweaking with the icon). Anyway I'm curios how to achieve similar result without connections? – KcFnMi Sep 21 '19 at 02:29
  • My bad on the connection, I read it too fast. Again, I am pretty sure what you want to achieve can be done through the style sheet :) – Mason Sep 21 '19 at 02:50
0

The toolbar on the left which is using QToolButton to show the QActions will have special QMacStyle styling applied to the down/pushed state. https://code.woboq.org/qt5/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm.html#3538 (more specifically here where it calls darkenPixmap())

Simplest solution seems like using a QToolButton or just the QAction route (actions are way more flexible than buttons anyway, you can use them multiple times in different UI elements).

Otherwise the code you posted in "Still a shy blink though" answer should work if you specifically add a file to the QIcon for the Selected mode. Or just use two icons and swap between them (seems easier).

Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22