PDA

View Full Version : Clickable Qlabel



lewis
18th July 2006, 16:56
Hi, I want to add the clicked() signal to a QLabel.

Can any1 point me the right direction to take?

jpn
18th July 2006, 16:57
Override QWidget::mousePressEvent() and/or QWidget::mouseReleaseEvent().
Emit the signal whenever you feel it's appropriate.. :)

lewis
18th July 2006, 17:00
:confused:

Im not sure I understand what you mean.

How and where can i override that event?

Is that a virtual function?

wysota
18th July 2006, 17:00
Hi, I want to add the clicked() signal to a QLabel.

Can any1 point me the right direction to take?

You can copy the code from wwActiveLabel (http://www.wysota.eu.org/wwwidgets). It should work fine without any changes (maybe one change is required, as widgets in Qt4 don't take the name argument in their constructors anymore).

lewis
18th July 2006, 17:04
Ill use that, but only if Im not able to make it myself quickly enough... I'd like to learn how to :) , thx tho

lewis
18th July 2006, 19:57
I get errors like these when using wwActiveLabel:

error C2027: use of undefined type 'QMouseEvent': see declaration of 'QMouseEvent'
error C2227: left of '->button' must point to class/struct/union/generic type

if i include QMouseEvent, i get linking errors.

im using qt 4.1.4 commercial

wysota
18th July 2006, 20:21
What kind of errors do you get?

This compiles fine:


/************************************************** *************************
* Copyright (C) 2004 by Witold Wysota *
* wysota@wysota.eu.org *
* *
************************************************** *************************/
#ifndef WWCLASSES_H
#define WWCLASSES_H

#include <QLabel>
#include <QTimer>
#include <QPoint>

/**
* @class wwActiveLabel
* @brief Label with mouse click and mouse move support
*
*
*/
class wwActiveLabel : public QLabel
{
Q_OBJECT
public:
wwActiveLabel(QWidget *parent = 0); ///< default constructor
~wwActiveLabel();
signals:
void doubleClicked(); ///< double click with LMB
void clicked(); ///< single click with LMB
void pressed(); ///< LMB pressed
void released(); ///< LMB released
void mouseOver(); ///< mouse pointer just entered the widget
void mouseOut(); ///< mouse pointer just left the widget
protected:
void mouseDoubleClickEvent ( QMouseEvent * e );
void mousePressEvent( QMouseEvent *e);
void mouseReleaseEvent( QMouseEvent *e);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
bool hitLabel(const QPoint &p);
bool m_pressed;
};

#endif


************************************************** *************************
* Copyright (C) 2004 by Witold Wysota *
* wysota@wysota.eu.org *
* *
************************************************** *************************/
#include "wwclasses.h"
#include <QMouseEvent>

wwActiveLabel::wwActiveLabel(QWidget *parent) : QLabel(parent) {
m_pressed = false;
setText("wwActiveLabel");
}
wwActiveLabel::~wwActiveLabel() {}
void wwActiveLabel::mouseDoubleClickEvent( QMouseEvent * e ) {
if(e->button() == Qt::LeftButton)
emit doubleClicked();
QLabel::mouseDoubleClickEvent(e);
}
void wwActiveLabel::mousePressEvent( QMouseEvent * e ) {
if(e->button() == Qt::LeftButton) {
m_pressed = true;
} else
m_pressed = false;
emit pressed();
}
void wwActiveLabel::mouseReleaseEvent( QMouseEvent * e ) {
if(m_pressed && e->button() == Qt::LeftButton && hitLabel(e->pos()))
emit clicked();
m_pressed = false;
emit released();

}
void wwActiveLabel::enterEvent( QEvent *e ) {
QLabel::enterEvent(e);
emit mouseOver();
}
void wwActiveLabel::leaveEvent( QEvent *e ) {
QLabel::leaveEvent(e);
emit mouseOut();
}
bool wwActiveLabel::hitLabel( const QPoint & p ){
return rect().contains(p);
}

lewis
18th July 2006, 20:33
Of course i had linking errors, i forgot to add it to my .pro file and regenerate the project file!!! dumb me!:o

wysota
18th July 2006, 20:35
Of course i had linking errors, i forgot to add it to my .pro file and regenerate the project file!!! dumb me!:o

If you use wwActiveLabel in a commercial product, please put a copyright notice somewhere in its docs and/or in the "about" window (if your app has one).

lewis
18th July 2006, 20:37
its not for commercial use, its only a little educationnal game that will be used inside a small non-profit organization.

Thx for ur wwActiveLabel widget!

Oleg S
4th June 2008, 10:33
Hi, I want to add the clicked() signal to a QLabel.

Can any1 point me the right direction to take?


Hi Lewis, this is Oleg S, I have the same problem that you, how did you solve it?

These are the messages errors:

error C2027: use of undefined type 'QMouseEvent'
c:\qt4.4.0\src\gui\kernel\qwidget.h(77) : see declaration of 'QMouseEvent'


error C2227: left of '->pos' must point to class/struct/union/generic type


Thanks so much!!

jpn
4th June 2008, 10:39
Oleg S,


#include <QMouseEvent>