Results 1 to 10 of 10

Thread: Mouse Over event on button

  1. #1
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Mouse Over event on button

    HI All,

    I am using qt 4.2.2 on my mac 10.4.7.
    I have created a button on which i have set an icon.
    When the dialog containing that button appears I want that when i move the mouse over the button the cursor symbol should change and also the icon should be enlarged.

    I set the cusor but actually it it changing when i click somewhere in the dialgo.But if I doesnot click any where in dialog and move mouse over the button it is showing the default cursor.

    If any body knows then plz plz help me.

  2. #2
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Over event on button

    try to sert mouse tracking on

  3. #3
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse Over event on button

    You may subclass QPushButton and reimplement enterEvent() and leaveEvent(). The first one is invoked when the mouse pointer gets over the button and the second one when it goes away from it, so you may change the icon to the big one in the first method and reset back to normal in the second one. Don't forget to call the methods from the superclass! It might be something like this:

    Qt Code:
    1. void MyPushButton::enterEvent(QEvent *event)
    2. {
    3. this->QPushButton::enterEvent(event);
    4. setIcon(QIcon(":/icons/bigbutton.png"));
    5. }
    6.  
    7. void MyPushButton::leaveEvent(QEvent *event)
    8. {
    9. this->QPushButton::leaveEvent(event);
    10. setIcon(QIcon(":/icons/smallbutton.png"));
    11. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: Mouse Over event on button

    Hi vfernandez ,

    thanks. But I have used these event but in this the image changes when I click somewhere on the Dialog but I want this when I move my mouse Cursor over the button not on the dialog.

    I have passed enterEvent and leaveEvent as a Signal and then using these function on that signal as a Slot.
    But I want tse button icon to change when I move my mouse cursor over the Image.

    Thanks.

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Over event on button

    You don't have to do anything with signal and slots.
    Reimplementing the enterEvent and the leaveEvent will do the magic.

    If you can post your code, we will be in a better position to help you out.

  6. #6
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Mouse Over event on button

    Hi,
    I have created a form with name SearchScanDiskDlg and have two buttons in it
    1. Photo recovery button
    2. createImageButton

    and calling this Dialog on a mainWindow Toolbar button click like.

    SearchScanDiskDlg *SearchScanDiskDlgObj=new SearchScanDiskDlg(this,0);
    SearchScanDiskDlgObj->setModal ( TRUE );

    I need when this dialog is shown and I move the cursor on any button then the button on which i move the mouse cursor will be enlarged and also i dont have to click anywhere on the dialog.


    Thanks.
    Last edited by vishal.chauhan; 8th January 2007 at 09:57. Reason: spelling error

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Over event on button

    You photoRecovery button and createImage button should be created from a class that reimplements the QAbstractButton (or may be QPushButton). Let will be something like

    Qt Code:
    1. class PictureButton : public QAbstractButton
    2. {
    3. public:
    4. //constructors and all other functions that you need.
    5. protected:
    6. void enterEvent(QEvent *event);
    7. void leaveEvent(QEvent *event);
    8. };
    To copy to clipboard, switch view to plain text mode 

    The implementation of enterEvent and leaveEvent is already given by vfernandez

    Now, you will need to create the buttons in following way

    Qt Code:
    1. PictureButton *photoRecoveryButton = new PictureButton(icon,parent);
    2. PictureButton *createImageButton = new PictureButton(icon,parent);
    To copy to clipboard, switch view to plain text mode 

    Hope this helps.

  8. #8
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: Mouse Over event on button

    Thanks for that.

    But Actually I have created the buttons as

    QGroupBox *groupBox;
    QPushButton *PhotoRecoveryButton;
    QPushButton *CreateImageButton;

    CreateImageButton = new QPushButton(Dialog);
    CreateImageButton->setObjectName(QString::fromUtf8("CreateImageButto n"));
    CreateImageButton->setWindowModality(Qt::NonModal);
    CreateImageButton->setGeometry(QRect(30, 230, 131, 121));
    CreateImageButton->setCursor(QCursor(static_cast<Qt::CursorShape>(13 )));
    CreateImageButton->setMouseTracking(true);
    CreateImageButton->setFocusPolicy(Qt::StrongFocus);
    CreateImageButton->setIcon(QIcon(QString::fromUtf8(":/images/CreateImageLogical.png")));
    CreateImageButton->setIconSize(QSize(128, 128));
    CreateImageButton->setCheckable(true);
    CreateImageButton->setChecked(false);
    CreateImageButton->setAutoDefault(true);
    CreateImageButton->setDefault(true);
    CreateImageButton->setFlat(true);
    QWidget::setTabOrder(CreateImageButton, PhotoRecoveryButton);



    and I have make a SearchScanDiskDlg.h in which I have declare the

    protected:

    void enterEvent(QEvent *event);
    void leaveEvent(QEvent *event);

    and then using these in .cpp file as

    void SearchScanDiskDlg::enterEvent(QEvent *event)
    {

    CreateImageButton->setIcon(QIcon(":/images/CreateImage1.png"));

    }



    void SearchScanDiskDlg::leaveEvent(QEvent *event)

    {

    CreateImageButton->setIcon(QIcon(":/images/CreateImageLogical.png"));

    }



    That is changes the icon on enterEvent and leaveEvent.

    It is changing the Icon only when I click somewhere on the Dialog
    I m sending you the form only to show u how it is looking.
    Attached Images Attached Images

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Mouse Over event on button

    As the previous posts explain, you have to use the enter/leave events within the Button Class, so you have to create a custom class inheriting from QAbstractButton. What you have done so far, was to implement the enter-, leaveEvents of the dialog class. They have nothing to do with the buttons. If you are reluctant to introducing your own button class, there is a simpler solution that is more than sufficient for this task: Installing an event filter with QObject::installEventFilter() ...

    check the Qt-Docs for this. It's fairly simple.

  10. #10
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Mouse Over event on button

    Thanks you to all.
    Now I m able to do this.

    Thanks.

Similar Threads

  1. Middle Button Mouse
    By jaime in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2006, 04:01
  2. Replies: 2
    Last Post: 24th July 2006, 19:36
  3. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 12:57
  4. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 10:24
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 20:25

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.