C++ Qt / Label sygnał

0

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)

2

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.

http://qt-project.org/doc/qt-4.8/qlabel.html#notes

0

Pytanie do czego ci właściwie potrzebny klikalny label.

WinAPI ma kontrolkę zwaną SysLink, wygląda to tak:
Безымянный.png

Qt też powinno mieć coś takiego, poszukaj.

1

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)));

1 użytkowników online, w tym zalogowanych: 0, gości: 1