[SOLVED] Custom widget with covered part



  • Hi,

    I want something like this:
    http://oi41.tinypic.com/2w3df11.jpg

    !http://oi41.tinypic.com/2w3df11.jpg(labeledButton)!

    I faced a problem: i need a QPushButton with a QLabel. The QLabel must be at the top of QPushButton and the QLabel must cover the top of the PushButton a litle bit. Moreover, such QPushButton with QLabel pairs must be ordered in grid layout. How could i solve this problem on the most efficient way?

    I found only the following way:
    Create custom button which inherits from QAbstractButton. Reimplement paintevent which draws a label as well. In this case i have the following important questions:
    How can i solve in an efficient way that i want handle only mouse events from the painted part of the custom widget? Or how can i make a custom widget which mouse area is not a rect?

    PS:
    I decided to not use a container widget contained a pushbutton and a label which has fixed coordinates or manual layout inside the container widget. I think this way not an efficient way: container widget seems redundant and fix coordinates are wished to be be avoided...

    Any other idea how to solve this issue?

    Thank you in advance your thoughts!


  • Moderators

    [quote author="Geron" date="1389312130"]I think this way not an efficient way: container widget seems redundant and fix coordinates are wished to be be avoided...[/quote]
    Why do you think that? This saves ALOT of work.

    [quote author="Geron" date="1389312130"]
    How can i solve in an efficient way that i want handle only mouse events from the painted part of the custom widget? Or how can i make a custom widget which mouse area is not a rect?
    [/quote]
    Otherwise inherit QPushButton and reimplement sizeHint() and paintEvent() methods as you already stated.
    To check if the mouse position is in an non-rect area you will need to this manually. Use QRegion, QPolygon, QPainterPath, ... and use their contains()/containsPoint() methods.

    But i would recommend the container widget approach, since this simplifies alot. And i doubt that you will notice any drawbacks from it.



  • Very thank you for your replies!
    Now im trying the QPolygon + containsPoint() solution to check wether the mouse position is in an non-rect area.

    It's a good question why i would like to avoid a container widget:

    • i have 10 000 item on lots pages, so it would be plus 10 000 widgets in the memory. I'm using page widget.
    • it would be a more complicated structure of the code... Although custom widget would have a more complicated paintevent - mouseevent,
    • but you are right the positioning will be similar as if i would do it with custom widget,

    One additional information: the picture in this topic is made with container widget,

    Now my question is towards people: which is a better solution custom widget or container widget?



  • So the quetion is what are the advantages / disadvantages : custom widget vs container widget in case of this problem?



  • Ok. It's solved. The solution will be: use a pushbutton as a layout item.
    And the pushbutton has a label. But the label has no parent. So the label can cover the pushbutton and can be independent of the layout items.
    Thank you for the clever guy who we talked about this:) If you have read this, thank you!



  • How to set solved this issue?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.