Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Custom widget with covered part

[SOLVED] Custom widget with covered part

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geron
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      [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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Geron
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Geron
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Geron
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Geron
              wrote on last edited by
              #6

              How to set solved this issue?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved