Results 1 to 9 of 9

Thread: Custom QwtPlot class

  1. #1
    Join Date
    Oct 2007
    Location
    Warminster, England
    Posts
    31
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Custom QwtPlot class

    I am attempting to create my own custom QwtPlot class, and use a static_cast to override the existing QwtPlot object in the programs dialog.
    The dialog has a QwtPlot object called plotpage, I need to exchange my own variable called plot from my class myQwtPlot
    Qt Code:
    1. QwtPlot* plot = static_cast<myQwtPlot*>(plotpage);
    2. if(!plot)qFatal("Failed to cast the plot pointer from myQwtPlot to QwtPlot");
    3. plot->setTitle("Account Balance Plot");
    To copy to clipboard, switch view to plain text mode 
    The code above is extracted from the dialog class constructor and appears to run correctly. When I attempt to use the plot variable in other member functions I get a SIGSEGV failure. The program is still expecting the original plotpage variable.
    Could some one confirm I am using the correct approach, and have the correct syntax for the static_cast command.
    Thanks,
    Carlton.

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,241
    Thanked 860 Times in 809 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom QwtPlot class

    Without knowing about the declaration/assignment of the plotpage variable nobody can tell you if your code is correct. IMO the only reasonable situation where your cast makes sense is something like this:

    Qt Code:
    1. QWidget *plotpage = plot1; // something derived from QwtPlot
    2. // ...
    3. QwtPlot *plot2 = dynamic_cast<QwtPlot *>(plotpage);
    To copy to clipboard, switch view to plain text mode 
    As long as you don't have rtti disabled don't use static_casts for situations like this.

    Uwe

  3. #3
    Join Date
    Oct 2007
    Location
    Warminster, England
    Posts
    31
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QwtPlot class

    plotpage is declared in the dialog header file (generated by Creator) as
    Qt Code:
    1. QtPlot *plotpage;
    To copy to clipboard, switch view to plain text mode 
    In your example above, plot2 would also be a QwtPlot object, I need it as a myQwtPlot object to enable my own class functions to override the QwtPlot functions.
    myQwtPlot is defined as follows;
    Qt Code:
    1. lass myQwtPlot : public QwtPlot
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit myQwtPlot(QWidget *parent = 0);
    6.  
    7. signals:
    8.  
    9. public slots:
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    Since there is only a single item to cast at the start of the program, I didn't think I would need a dynamic_cast, and avoid the need for rtti
    Carlton.

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,241
    Thanked 860 Times in 809 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom QwtPlot class

    Ok, plotpage is declared as QtPlot - but what is this ?

    If it is something derived from QwtPlot you don't need to cast at all. If not your cast is very, very wrong and all it does is to convert a compile error into a runtime error.

    Again your cast makes only sense when plotpage is declared as a baseclass of QwtPlot ( QObject, QWidget, QFrame ) or maybe a void * - and of course a object of a class derived from QwtPlot has been assigned to it.

    Uwe

  5. The following user says thank you to Uwe for this useful post:

    Carlton (6th September 2010)

  6. #5
    Join Date
    Oct 2007
    Location
    Warminster, England
    Posts
    31
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QwtPlot class

    plotpage is a QwtPlot object, I don't know how the 'w' dropped out on the cut & paste earlier.
    Qt Code:
    1. QwtPlot *plotpage;
    To copy to clipboard, switch view to plain text mode 
    The intention is to convert the existing plotpage object ( class QwtPlot ) to my own derived class myQwtPlot.

  7. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,241
    Thanked 860 Times in 809 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom QwtPlot class

    But when the result is assigned to a local variable, that is declared as QwtPlot *, the const_cast is pointless.

    Uwe

  8. #7
    Join Date
    Oct 2007
    Location
    Warminster, England
    Posts
    31
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QwtPlot class

    The intention was to keep using the base class in the .ui file, then cast it to my derived class to enable me
    to use my own version of QwtPlot, so I can override the picker and zoom functions.

  9. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,241
    Thanked 860 Times in 809 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom QwtPlot class

    But this is not what the the code does, that you have posted !

    Also QwtPlot has no picker or zoom functionality. If you want to manipulate zooming/picking you have to derive from QwtPlotZoomer, QwtPlotPicker.

    Uwe

  10. The following user says thank you to Uwe for this useful post:

    Carlton (6th September 2010)

  11. #9
    Join Date
    Oct 2007
    Location
    Warminster, England
    Posts
    31
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QwtPlot class

    Thanks for the help, it is much appreciated. I shall return to the manual again.

Similar Threads

  1. QwtPlot - promote to custom widget
    By MrGarbage in forum Qwt
    Replies: 7
    Last Post: 10th February 2011, 10:12
  2. Replies: 1
    Last Post: 6th May 2010, 11:05
  3. qhash with custom class
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 06:30
  4. custom class for tslib
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 28th April 2009, 13:15
  5. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.