PDA

View Full Version : Mouse Over event on button



vishal.chauhan
4th January 2007, 09:49
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.

kernel_panic
4th January 2007, 10:22
try to sert mouse tracking on

vfernandez
4th January 2007, 11:57
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:



void MyPushButton::enterEvent(QEvent *event)
{
this->QPushButton::enterEvent(event);
setIcon(QIcon(":/icons/bigbutton.png"));
}

void MyPushButton::leaveEvent(QEvent *event)
{
this->QPushButton::leaveEvent(event);
setIcon(QIcon(":/icons/smallbutton.png"));
}

vishal.chauhan
8th January 2007, 07:36
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.

munna
8th January 2007, 08:45
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.

vishal.chauhan
8th January 2007, 08:57
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.

munna
8th January 2007, 09:13
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




class PictureButton : public QAbstractButton
{
public:
//constructors and all other functions that you need.
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
};



The implementation of enterEvent and leaveEvent is already given by vfernandez

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




PictureButton *photoRecoveryButton = new PictureButton(icon,parent);
PictureButton *createImageButton = new PictureButton(icon,parent);



Hope this helps.

vishal.chauhan
8th January 2007, 09:58
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("CreateImageButton"));
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.

momesana
9th January 2007, 12:27
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.

vishal.chauhan
10th January 2007, 05:03
Thanks you to all.
Now I m able to do this.

Thanks.