开发者

Qwidget, how to highlight the widget under the cursor

I ha开发者_如何学Cve created some QLabel type of widgets in QT, and added that to a QToolbar. I want to highlight the particular widget which is under the cursor. I am unable to understand how do I do that. Can somebody please help ? I need this information on QT 4.

Thanks.


You have several possibilities. First, as you subclass QLabel, you can handle mouse events directly in your class. Make sure to use QWidget::setMouseTracking() to enable this. In such scenario you can do whatever you want with your control but you will have to override paint routine so that your class can draw itself in some specific way.

Unfortunately QLabel does not support "hover" style sheet state so that you cannot do it easily with styles sheets. However, if you consider subclassing from QPushButton you can have this wonderful feature so that with the help of CSS you get nice highliting effect. For more info on style sheets in QT look here.

If subclassing QPushButton is fine for you, then look here. Just make sure you also use hover state like in this simple example:

QPushButton:hover {
     background-color: black;
 }
QPushButton:hover {
     background-color: white;
 }

Example for the mouse events handling can be found here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜