Results 1 to 11 of 11

Thread: mouseOver Label

  1. #1
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default mouseOver Label

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: mouseOver Label


  3. #3
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseOver Label

    This is the code I wrote. I got errors in Widget.cpp file - what do i do about those?

    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QLabel>
    5. #include <QWidget>
    6.  
    7. namespace Ui {
    8. class Widget;
    9. }
    10.  
    11. class Widget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Widget(QWidget *parent = 0);
    17. ~Widget();
    18.  
    19. private:
    20. Ui::Widget *ui;
    21. };
    22.  
    23. class Label : public QLabel
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. Label(QWidget *parent=NULL);
    29. ~Label();
    30.  
    31. public slots:
    32. void mouseOverLabel();
    33. void mouseNotOverLabel();
    34.  
    35. protected:
    36. virtual void enterEvent(QEvent *);
    37. virtual void leaveEvent(QEvent *);
    38.  
    39. signals:
    40. void enter(QString);
    41. void leave(QString);
    42.  
    43. };
    44.  
    45. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9. setFixedSize(145,45);
    10.  
    11. Label *label = new Label;
    12. label->setGeometry(10,10,100,30);
    13. label->setText("Where is the Mouse?");
    14.  
    15. connect(label,SIGNAL(enter(QString)),label,SLOT(mouseOverLabel()));
    16. connect(label,SIGNAL(leave(QString)),label,SLOT(mouseNotOverLabel()));
    17. }
    18.  
    19. void Label::mouseOverLabel()
    20. {
    21. label->setText("The Mouse is Over Me :-) ");
    To copy to clipboard, switch view to plain text mode 
    ERROR: Label was not declared in this scope
    Qt Code:
    1. }
    2.  
    3. void Label::mouseNotOverLabel()
    4. {
    5. label->setText("Where is the Mouse?");
    To copy to clipboard, switch view to plain text mode 
    ERROR: Label was not declared in this scope
    Qt Code:
    1. }
    2.  
    3. void Label::enterEvent(QEvent *e)[B][COLOR="red"]ERROR:unused paramter e[/COLOR][/B]
    4. {
    5. emit enter( text() );
    6. }
    7.  
    8. void Label::leaveEvent(QEvent *e)
    9. {
    10. emit leave( text() );
    11. }
    12.  
    13. Widget::~Widget()
    14. {
    15. delete ui;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by eLancaster; 29th January 2011 at 14:05.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseOver Label

    ERROR: Label was not declared in this scope
    There must be more to the error...
    are u getting warnings or errors ?

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: mouseOver Label

    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?
    Qt Code:
    1. setText("Where is the Mouse?");
    To copy to clipboard, switch view to plain text mode 

    (remove the label-> part)
    Last edited by tbscope; 30th January 2011 at 05:21.

  6. #6
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseOver Label

    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
    Qt Code:
    1. #ifndef LABELCHANGE_H
    2. #define LABELCHANGE_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6.  
    7. namespace Ui {
    8. class LabelChange;
    9. }
    10.  
    11. class LabelChange : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit LabelChange(QWidget *parent = 0);
    17. ~LabelChange();
    18.  
    19. public slots:
    20. void mouseOverLabel();
    21. void mouseNotOverLabel();
    22.  
    23. protected:
    24. virtual void enterEvent(QEvent *);
    25. virtual void leaveEvent(QEvent *);
    26.  
    27. signals:
    28. void enter(QString);
    29. void leave(QString);
    30. private:
    31. Ui::LabelChange *ui;
    32. QLabel *label;
    33. };
    34.  
    35. #endif // LABELCHANGE_H
    To copy to clipboard, switch view to plain text mode 

    LabelChange.cpp
    Qt Code:
    1. #include "labelchange.h"
    2. #include "ui_labelchange.h"
    3.  
    4. LabelChange::LabelChange(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::LabelChange)
    7. {
    8. ui->setupUi(this);
    9. setFixedSize(145,45);
    10.  
    11. label->setGeometry(10,10,100,30);
    12. label->setText("Where is the Mouse?");
    13.  
    14. connect(label,SIGNAL(enter(QString)),label,SLOT(mouseOverLabel()));
    15. connect(label,SIGNAL(leave(QString)),label,SLOT(mouseNotOverLabel()));
    16. }
    17.  
    18. void LabelChange::mouseOverLabel()
    19. {
    20. label->setText("The Mouse is Over Me :-) ");
    21. }
    22.  
    23. void LabelChange::mouseNotOverLabel()
    24. {
    25. label->setText("Where is the Mouse?");
    26. }
    27.  
    28. void LabelChange::enterEvent(QEvent *e)WARNING: UNUSED PARAMETER 'e'
    29. {
    30. emit enter( label->text());
    31. }
    32.  
    33. void LabelChange::leaveEvent(QEvent *e)WARNING: UNUSED PARAMETER 'e'
    34. {
    35. emit leave(labeltext() );
    36. }
    37.  
    38. LabelChange::~LabelChange()
    39. {
    40. delete ui;
    41. }
    To copy to clipboard, switch view to plain text mode 


    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?
    Last edited by eLancaster; 30th January 2011 at 20:12.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: mouseOver Label

    Please make yourself familiar with basic debugging methods. But to answer you question: Where did you initialize the member called label?

  8. #8
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseOver Label

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: mouseOver Label

    Quote Originally Posted by eLancaster View Post
    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/

  10. #10
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseOver Label

    OH! I see it.
    and yeah it works too.
    Thanks alot! and ill look over debugging.

  11. #11
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseOver Label

    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
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6.  
    7. namespace Ui {
    8. class Widget;
    9. }
    10.  
    11.  
    12. class Label : public QLabel
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Label(QWidget *parent);
    18. ~Label();
    19.  
    20. protected:
    21. virtual void enterEvent(QEvent *);
    22. virtual void leaveEvent(QEvent *);
    23.  
    24. signals:
    25. void enter(QString);
    26. void leave(QString);
    27. };
    28.  
    29. class Widget : public QWidget
    30. {
    31. Q_OBJECT
    32.  
    33. public:
    34. explicit Widget(QWidget *parent = 0);
    35. ~Widget();
    36.  
    37. public slots:
    38. void mouseOverLabel();
    39. void mouseNotOverLabel();
    40.  
    41. private:
    42. Ui::Widget *ui;
    43. Label *label;
    44. };
    45.  
    46. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    widget.cpp

    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9. label = new Label(this);//Undefined Reference Label::Label(QWidget*)
    10. label->setText("Where is the Mouse?");//Undefined Reference Label::Label(QWidget*)
    11. connect(label,SIGNAL(enter(QString)),this,SLOT(mouseOverLabel()));
    12. connect(label,SIGNAL(leave(QString)),this,SLOT(mouseNotOverLabel()));
    13. }
    14.  
    15. void Widget::mouseOverLabel()
    16. //code
    17.  
    18. void Widget::mouseNotOverLabel()
    19. //
    20.  
    21. void Label::enterEvent(QEvent *)
    22. //code
    23.  
    24. void Label::leaveEvent(QEvent *)
    25. //code
    26.  
    27. Widget::~Widget()
    28. //code
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 24th May 2009, 13:58
  2. QMenu: remain visible while mouseOver
    By vonCZ in forum Newbie
    Replies: 10
    Last Post: 25th October 2007, 16:54
  3. QT3: mouseover effect in QListViewItem
    By karye in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 12:44
  4. Mouseover signal on QAction
    By manhds in forum Qt Programming
    Replies: 14
    Last Post: 21st March 2006, 03:24
  5. Mouseover event on QPushButton
    By Twey in forum Newbie
    Replies: 2
    Last Post: 13th January 2006, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.