Make Qlabel Clickable

Make Qlabel clickable or double clickable in Qt

Create a new class derived from QLabel, reimplement mousePressEvent to emit custom pressed() signal (or any other functionality you need)

If you need to use your clickable label in ui files, follow these steps:

  1. Add QLabel to the form

  2. Right-click on added label and select Promote to...

  3. Enter your clickable label class name and its header file name

  4. Press add, than select your label in the tree and select promote

Sample Image

Sample Image

Now you can use your subclassed label (this tutorial actually works for any subclassed widget) as any QWidget using ui->

Clickable Qlabel How to use inside Designer

Inside Qt Desiger, you have to create a QLabel and then "promote" it to the ClickableLabel class.

To do so, right-click the QLAbel object instantiated in your widget, select "Promote widget..." and specify ClickableLabel class name and path to the header file defining it.

Note that, both header and source files for ClickableLabel must be part of your Qt project.

This should simply work!

Qt C++ QLabel Clickable mouse events doesn't work

Your label in ui is QLabel class, but you have created lblMouse. So, in ui you must change code

<widget class="QLabel" name="lblMouse">

to

<widget class="lblMouse" name="lblMouse">

EDIT:

To change this you can:

  1. Use any text editor;
  2. Go to Designer in Qt Creator chosing your ui, select your QLabel, call context menu and click "Promote to...". Then check that "Base class name" is correct (QLabel), write your class (lblMouse) in "Promoted class name" field, click "Add" button, then "Promote" button. That's all. Your label now your own label class.

About setText() method.

  1. Go to designer;
  2. Choose your label;
  3. In right side in object propeties find QLabel area and click on a round arrow front of "text" property. That's all. Now, if you do setText() method in constructor - it will works.

Clickable Qlabel: Binding multiple labels to a single function

Overriding functions in that way is not suggested, especially for what are considered protected functions in Qt, which is the case of any *Event() function of QObjects and QWidgets.

Also, in your case you're just overwriting the method with the same signature, which would never allow you to get the source of the function call.

A possible solution would be to use a lambda with the source as a keyword argument:

self.label_ligne_1_1.mousePressEvent = lambda ev, label=self.label_ligne_1_1: self.label_click(label)

But I wouldn't suggest you to do that.
A better approach, instead, would be to install an event filter on each label, and then set the pixmap each time a mouse press event is captured:

class Squares(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QGridLayout(self)
layout.setSpacing(0)

for row in range(4):
for col in range(4):
square = QtWidgets.QLabel()
square.setPixmap(QtGui.QPixmap('tab.png'))
layout.addWidget(square, row, col)
setattr(self, 'label_ligne_{}_{}'.format(row + 1, col + 1), square)
square.installEventFilter(self)

def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
source.setPixmap(QtGui.QPixmap('tabx.png'))
return super().eventFilter(source, event)

Insert clickable link in QLabel and detect click on this link to provoke an action

Ok, for those interested, I got the answer:

  1. Disable the "openExternalLinks" property of the QLabel
  2. Connect the signal linkActivated of the QLabel to your handler.

That's all: linkActivated gives you the URL that the link refers to in argument, so my pseudo code works perfectly.

// header
private slots:
void on_description_linkActivated(const QString &link);

// cpp
void KernelBuild::on_description_linkActivated(const QString &link)
{
if( link == "#browse_output" ){
on_outfilebtn_clicked();
}
}

pyqt: add clicked event for a Qlabel

You can do this using setOpenExternalLinks

self.labelOnlineHellp.setOpenExternalLinks(True)

If you want to do something different than the default behavior (ie. open link in the default browser), you can connect to the linkActivated signal instead (don't use setOpenExternalLinks to True if you're handling the opening of the link yourself).

self.labelOnlineHelp.linkActivated.connect(self.link_handler)

def link_handler(self, link):
subprocess.call(['/path/to/firefox', link])


Related Topics



Leave a reply



Submit