CommandLink buttons with Qt

Published Friday May 18th, 2007
14 Comments on CommandLink buttons with Qt
Posted in Qt, Uncategorized

Windows Vista introduced the concept of command links. A command link is a special button type primarily designed to streamline Wizard-like interfaces. Pressing a command link should always take you to a different page, just like in a web browser. In addition to the visual difference, it can also include a descriptive text.

Example :
Qt command link

A command link class did not make it into 4.3, and it has not yet been decided if it will be part of our standard widget set.
However some support was added in the style API to support these features, so I decided to make a small
example on how to implement this functionality using Qt 4.3. In fact, the screen shot above was made using Qt.

You can try out the following subclass of QPushButton that adds these features : commandlink.h and commandlink.cpp . Note that the size hint calculation is somewhat rudimentary and does not guarantee the visibility of the description text.

To use this in a QWizard you could do something like this :

QWizard wizard;
QWizardPage page;
QVBoxLayout layout;
CommandLink button;
button.setText(“&Press me!”);
button.setDescription(“Pressing this button should do something”);
layout.addWidget(&button);
page.setLayout(&layout);
wizard.addPage(&page);
wizard.show();

The only thing remaining now is to connect the clicked signal to for instance QWizard::next(). Note that the code will also work on other platforms and styles. This is the above button rendered with Plastique style :

Qt command link

Update 05.21.07 : Tweaked the font settings a bit

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Qt, Uncategorized

14 comments

AlKu says:

Wow, this is awesome. I did submit this sort of thing a couple of times on the bug tracker and contacted developers about it.
Finally a somehow good solution is available. I was creating this button myself from scratch before but it wouldn’t look too perfect.
Anyway, it looks good but what I immediately noticed is that I think you got the font inside the buttons wrong. On Vista the button uses (I think Segoe UI) at size 16px. Just compare your screenshot and the actual Windows Vista “Add Printer” dialog πŸ™‚

Jason says:

I never understand how you determine what to include in Qt releases. I mean, something like this has a ton more applicability than MapReduce. I still don’t know why you ever made Jambi either, since Qt compiles on more platforms than Java runs on.

Is there somewhere we can vote for what goes into Qt4.4?

AlKu says:

Oh, one more thing. Is there ever gonna be support for Vistas “reduced” client areas with those large glass window frames inside the client area (like in IE7 and WMP)?
Currently I do a little hack with the Windows dwmapi.dll and its “DwmExtendFrameIntoClientArea” function, then I use the Qt::WA_NoSystemBackground attribute for my main window and override the paintEvent() so I just paint the background where the client area is (otherwise it will be painted everywhere). But the problem here is the lack of Qt’s ability to paint stuff on transparent window backgrounds (to recreate: simply create a QMainWindow, set the Qt::WA_NoSystemBackground or Qt::WA_OpaquePaintEvent on it, put some widgets inside and then just run the app – the background becomes black :(). So when you put QPushButtons into that glass frame (with my overridden paintEvent()) and press them once, they’ll get black edges (where they’re used to be transparent pixels)…

AlKu says:

Jason made a good point though. I think more of those Vista features should be implemented, because not having them (or not being able to workaround to them, like I said in my previous comment about the glass frame and Qt’s lack of painting on a transparent window background) is for some developers (like myself) a pretty heavy drawback which makes me thinking about moving back to MFC or even forward to .NET 3.
I mean all those tools you have are real cool too, but I think the visual aspect plays a very important role too. I’m not just talking about the command links, painting on transparent system background, or the resized client areas, it’s alot of other things too (e.g. selection-pixmap on items in views, wrong font choices [Task Tracker #136762], QProgressBar on Vista looks ridiculous, behaves different, and the valueless progress bar looks nothing like it should […]). Also it’s kinda annoying that on XP alot of my custom visual styles I used are displayed wrong, so I’m excited how Qt4 will work with custom Vista visual styles in the future.
Well, I shouldn’t complain anyway since it’s a pretty good toolkit and I’m only a open source Qt user πŸ˜€

jbache says:

AlKu: I’m glad you appreciate it. I am aware of the font issue and might do some more tweaks to it later. If you can live with hard coded fonts for now, setting it in the code yourself should be fairly straight forward though. Regarding your second question I’m not entirely sure. This feature is hard to directly support on anything but Vista so adding it in a cross platform way might not make all that much sense. However I believe we have solved some of the issues you mention internally and it sounds like something we could get back to in a later blog entry. Regarding your other “issues”, they are all being tracked as normal bugs and are likely to be resolved in the near future.

Jason: Yes, there is in fact a customer voting system in our task tracker. This is what the current “top 10” lists are based on.

AlKu says:

Yes, I did have to set the font on my own to solve this “issue” πŸ™‚
I can see your point about the platform dependency and the only way I can think of is to simply ignore the functions on other platforms than Vista but this is a pretty weak solution so it’d be best just to not have it at all (?).
Anyway, those issues aren’t too big of a problem to me since I can do them myself with a few .dll calls. But what is really bugging me is that Qt can’t paint on transparent window backgrounds. Until recently I never thought Qt couldn’t do it because this surely is something that gets more attention every day (e.g. Windows Sidebar gadgets, Trillian 4 windows, etc.). This makes it impossible for me to display something like this on my desktop:
http://www.closetosoftware.com/images/cad2007.jpg
So, when you said “we have solved some of the issues you mention internally”, did you refer to this? Do you think this will be ‘fixed’ any time soon, maybe even 4.3 Final?
Thanks for the information though πŸ™‚

Branan says:

With compositing WMs like Compiz and Kwin-composite, it should be possible to implement everything Vista can do and more on X11. I expect MacOS X can do most of the fancy graphics stuff as well.

Alku: There’s a tutorial that shows creating a shaped window. I’m not certain it will do what you want (I haven’t looked at Vista’s new APIs much yet), but if you want to do something like Windows Media Player, it might work like you want. http://doc.trolltech.com/4.3/widgets-shapedclock.html

Andreas says:

Alku, usually when a feature cannot go into Qt it’s because Qt cannot provide the feature on all its supported platforms. Remember that Qt is a cross-platform toolkit, and even if it’s easy to provide a specific feature for Vista and XP, if Mac OS X and Qtopia or the other unixes cannot support it (or don’t provide 100% support for the feature even if they /could/ support it), it gets really hard to add. This isn’t about toolkits really; other toolkits probably can’t support the feature either.

As for transparent toplevels, this is currently not possible on X11 unless you use XComposite, an extension supported only by the latest X servers, and only on certain special graphics cards. So if you base your app on this feature, your app isn’t going to be portable, unfortunately. Vista widgets – certainly some are interesting in general. But some are covered by foreign licensing, so again, it’s hard for us to just throw them into Qt.

If you’re interested in discussing the topic in general, I’d like to recommend qt-interest, or other public forums. (since this is slightly OT…)

Jason says:

jbache, I know about that, but I am speaking more generally. More like development initiatives. Do you have a list for 4.4? Like 4.3 has QtScript, DirectX drawing, etc…

andyed says:

Sorry Andreas I do not want to prolong the top level widget thing. But on Windows we have managed to do this via a few GDI calls but of course anti aliasing is against whatever the backing store has. On Mac it is more difficult because you have to mod Qt to get the window into the KoverlayWindowclass to support top level transparency. I don’t think you can shift a window into the Koverlaywindowclass afer creation.

jbache says:

Eron: It sounds like you could use QLabel::linkActivated() to achieve this.

AlKu says:

I got one more question though πŸ™‚
I’ve submitted the issue about QProgressBar on Vista not using the fixed pulse width (http://trolltech.com/developer/task-tracker/index_html?method=entry&id=157332) and it’s now set to Open/Active for over a month now. For me this is a very annoying issue. For one thing it just looks really really weird when the progress value is small; and the other thing is that when the progress value is small, the pulse glowing width is also really small and the animation seems quite laggy to the eye (doesn’t move very smooth from left to right).
Just out of curiosity, will this be addressed soon or for the final 4.3 release πŸ™‚ ?

jbache says:

alku: This issue has in fact already been fixed in the latest snapshots and it is quite possible that it will make it into 4.3.0.

Commenting closed.

Get started today with Qt Download now