PDA

View Full Version : QPushButton and mouse



lixo1
25th January 2009, 15:40
Hi everybody,
I'm absolutely a Qt4 newbie.
I have the following problem: I have a QPushButton, with a icon image (iconSize: 90x90px) I would like to associate another icon image when I pass the mouse over this button, without clicking.
I tried with style sheet but it doesn't works (in the designer it works but the image is not in 90x90px, but in the application it not works). I tried that but nothing:

QPushButton: hover {
background-image: url("mypicturefromresources.png");
background-position: center;
width: 90px;
height: 90px;
}

Is there another suggestion to do it in a easy way?
Thank you very much.

radek.z
25th January 2009, 15:49
Hi,

if I undesrtood you correctly then reimplement the void QWidget::mouseMoveEvent ( QMouseEvent * event ) shoudl help you.

Radek

drhex
25th January 2009, 23:14
Or perhaps better QWidget::enterEvent

wysota
26th January 2009, 02:10
You are setting the push button to be 90x90 which will probably not happen if it is inside a layout. The icon doesn't occupy the whole area of the button (for instance there are margins involved).

By the way, check out the "Common mistakes" section of Qt Style Sheets reference.

lixo1
26th January 2009, 16:05
Hi everyboy,
First of all thank you very much for all helps.
I think that the best way is overriting the mouseMoveEvent or maybe the enterEvent, but I'm substancially new, so I tried that:


void MyMainWindow::enterEvent(QEvent *){ //it will overrite the default enterEvent
//for example
ui->pushbutton_calculate->setIcon(QIcon("...my icon"));

}
This example will works everytime I pass the mouse over my MainWindow, so, how to determine if the mouse is over my pushbutton_calculate?
What I have to do?
Thank you another time.

greenvirag
28th January 2009, 11:03
It's because you didn't tell the program, that this event is for your pushbutton. Recently I made something similar in my program, but I used QGraphicsview, and HoverEvent.

I don't find this event at QPushButton.

Maybe you should do: write over the enterEvent(QEvent* event) of your pushbutton with explicit conversation to QHoverEvent*. To do this, you must make a subclass of QPushButton.

lixo1
28th January 2009, 18:45
Hi everybody,
So thank you very much, for yours opinions and explanations, now I really understand the Qt mechanism!
Cheers.