PDA

View Full Version : mouseOver Label



eLancaster
29th January 2011, 13:22
I'm trying to write a program in which there is a QWidget with a QLabel inside of it. When the mouse moves over the QLabel, the text changes and when the moves away from QLabel it changes back to the original.
I've been reading the documentation and have become boggled about how to do this. Could someone tell me which class I ought to be using and give an example of its use.

Thanks

Lykurg
29th January 2011, 13:47
See QWidget::enterEvent() and QWidget::leaveEvent().

eLancaster
29th January 2011, 14:54
This is the code I wrote. I got errors in Widget.cpp file - what do i do about those?

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QLabel>
#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private:
Ui::Widget *ui;
};

class Label : public QLabel
{
Q_OBJECT

public:
Label(QWidget *parent=NULL);
~Label();

public slots:
void mouseOverLabel();
void mouseNotOverLabel();

protected:
virtual void enterEvent(QEvent *);
virtual void leaveEvent(QEvent *);

signals:
void enter(QString);
void leave(QString);

};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setFixedSize(145,45);

Label *label = new Label;
label->setGeometry(10,10,100,30);
label->setText("Where is the Mouse?");

connect(label,SIGNAL(enter(QString)),label,SLOT(mo useOverLabel()));
connect(label,SIGNAL(leave(QString)),label,SLOT(mo useNotOverLabel()));
}

void Label::mouseOverLabel()
{
label->setText("The Mouse is Over Me :-) ");ERROR: Label was not declared in this scope

}

void Label::mouseNotOverLabel()
{
label->setText("Where is the Mouse?");ERROR: Label was not declared in this scope

}

void Label::enterEvent(QEvent *e)ERROR:unused paramter e
{
emit enter( text() );
}

void Label::leaveEvent(QEvent *e)
{
emit leave( text() );
}

Widget::~Widget()
{
delete ui;
}

aamer4yu
30th January 2011, 06:14
ERROR: Label was not declared in this scope
There must be more to the error...
are u getting warnings or errors ?

tbscope
30th January 2011, 06:17
You have no object called "label" in your Label class.
I guess this is a copy and paste error.

Did you mean to use this?

setText("Where is the Mouse?");

(remove the label-> part)

eLancaster
30th January 2011, 21:12
OK I fixed the issues above, and the program not give any errors, but whenever I build the program, it terminates unexpectedly giving an exit status of 0. Why is that happening? The code is below:

LabelChange.h

#ifndef LABELCHANGE_H
#define LABELCHANGE_H

#include <QWidget>
#include <QLabel>

namespace Ui {
class LabelChange;
}

class LabelChange : public QWidget
{
Q_OBJECT

public:
explicit LabelChange(QWidget *parent = 0);
~LabelChange();

public slots:
void mouseOverLabel();
void mouseNotOverLabel();

protected:
virtual void enterEvent(QEvent *);
virtual void leaveEvent(QEvent *);

signals:
void enter(QString);
void leave(QString);
private:
Ui::LabelChange *ui;
QLabel *label;
};

#endif // LABELCHANGE_H

LabelChange.cpp


#include "labelchange.h"
#include "ui_labelchange.h"

LabelChange::LabelChange(QWidget *parent) :
QWidget(parent),
ui(new Ui::LabelChange)
{
ui->setupUi(this);
setFixedSize(145,45);

label->setGeometry(10,10,100,30);
label->setText("Where is the Mouse?");

connect(label,SIGNAL(enter(QString)),label,SLOT(mo useOverLabel()));
connect(label,SIGNAL(leave(QString)),label,SLOT(mo useNotOverLabel()));
}

void LabelChange::mouseOverLabel()
{
label->setText("The Mouse is Over Me :-) ");
}

void LabelChange::mouseNotOverLabel()
{
label->setText("Where is the Mouse?");
}

void LabelChange::enterEvent(QEvent *e)WARNING: UNUSED PARAMETER 'e'
{
emit enter( label->text());
}

void LabelChange::leaveEvent(QEvent *e)WARNING: UNUSED PARAMETER 'e'
{
emit leave(labeltext() );
}

LabelChange::~LabelChange()
{
delete ui;
}



Added after 7 minutes:

ERROR MESSAGE
The inferior stopped because it received a signal from the Operating System.

Signal name :
SIGSEGV
Signal meaning :
Segmentation fault

How can I remedy this?

Lykurg
30th January 2011, 21:12
Please make yourself familiar with basic debugging methods. But to answer you question: Where did you initialize the member called label?

eLancaster
30th January 2011, 21:22
Is there a link to find out about debugging methods, i'd appreciate it.
I initialize it in LabelChange.h. It's a private member.

Lykurg
30th January 2011, 21:47
I initialize it in LabelChange.h. It's a private member.Initialisation != Declaring. Hint: Null pointer.

As for learning how to debug, best use google for "gdb" or http://sourceware.org/gdb/current/onlinedocs/gdb/

eLancaster
31st January 2011, 02:12
OH! I see it.
and yeah it works too.
Thanks alot! and ill look over debugging.

eLancaster
1st February 2011, 08:36
I tried to improve the program by making it so that the cursor has to be precisely over the label to change the text. For, this i made a seperate label class and made an object of it in the widget class. I get an error of undefined reference in widget.cpp, and from what i've read, its because I need to make a destructor for the Label class - but how can I do that without making label a public member.

widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>

namespace Ui {
class Widget;
}


class Label : public QLabel
{
Q_OBJECT

public:
Label(QWidget *parent);
~Label();

protected:
virtual void enterEvent(QEvent *);
virtual void leaveEvent(QEvent *);

signals:
void enter(QString);
void leave(QString);
};

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

public slots:
void mouseOverLabel();
void mouseNotOverLabel();

private:
Ui::Widget *ui;
Label *label;
};

#endif // WIDGET_H


widget.cpp



#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
label = new Label(this);//Undefined Reference Label::Label(QWidget*)
label->setText("Where is the Mouse?");//Undefined Reference Label::Label(QWidget*)
connect(label,SIGNAL(enter(QString)),this,SLOT(mou seOverLabel()));
connect(label,SIGNAL(leave(QString)),this,SLOT(mou seNotOverLabel()));
}

void Widget::mouseOverLabel()
//code

void Widget::mouseNotOverLabel()
//

void Label::enterEvent(QEvent *)
//code

void Label::leaveEvent(QEvent *)
//code

Widget::~Widget()
//code