[PyQt] DiagramScene example

Lieven Buts lievbuts at vub.ac.be
Thu Jan 8 20:56:08 GMT 2009


Hi all,

during the holidays I found some time to work on the
Python version of the DiagramScene example. It was quite an
education :-) . I now have an almost working version that is a direct 
transposition of the C++ version from Qt 4.3.x. I will try to make it a bit
more Pythonic now, and get the last details working properly. If anybody
 wants to comment, the current version is available at 

http://ultr.vub.ac.be/~lieven/pyqt/diagramscene-1.tar.gz

Most things seem to work properly, except that the "Bold"
button keeps resetting all the time. I'm still looking into that.
I made some notes on porting issues which I'll organise later.

There is one problem with the original version which presents me
with a bit of a dilemma. There appears to be a logic error in the 
C++ version. The function

 bool DiagramScene::isItemChange(int type)
 {
     foreach (QGraphicsItem *item, selectedItems()) {
         if (item->type() == type)
             return true;
     }
     return false;
 }

returns true if _any_ item in the selectedItems() list matches
the requested type, and functions like

 void DiagramScene::setLineColor(const QColor &color)
 {
     myLineColor = color;
     if (isItemChange(Arrow::Type)) {
         Arrow *item =
             qgraphicsitem_cast<Arrow *>(selectedItems().first());
         item->setColor(myLineColor);
         update();
     }
 }

then go to work on the _first_ item in the list, which may or
may not be of the right type. Because of this, it possible to
crash the C++ version, for instance by selecting first a 
DiagramItem, adding a text item to the selection, and
changing the font. The code then tries to call setFont(...)
on a DiagramItem, which does not support this method.
So I removed isItemChange, and replaced it with a
getSelectedItemsByType function. Of course this means
that the Python version is not an exact port any more.

Best wishes for the new year,

-- 
Dr. ir. Lieven Buts, Postdoctoral Fellow
Structural Biology Brussels, Vrije Universiteit Brussel
Department of Cellular and Molecular Interactions, VIB


More information about the PyQt mailing list