Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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->setObjectName(name);
    icon->setGeometry(QRect(x, y, w, h));
    icon->setOnPixmap(QPixmap(imgOnInd_dm));
    icon->setOffPixmap(QPixmap(imgOffInd_dm));
    icon->setAttribute(Qt::WA_TranslucentBackground, true);
    icon->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    icon->setVisible(vis);
    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 https://stackoverflow.com/questions/35442678/pyqt-button-click-area-non-rectangular-area help?

    Alternatively I come across https://wiki.python.org/moin/PyQt/Making 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