Witam!
Czy jest możliwość aby po kliknięciu w label wykonała się jakaś akcja ? (chodzi mi o to jak zrobić właśnie sygnał kliknięcia w label)
Witam!
Czy jest możliwość aby po kliknięciu w label wykonała się jakaś akcja ? (chodzi mi o to jak zrobić właśnie sygnał kliknięcia w label)
W samym QLabel
nie ma takiej możliwości, ale kto Ci broni stworzyć klasę dziedziczącą po QLabel i reimplementować zdarzenie kliknięcia?
A “clicked” signal may sometimes be required from a label, but there is no “clicked” signal emitted by QLabel.
To make it clickable : Catch the mouse press event on the label. Then emit ‘clicked’ signal.
Example :
void ClickableLabel::mousePressEvent ( QMouseEvent * event )
{
QLabel::mousePressEvent(event);
emit clicked();
}
To make sure other funcionality still works, you also need to call the base class implementation of the event.
Pytanie do czego ci właściwie potrzebny klikalny label.
WinAPI ma kontrolkę zwaną SysLink, wygląda to tak:
Qt też powinno mieć coś takiego, poszukaj.
Qt jest świetnie udokumentowane (chociaż trafiają się pewne niedociągnięcia).
Z jednej strony dokumentacja mówi
Qt doc napisał(a)
Detailed Description
The QLabel widget provides a text or image display.
QLabel is used for displaying text or an image. No user interaction functionality is provided.
A z drugiej strony masz:
Qt doc napisał(a)
void QLabel::linkActivated(const QString & link) [signal]
This signal is emitted when the user clicks a link. The URL referred to by the anchor is passed in link.
This function was introduced in Qt 4.2.
See also linkHovered().
Czyli zrób tak:
QLabel *label = new QLable(parent);
label->setTextFormat(Qt::RichText);
label->setText("test <url=google.pl>some link</url> now");
connect(label, SIGNAL(linkActivated(QString)), this, SLOT(linkClicked(QString)));