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:
Add
QLabel
to the formRight-click on added label and select
Promote to...
Enter your clickable label class name and its header file name
Press add, than select your label in the tree and select
promote
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:
- Use any text editor;
- 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.
- Go to designer;
- Choose your label;
- 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 dosetText()
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:
- Disable the "
openExternalLinks
" property of theQLabel
- 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
Finding Index of Nearest Point in Numpy Arrays of X and Y Coordinates
Python's in (_Contains_) Operator Returns a Bool Whose Value Is Neither True Nor False
Python Re.Sub Back Reference Not Back Referencing
How to Tell a Python Script to Use a Particular Version
How to Get Stable Results with Tensorflow, Setting Random Seed
Validation of a Password - Python
Downloading a Directory Tree with Ftplib
How to Access a Standard-Library Module in Python When There Is a Local Module with the Same Name
How to Set Headers Using Python's Urllib
If Two Variables Point to the Same Object, Why Doesn't Reassigning One Variable Affect the Other
How to Redirect Stderr in Python
Difference Between Pygame.Display.Update and Pygame.Display.Flip
Are Sets Ordered Like Dicts in Python3.6
Python Method Name with Double-Underscore Is Overridden
Why Do I Get "Pickle - Eoferror: Ran Out of Input" Reading an Empty File