Important: Please read the Qt Code of Conduct -

QLabel Opacity Register Event

  • Hello Folks
    Maybe you can help.
    I have a QLabel with QPixmap (code below) so that when you click it switches between on and off icons.
    This works perfect.
    However, the only area of the QLabel that registers a touch or mouse-click is the non-transparent areas of the QPixmap which is a PNG image.
    It becomes difficult to press as you have to hit the non-opaque sections of the PNG.
    The issue is this line,if i remove it the click registers on the entire 200X200 pixel QLabel but then I lose the transparency effect

    icon->setAttribute(Qt::WA_TranslucentBackground, true);

    Any clues as to how i make the entire 200x200 Qlabel clickable even the transparent bits?

    mainArea::mainArea(QWidget *parent) :QLabel(parent)
    icon->setGeometry(QRect(x, y, w, h));
    icon->setAttribute(Qt::WA_TranslucentBackground, true);
    icon->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    QObject::connect(icon, SIGNAL(clicked()), this, SLOT(btnClicked()));

  • @nanamo
    There may be a better answer, but:

    Why choose a QLabel for clickability? This can be done via a QPushButton, which more naturally supports clicking. Does help?

    Alternatively I come across non-clickable widgets clickable (Qt4, but hopefully still applicable) which uses eventFilter() and detecting mouse release in object rectangle.

  • @JonB Thanks Jon
    QPushButton not a goer as is would be too much of an upheaval at this stage considering the work done to this point.
    I will look at your eventfilter suggestion.

    I thought the answer would be more straightforward than this but Ive been looking for a few days now..head wrecked.

Log in to reply