PDA

View Full Version : linkActivated signal in QLabel?



anupamgee
27th April 2009, 13:21
hi friends,
I used a qlabel .I want to call a customized SLOT on the click of QLabel.
But it is not working for me ..................
i used the following code:
tabwindow.cpp



TabWindow::TabWindow(QWidget *parent): QWidget(parent)
{
onlinelabel = new QLabel(this);
onlinelabel->setText(tr("label"));
onlinelabel->setGeometry(11,46,50,16);
connect(onlinelabel,SIGNAL(linkActivated(QString)) ,this,SLOT(showmenu(QString)));
}

void TabWindow::showmenu(const QString & link)
{ //some code
}


tabwindow.h


class TabWindow : public QWidget
{
Q_OBJECT
public:
TabWindow(QWidget *parent = 0);
QLabel *onlinelabel;
public slots:
void showmenu(const QString & link);
};


What i am doing wrong??

Lykurg
27th April 2009, 14:10
linkActivated is emitted if you click a HTML link (a). And since you set the text 'label' the signal newer will get emitted. And it seems to me you may will have a look at QPushButton or QToolButton to get a "label" with a menu...

SABROG
27th April 2009, 19:09
If you want QLabel with signal clicked() try this:



#include <QtGui/QApplication>
#include <QtGui/QMouseEvent>
#include <QtGui/QLabel>

class QExLabel : public QLabel
{
Q_OBJECT
public:
QExLabel(QWidget *parent = 0) : QLabel(parent){};
signals:
void clicked();
protected:
void mouseReleaseEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
emit clicked();
}
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QExLabel label;
QObject::connect(&label, SIGNAL(clicked()), &app, SLOT(quit()));
label.show();
return app.exec();
}

#include <main.moc>

Lykurg
27th April 2009, 20:28
If you want QLabel with signal clicked() try this:
This isn't a reaction a user would expect. Why: you press the mouse anywhere down, move it over the label and then release it. In you case the signal would be emitted. But normally you also have to check if the mouse was pressed down over your label.