23

See also related question: How do you debug Qt layout problems

I've got some complex widget hierarchies that I'm trying to lay out, and I run into the usual problems of things being closer together or further apart than I expect. I've found a very useful technique is to create a stylesheet with a list of different background and border colours for widgets at different points in the hierarchy. I've also added a QFileSystemWatcher to reload the stylesheet every time it's saved which allows rapid changes without needing to rebuild.

However, I keep thinking that there must be a better way. Looking at tools such as Firefox's web developer toolbar which will do things like mark borders or show the hierarchy of controls. Are there any tools or libraries that will do this for Qt apps?

UPDATE 5 May 2016

I've found one of the easiest and most informative ways of diagnosing spacing problems is to dump the widget hierarchy in terms of the QLayouts. I've created some sample code that embeds information about size hints, spacer items, padding and content margins: https://gist.github.com/pjwhams/6ebc040db3ab55615eafd831e184e39c

Another tool: https://github.com/robertknight/Qt-Inspector

Community
  • 1
  • 1
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • My brain tells me that I read an article at one point written by a developer at Trolltech/Nokia and they mentioned a utility or helper class they used. I can't for the life of me find that article. Maybe somebody else knows. – Arnold Spence Mar 24 '11 at 16:29
  • Are you referring to layout management in Qt Designer (as per the referenced question) or are you building your widgets and layout dynamically? – Derick Schoonbee Apr 02 '11 at 12:45
  • 1
    Well, I was looking for something like that a few weeks ago and gave up. I usually override the paintEvent of the widgets of interest and fill them with solid colors. I'm using stylesheets too, but that bit me in the ass once. – atamanroman Apr 04 '11 at 08:18
  • 1
    @Derrick - I am building layouts dynamically. I was thinking of either an external tool that can hook into the process and annotate the widget hierarchy at runtime, or a class/library that you can build in that overrides the drawing of each item to show diagnostic info, such as you get in CSS tools. – the_mandrill Apr 04 '11 at 12:39
  • @the_mandrill: Can you share your QFileSystemWatcher with us? – Christopher Oezbek Jul 25 '11 at 09:41
  • @Christopher. It's really simple, [QFileSystemWatcher](http://doc.qt.nokia.com/latest/qfilesystemwatcher.html) is a Qt class. Just connect to the `fileChanged(const QString&)` signal and call `qApp->setStyleSheet()` – the_mandrill Jul 25 '11 at 17:36
  • I'd recommend dhaumann answer of GammaRay – Roman A. Taycher Apr 06 '16 at 13:56

4 Answers4

4

I had a related follow-on question in another thread to which Dmitry came up with a great method that led to a good solution: Drawing an overlay on top of an application's window

EDIT: I've also found a useful technique hidden away in the documentation of QLayout::itemAt() which shows how to iterate through the QLayoutItems in a layout and draw their rectangles

Community
  • 1
  • 1
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
3

What you are looking for is basically a tool that analyzes the QObject or rather QWidget hierarchy and adds annotations. This is exactly what GammaRay is about. It is GPL licensed and therefore freely available. You can get further infos on the GammaRay project page. I've used it several times, and it works as expected.

dhaumann
  • 1,590
  • 13
  • 25
  • GammaRay can be useful, though I've found that if you have a custom Qt build then it can be difficult to get it built and running, especially on Windows. – the_mandrill May 05 '16 at 10:51
3

Could you use a QHoverEvent to add a dark border or a popup with an id every time you hover over an element?

chotchki
  • 4,258
  • 5
  • 34
  • 55
  • Interesting idea. Presumably though you would need to handle that in each class? Or could you monitor the events via the application? – the_mandrill Apr 07 '11 at 08:34
  • Yes you would unless you could use an Aspect Oriented Programming extension to auto decorate them. (I've only done that in java not c++). – chotchki Apr 07 '11 at 17:39
  • You can just set a stylesheet for the root QWidget with the rule QWidget:hover{ background-color: #f00; } – Tim MB Feb 19 '14 at 21:22
0

Late, but QObject::dumpObjectTree() helped me solve my issues.

Dumps a tree of children to the debug output.

Nick
  • 4,901
  • 40
  • 61
  • Note that this requires you to be using the debug version of the *Qt libraries*, not just your app, which may or may not be annoying to set up. – Devin Lane Aug 07 '15 at 22:12