Results 1 to 8 of 8

Thread: Custom widget: a button within a button

  1. #1
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Custom widget: a button within a button

    Hello everybody,

    I know, this may sound weird by the title. I'm working on an application that includes a panel which acts like a tab bar, excepted that is also has the option of showing no widget at all to save space. Users of KDE or Qt Creator probably understand what I am talking about, others can look at the following screenshot:



    I've implemented this bar using a horizontal layout and some QToolButtons, and it works great. But now I need to insert a button within the tool button that would emit a custom signal when clicked. Here is a mockup:



    I looked at various sources, including the code for QPushButton, and I see no easy way to implement that. Moreover this is the first time I would do such a thing. I don't need complete layouting, just to display a button after the text that would receive mouse events. Would anybody have a couple of hints to guide me through this?

    Thanks in advance,
    Alex.
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom widget: a button within a button

    subclass QAbstractButton and override the hitButton() and/or mouseevents... its easy.

  3. #3
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom widget: a button within a button

    But how do you handle rendering this way?

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom widget: a button within a button

    your button is instresting... i dont know why but i want to code the button.. let me fire up QtCreator, lets see wether i am good enough to code or not

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Custom widget: a button within a button

    Hi,

    • use QToolButton as basis class
    • reimp sizeHint() and add a extra space
    • reimp paint event and draw your close button
    • reimp mouse press event handlers and check there, if your close button is hit.


    or have a look at QwwClearLineEdit, your need is similar.

  6. The following user says thank you to Lykurg for this useful post:

    Gnurou (18th June 2009)

  7. #6
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom widget: a button within a button

    Lykurg: thanks, this is indeed exactly what I need. I'll have a look at the code, it seems easier than I thought.

  8. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom widget: a button within a button

    oh man.. that line edit is cool. why i did not saw it before?

  9. #8
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Custom widget: a button within a button

    Or you can do the following:
    Create a custom QWidget with a horizontal layout, were you place a QLabel on the left with your text and a QLabel on the right with a pixmap with an X.
    You need to enable mousetracking and reimplement mousedown/up/pressed to catch various the mouse actions. (and paint if you want visual changed on down, up, active etc...)

    QAssistant does this on it's own tabs, so I guess you can take a look at the code for that one. Assistant is not using toolbuttons, but a QTabBar with cornerwidgets on the tabs. and since you can style the TabBar with Style sheets, it's not to hard to get it working. The cool thing with corner widgets, is that you can add any QWidget or subclass in there

Similar Threads

  1. How to use custom widget editor with QItemDelegate
    By wysman in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 18:20
  2. Playbutton functionality
    By uchennaanyanwu in forum Qt Programming
    Replies: 5
    Last Post: 31st July 2008, 22:29
  3. Custom Widget - First Steps
    By sekatsim in forum Qt Programming
    Replies: 8
    Last Post: 26th June 2008, 17:19
  4. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  5. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 15:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.