LittleSaints
LittleSaints

Reputation: 421

Qt Qpushbutton: How to set the same appearance when pressed or not pressed?

I'm using QTCreator in a GUI frame for a touch LCD application. I arranged some pushbuttons: for example one of them is "+" ("+" made with my own graphic symbol) to increase a level. When I pressed the button using my finger on LCD touch display, It appeared as pressed with a space around the button to show the button is pressed, but the visual effect is not good. I'd like to press the qpushbutton without this "reaction" so visible on the display. How can I set graphically the qpushbutton in order to avoid its "external crown" when pressed?

Upvotes: 0

Views: 66

Answers (1)

rpsml
rpsml

Reputation: 1498

I am not sure that you can do that. Once I had to emulate something similar (I needed a label with a background that changed color if the user clicked on it). So I created a new class inherited from QLabel to handle click events. You can use this to emulate your non-depleting button (a QLabel can show an image).

The essential of the class I wrote is below (I had to erase a bunch of other stuff it was doing so a bug may have slipped through):

#ifndef QCLICKLABEL_H
#define QCLICKLABEL_H
//----------------------------------------------------------------------------------------
#include <QLabel>
#include <QMouseEvent>
#include <QWheelEvent>
//----------------------------------------------------------------------------------------
class QClickLabel : public QLabel
{
  Q_OBJECT

  public:
    // Constructors -- Mimics the QLabel constructors
    QClickLabel(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags(),
                bool MouseTrack = false,      // Set to true to get mouse move events without clicks
                bool InLabelRelease = true) : // Set to false to emit signal when the mouse is released outside label area
      QLabel(parent, f), 
      InLabelReleaseOnly(InLabelRelease)
        { setMouseTracking(MouseTrack); }

    QClickLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags(),
                bool MouseTrack = false,      // Set to true to get mouse move events without clicks
                bool InLabelRelease = true) : // Set to false to emit signal when the mouse is released outside label area
      QLabel(text, parent, f), 
      InLabelReleaseOnly(InLabelRelease)
        { setMouseTracking(MouseTrack); }

  signals:
    void clicked();
    void middleClicked();
    void rightClicked();
    void mouseButtonUp(QMouseEvent *e);   // MouseButtonUp signal
    void mouseButtonDown(QMouseEvent *e); // MouseButtonDown signal
    void mouseMove(QMouseEvent *e);       // MouseMoved signal (see MouseTrack in constructor)

  protected:
    // Mouse release event -- only handles the simple left and right clicks
    void mousePressEvent(QMouseEvent *e)
    {
      QLabel::mousePressEvent(e);
      if (this->rect().contains(e->pos()))    // Click is within label bounds.
        emit mouseButtonDown(e);              // Sends signal.
    }

    // Mouse release event -- only handles the simple left and right clicks
    void mouseReleaseEvent(QMouseEvent *e)
    {
      QLabel::mouseReleaseEvent(e);
      if (InLabelReleaseOnly &&             // User wants mouse to be within label bounds upon release
          !this->rect().contains(e->pos())) // But it is not
        return;                             // So, gets out.

      if (e->button() == Qt::LeftButton)    // Left button...
        emit clicked();                     // ...triggers normal Click.
      if (e->button() == Qt::RightButton)   // Right button...
        emit rightClicked();                // ...triggers RightClick.
      if (e->button() == Qt::MiddleButton)  // Middle button...
        emit middleClicked();               // ...triggers MiddleClick

      emit mouseButtonUp(e);                // Send an addition button up signal.
    }

    void mouseMoveEvent(QMouseEvent *e)
    {
      QLabel::mouseMoveEvent(e);
      emit mouseMove(e);
    }

  private:
    bool InLabelReleaseOnly;                // Only sends mouse release signal if the mouse was on the label area
    QClickLabel(const QClickLabel &QCL);    // Private copy constructor
};
//----------------------------------------------------------------------------------------
#endif // QCLICKLABEL_H

Note that you can use this in code or in the form designer. For the latter drop a QLabel in the form, right-click it, chose "Promote to..." and chose this class.

Now you can add your "+" image to the label, shape it to the proper size, and use it as a button with no visual feedback.

I still would be interested to see if anyone comes up with a simple way to do a no-visual feedback on a true QPushButton.

Upvotes: 0

Related Questions