subcommanderblog.wordpress.com

Subcommander Development Blog

Archive for April 2008

Qt4 Item View Models..

leave a comment »

Today I learned something about implementing your own QAbstractItemModel with Qt4.

I worked on the subcommander (2.0) bookmark view to hide incomplete bookmarks (as in 1.2). If a bookmark has no source url, it is hidden. That’s useful if you don’t need the trunk/branches/tag repository bookmarks. Hiding them also removes some noise the bookmark view.

The second reason is that you can’t trigger a command on an incomplete bookmark from the gui. This avoids checks at various places if a bookmark is valid. The code is a lot cleaner if it can rely on a properly filled bookmark object.

The bookmark view is already based on a custom item model, so the easiest was to wrap the existing item model in a filter proxy model (QSortFilterProxyModel) and overwrite filterAcceptsRow().

Implementing the proxy model was just a few lines. No problem. Replacing the old model with the proxy model was a bit more work because the bookmark view class directly calls custom methods on the item model (to get a bookmark for a given index and getting the model index based on a bookmark object) that are not available in Qt’s interface.

That didn’t look right. I would have to change all this places to use the proxy model and have to delegate my custom methods through it? Ideally, it should be transparent. Just wrap the model into the proxy model, set it on the view, done.

I solved the “get bookmark for id” issue by making 2 changes. First I introduced a custom “role” to the item model. I can now call the data() method on the model to get the bookmark object. No need for a custom method. The second change is, that I call the data() method on the model index instead of the item model. I didn’t know until now that QModelIndex has its own data() method that delegates to the model. That way I don’t even need to know the model object.

old code:

void BookmarkView::contextMenuEvent( QContextMenuEvent* e )
{
  Bookmark* bookmark = _itemModel->bookmark( indexAt(e->pos()) );
  if( ! bookmark )
    return;
  ...
}

new code:

(since i need the interesting part multiple times I moved it to an extra method)

Bookmark* BookmarkView::getBookmark( const QModelIndex& index )
{
  return index.data(BookmarkViewItemModel::BookmarkRole).value<Bookmark*>();
}

void BookmarkView::contextMenuEvent( QContextMenuEvent* e )
{
  Bookmark* bookmark = getBookmark(indexAt(e->pos()));
  if( ! bookmark )
    return;
  ...
}            

I don’t have an idea for to the reverse lookup (bookmark to index) so I added a delegating method to the proxy model until I have a better idea.

So, what I have learned is:

  • avoid custom methods in an item model.
  • use custom roles to get data from the model (looks obvious now..;).
  • call data() on the index instead of the model.
Advertisements

Written by hauner

Sunday, 20 April, 2008 at 23:33

Posted in subcommander

Tagged with

Working on Subcommander 1.2.3

leave a comment »

Subcommander 1.2.2 does not build with gcc 4.3. I received a patch to fix the gcc 4.3 build issues (1.2.2 uses a few “ancient” includes). While I was applying and committing the patch I noticed that there are still a few bug fixes for 1.2 in the repository that were only released in the 1.2.2+ MacOSX universal build.

So the final 1.2.x (ie. 1.2.3) release will have a few additional bug fixes. After that I plan to concentrate on 2.0 again. :)

Unfortunately, 1.2.3 takes more time than I would like. Qt3 doesn’t work on MacOSX Leopard anymore. That’s bad because I used to test the source archive on MacOSX.

So I’m currently setting up a Linux system for Subcommander development to test the source archive:

I started with an iso image of ubuntu 7.04 that was still on my disk and installed it into VMware Fusion. There were a lot of updates and development packages I had to install. But that went *very* smooth. :)

The last thing I did was to install the Subcommander 1.2.2 package and checkout the 1.2.x branch. (Uhhh, Qt3 looks really bad in the Gnome environment.., maby i should have used kubuntu ;)

The next step will be to check the build and prepare the release. Subcommander 1.2.3 is on its way…

Written by hauner

Friday, 4 April, 2008 at 11:20

Posted in subcommander