PDA

View Full Version : Retrieving offsets of scroll in QWorkspace



hardgeus
8th May 2006, 23:46
This question is regarding QT4.

I am using QWorkspace to implement an MDI application. The app is a database design tool, and I am using the windows in the QWorkspace to represent DB tables. I am painting on the background to represent relations between the tables. Everything works fine except when the workspace is scrolled. First of all, I draw my relations based on "pristine" coordinates, so the relations do not scroll with the tables. Second, when I want to save out the positions of everything, I want to save out their unscrolled positions.

The bottom line is that I need to retrieve the current scroll state of the QWorkspace, but I cannot seem to find any way to do it. There doesn't seem to be any "scroll" signal emitted, nor anything I can trap in an eventfilter. The actual scrollbars themselves are private and inaccesible to my class which inherits from QWorkspace. I know there are nasty workarounds, but I could solve this problem very cleanly if I could simply figure out the current scroll state.

Is there any way to retrieve the current scroll offsets in a QWorkspace?

wysota
9th May 2006, 00:40
What you are doing is a little unclear for me... Are you painting on QWorkspace directly? You should be using child windows (probably QScrollArea based) and it should be easy to get coordinates of their objects. Can you explain (a screenshot could be handy here)?

hardgeus
9th May 2006, 01:24
What you are doing is a little unclear for me... Are you painting on QWorkspace directly? You should be using child windows (probably QScrollArea based) and it should be easy to get coordinates of their objects. Can you explain (a screenshot could be handy here)?

Here's a screenshot (click to enlarge)

http://img336.imageshack.us/img336/7329/pgdesignerscrollbar0ui.th.jpg (http://img336.imageshack.us/my.php?image=pgdesignerscrollbar0ui.jpg)

Yes, I am painting directly onto the QWorkspace. Everything works fine until I scroll. When I scroll, the drawing of the lines is wrong because the drawing code has no knowledge of the scroll state (NOTE: in the screenshot there is no scrolling going on...I am talking about scrolling of the enclosing workspace, not inside of the individual child windows). The problem isn't just the drawing of the lines. I also want to save out the absolute x,y coordinates of all windows to a save file, and these numbers will be wrong if the workspace is scrolled.

wysota
9th May 2006, 01:37
Your approach is strange, but ok :) Getting the values of those scrollbars will not be easy, because they are declared as private members of QWorkspace, so you have to "hack" the access restrictions using Qt's mechanisms.

If you look at QWorkspace source code, you'll notice, that the scrollbars are created and assigned names:

d->vbar = new QScrollBar(Qt::Vertical, this);
d->vbar->setObjectName(QLatin1String("vertical scrollbar"));
//...
d->hbar = new QScrollBar(Qt::Horizontal, this);
d->hbar->setObjectName(QLatin1String("horizontal scrollbar"));

Now you have to get a pointer to those items using QObject::findChild (or qFindChild() if you're using MSVC):

QScrollBar *vertical = workspace->findChild<QScrollBar*>(QLatin1String("vertical scrollbar"));
QScrollBar *horizontal = workspace->findChild<QScrollBar*>(QLatin1String("horizontal scrollbar"));

Then you can fetch their values using QAbstractSlider::value().

hardgeus
9th May 2006, 01:50
Your approach is strange, but ok :)
...


That's it, it's working now, thanks! I agree with you that my approach is somewhat odd, but I can't think of a cleaner way to do it. This is actually my third time rewriting this app:

http://www.hardgeus.com/projects/pgdesigner/

I initially wrote it in FLTK, then ported it to wxWindows, and finally I'm moving it to QT. For this go-round I have made a fundamental change, which is this somewhat strange approach you see. In my initial implementations, I implemented my own widgets from the base widget classes in the APIs, but 90% of my code ended up being funky event handling, drag and drop etc. etc. I decided this time I would just use built in widgets with minor subclassing.

I don't know if you've used Star Designor, or Microsoft's built in datamodel tool with MS SQL Server, but that's the sort of interface I'm going for. Do you have any cleaner ideas of how I could implement my widgets?

wysota
9th May 2006, 03:06
You could use Q3Canvas or wait for QGraphicsView (http://blogs.qtdeveloper.net/archives/2006/05/01/a-graphicsview-sneak-peek/) when Qt4.2 is released.

If you wish to stay with your approach, you should at least remove the decorations from child windows.

hardgeus
9th May 2006, 03:25
You could use Q3Canvas or wait for QGraphicsView (http://blogs.qtdeveloper.net/archives/2006/05/01/a-graphicsview-sneak-peek/) when Qt4.2 is released.

If you wish to stay with your approach, you should at least remove the decorations from child windows.

Yeah, QGraphicsView seems like a really good fit. It shouldn't be too incredibly difficult to port the current code over once 4.2 is released, as I'm really letting QT handle all of the dirty work right now so my code is pretty light.

In the meantime, by window decorations you're referring to the min/max button etc? I tried getting rid of them by setting window flags, but I couldn't get it to work.