PDA

View Full Version : linkActivated



dragon
11th October 2007, 15:59
Hello anyone,

I have Qt-4.2.2 installed.

I have in a MainWindow a Label called searchLabel.
In Qt-4.2 there is a new signal LinkActivated();
I want to click on the searchLabel to start a another application for example a Dialog.

In my MainWindow i have the connection:



connect(searchLabel, SIGNAL(linkActivated(const QString &)), this, SLOT(search()));

void MainWindow::search()
{
searchDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
}
}


Normally in the docs the signal is:
connect(label_2, SIGNAL(linkActivated(const QString & )), this, SLOT(myslot(const QString &)));

In my Qt-Designer the flag Qt::LinksAccessibleByMouse is set.

Nothing happens, no errors in my console.

Can i use the signal LinkActivated() only for a URL adress?.

Thanks in advance.

jpn
11th October 2007, 16:13
I don't see anything wrong with provided code snippet... Are you sure the connect-statement gets executed and searchLabel exists by that time? Do you see a warning if you intentionally make it fail?

Byngl
11th October 2007, 16:19
Does the label have a url embedded, or do you just want a signal when the user clicks on the QLabel? If it is the second, then I've subclassed QLabel and added a mouseReleaseEvent



class ClickLabel : public QLabel
{
Q_OBJECT

public:
ClickLabel(QWidget *parent = 0, Qt::WindowFlags f = 0);
ClickLabel(const QString &text, QWidget *parent = 0, Qt::WindowFlags f = 0);
virtual ~ClickLabel();

Q_SIGNALS:
void labelClicked(ClickLabel *);

protected:
virtual void mouseReleaseEvent(QMouseEvent * event);
};



ClickLabel::ClickLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
}


ClickLabel::ClickLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
: QLabel(text, parent, f)
{
}


ClickLabel::~ClickLabel()
{
}


void ClickLabel::mouseReleaseEvent(QMouseEvent * event)
{
if (event->button() == Qt::LeftButton)
{
emit labelClicked(this);
}
}

Just change searchLabel to a ClickLabel...

dragon
11th October 2007, 18:34
A signal when the user clicks on the QLabel
I've subclassed QLabel and added a mouseReleaseEvent and change searchLabel to ClickLabel.

Nothing happens.
In my console no errors.
I'am doing this on Windows XP with QT-4.2.2 opensource.

What iám doing wrong.

jpn
11th October 2007, 18:42
Really hard to say without seeing the code...

dragon
11th October 2007, 19:04
Header file:



#include "ui_labeldialog.h"


class labelDialog : public QDialog, Ui::labelDialog
{
Q_OBJECT

public:
labelDialog(QWidget *parent = 0);

public slots:
void search();
};


class ClickLabel : public QLabel

{
Q_OBJECT

public:
ClickLabel(QWidget *parent = 0, Qt::WindowFlags f = 0);
ClickLabel(const QString &text, QWidget *parent = 0, Qt::WindowFlags f = 0);
virtual ~ClickLabel();

Q_SIGNALS:
void labelClicked(ClickLabel *);

protected:
virtual void mouseReleaseEvent(QMouseEvent * event);
};



Implentation file:



#include <QtGui>
#include "mainwindow.h"
#include "vhf.h"


labelDialog::labelDialog(QWidget *parent)
:QDialog(parent)
{

setupUi(this);
connect(ClickLabel, SIGNAL(linkActivated(const QString &)), this, SLOT(search()));
}

void labelDialog::search()
{
vhfDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
}
}

ClickLabel::ClickLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
}

ClickLabel::ClickLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
: QLabel(text, parent, f)
{
}


ClickLabel::~ClickLabel()
{
}


void ClickLabel::mouseReleaseEvent(QMouseEvent * event)
{
if (event->button() == Qt::LeftButton){
emit labelClicked(this);
}
}

jpn
11th October 2007, 19:12
Well, you're still connecting to linkActivated() signal which gets only emitted if you actually have links in the label. So connect to labelClicked() signal and make sure you promoted the label as ClickLabel in designer. You might need to declare ClickLabel in a separate header to be able to promote in designer.