View Full Version : Custom widget: a button within a button

18th June 2009, 07:23
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,

18th June 2009, 07:52
subclass QAbstractButton and override the hitButton() and/or mouseevents... its easy.

18th June 2009, 08:23
But how do you handle rendering this way?

18th June 2009, 08:29
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 :)

18th June 2009, 09:03

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 (http://www.wysota.eu.org/wwwidgets/), your need is similar.

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

18th June 2009, 10:01
oh man.. that line edit is cool. why i did not saw it before?

18th June 2009, 10:03
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 :)