On 2009-07-01 08:58+0100 Alban Rochel wrote:

> Hello all,
>
> Actually, this is one of the few days I am in contact, so:
> - I must have misunderstood something about dynamic casts because I
> believed that QtPLDriver* widget=(QtPLDriver*)(pls->dev) would have
> created a "pure" QtPLDriver* object, and thus the member functions
> called after this would have been e.g. QtPLDriver::drawLine rather than
> e.g. QtRasterDevice::drawLine (no call of the virtual functions). This
> was the meaning of my "cryptic comment".
> - There is a simple and straightforward way to fix that without worrying
> about RTTI or whatever (well, at least for that): just define
> plD_whatever functions for every kind of driver, using simple C-casts.
>
> I'm afraid lots of these matters exceeded my knowledge in C++, but I'm
> interested by the explanations you provide!

Today, I have been taking a different tack looking at this from the point of
view of sip configuration following the instructions in
http://www.riverbankcomputing.co.uk/static/Docs/sip4/using.html.  There they
make a big deal of python versus C++ ownership of objects.  And they
advocate using the /TransferThis/ directive to deal with the issue. However,
it turns out that directive gives you syntax errors unless it is applied to
a "parent" argument that is the first argument without any default value.
Accordingly, this demands the order of arguments in QtPLWidget and
QtExtWidget be changed (I left the default parent parameter as zero in qt.h,
but not in plplot_pyqt4.sip since sip would not allow that).

For what it is worth, here is the patch for this change:

Index: bindings/qt_gui/pyqt4/plplot_pyqt4.sip
===================================================================
--- bindings/qt_gui/pyqt4/plplot_pyqt4.sip      (revision 10102)
+++ bindings/qt_gui/pyqt4/plplot_pyqt4.sip      (working copy)
@@ -22,7 +22,7 @@
  %End

    public:
-    QtPLWidget(int i_iWidth=QT_DEFAULT_X, int i_iHeight=QT_DEFAULT_Y, QWidget* 
parent=0);
+    QtPLWidget(QWidget* parent /TransferThis/, int i_iWidth=QT_DEFAULT_X, int 
i_iHeight=QT_DEFAULT_Y);
      virtual ~QtPLWidget();

      void clearWidget();
@@ -44,7 +44,7 @@
  %End

    public:
-    QtExtWidget(int i_iWidth=QT_DEFAULT_X, int i_iHeight=QT_DEFAULT_Y, 
QWidget* parent=0);
+    QtExtWidget(QWidget* parent /TransferThis/, int i_iWidth=QT_DEFAULT_X, int 
i_iHeight=QT_DEFAULT_Y);
      virtual ~QtExtWidget();

      void captureMousePlotCoords(double *x, double *y);
Index: bindings/qt_gui/plqt.cpp
===================================================================
--- bindings/qt_gui/plqt.cpp    (revision 10102)
+++ bindings/qt_gui/plqt.cpp    (working copy)
@@ -497,7 +497,7 @@
  #endif

  #if defined (PLD_qtwidget) || defined(PLD_extqt)
-QtPLWidget::QtPLWidget(int i_iWidth, int i_iHeight, QWidget* parent):
+QtPLWidget::QtPLWidget(QWidget* parent, int i_iWidth, int i_iHeight):
         QWidget(parent), QtPLDriver(i_iWidth, i_iHeight)
  {
        m_painterP=new QPainter;
@@ -893,8 +893,8 @@
  #endif

  #if defined(PLD_extqt)
-QtExtWidget::QtExtWidget(int i_iWidth, int i_iHeight, QWidget* parent):
-       QtPLWidget(i_iWidth, i_iHeight, parent)
+QtExtWidget::QtExtWidget(QWidget* parent, int i_iWidth, int i_iHeight):
+       QtPLWidget(parent, i_iWidth, i_iHeight)
  {
        cursorParameters.isTracking=false;
        cursorParameters.cursor_x=-1.0;
Index: include/qt.h
===================================================================
--- include/qt.h        (revision 10102)
+++ include/qt.h        (working copy)
@@ -343,7 +343,7 @@
        public:
                // Parameters are the actual size of the page, NOT the size of 
the widget
                // Call QWidget::resize for that
-               QtPLWidget(int i_iWidth=QT_DEFAULT_X, int 
i_iHeight=QT_DEFAULT_Y, QWidget* parent=0);
+               QtPLWidget(QWidget* parent=0, int i_iWidth=QT_DEFAULT_X, int 
i_iHeight=QT_DEFAULT_Y);

                virtual ~QtPLWidget();

@@ -395,7 +395,7 @@
        Q_OBJECT

        public:
-               QtExtWidget(int i_iWidth=QT_DEFAULT_X, int 
i_iHeight=QT_DEFAULT_Y, QWidget* parent=0);
+               QtExtWidget(QWidget* parent=0, int i_iWidth=QT_DEFAULT_X, int 
i_iHeight=QT_DEFAULT_Y);

                virtual ~QtExtWidget();

Index: drivers/qt.cpp
===================================================================
--- drivers/qt.cpp      (revision 10102)
+++ drivers/qt.cpp      (working copy)
@@ -895,14 +895,14 @@

    if (pls->xlength <= 0 || pls->ylength <= 0)
    {
-    widget=new QtPLWidget;
+    widget=new QtPLWidget();
      pls->dev=(void*) widget;
      pls->xlength = (int)widget->m_dWidth;
      pls->ylength = (int)widget->m_dHeight;
    }
    else
    {
-    widget=new QtPLWidget(pls->xlength, pls->ylength);
+    widget=new QtPLWidget(NULL, pls->xlength, pls->ylength);
      pls->dev=(void*) widget;
    }

Index: examples/python/pyqt4_test.py
===================================================================
--- examples/python/pyqt4_test.py       (revision 10102)
+++ examples/python/pyqt4_test.py       (working copy)
@@ -33,7 +33,7 @@
          print "init"
          QtGui.QMainWindow.__init__(self, None)

-        self.plot = plplot_pyqt4.QtExtWidget(800, 800, self)
+        self.plot = plplot_pyqt4.QtExtWidget(self, 800, 800)
          self.setCentralWidget(self.plot)

          plplot_pyqt4.plsetqtdev(self.plot)
Index: examples/c++/qt_PlotWindow.cpp
===================================================================
--- examples/c++/qt_PlotWindow.cpp      (revision 10102)
+++ examples/c++/qt_PlotWindow.cpp      (working copy)
@@ -35,7 +35,7 @@
        plotMenu->addAction("Histogram", this, SLOT(plotHistogram()));
        plotMenu->addAction("Interactive Selection", this, SLOT(interactive()));

-       plot=new QtExtWidget(QT_DEFAULT_X, QT_DEFAULT_Y, this);
+       plot=new QtExtWidget(this, QT_DEFAULT_X, QT_DEFAULT_Y);
        setCentralWidget(plot);

The result builds and installs without issues, and the installed qt_example
builds and runs without (valgrind or any other) issues.  However,
pyqt4_test.py still provides just a black GUI with no plot drawn (sigh).

So the above patch works as well as the present situation, but no better.
Thus, it only appears to be useful as an indication of where QtPLWidget and
QtExtWidget are resident in the source code, and as an example that shows
the /TransferThis/ sip directive is (at least) not the complete answer.

Hazen, if you want to try out further sip directives having to do with
object ownership, the above patch might be a good start for you.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________

------------------------------------------------------------------------------
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel

Reply via email to