[Qt-interest] newbe alert - I am trying to convert plotting from C# to QT4

Constantin Makshin cmakshin at gmail.com
Tue Apr 12 18:59:35 CEST 2011


My idea:
1) create Plot class derived from QObject — as the name suggests, it'll represent your plot;
2) add a QGraphicsScene object to the class — this object will contain data (QGraphicsItem-s) Qt will use to draw the plot;
3) add update() or similar slot that will update (update/recreate QGraphicsItem-s) the plot when called, i.e. it'll act as your Panel_paint() function. In the end of this function don't forget to call update() function of the object created in (2) to schedule plot repainting;
4) add other members as necessary;
5) create a QGraphicsView widget — it'll be used to show the plot, i.e. it'll act as your Panel class. Set the object created in (2) as the view's scene.

Now, each time you need to update the plot, call the update() function of your Plot object, either directly or through a signal-slot connection. For convenience, you can set the QGraphicsView object as the Plot object's parent to destroy the plot when the widget is destroyed.

Example:

// plot.h
class Plot : public QObject
{
    Q_OBJECT;

    QGraphicsScene m_scene;

public:
    Plot (QObject* parent = NULL);

    inline QGraphicsScene* scene () const;

public slots:
    void update ();
};

inline QGraphicsScene* Plot::scene () const
{
    return &m_scene;
}

// plot.cpp
Plot::Plot (QObject* parent) : QObject(parent)
{
    // ...
}

void Plot::update ()
{
    // ...
    m_scene.update();
}

// form.h
class Form : public QWidget
{
    Q_OBJECT;

    Plot* m_plot;
    QPushButton* m_button;

public:
    Form (QWidget* parent = NULL);

    void someFunc ();
};

// form.cpp
Form::Form (QWidget* parent) : QWidget(parent)
{
    QGraphicsView* plotView = new QGraphicsView(this);
    m_plot = new Plot(plotView);
    plotView->setScene(m_plot.scene());

    m_button = new QPushButton(tr("Click me"), this);
    connect(m_button, SIGNAL(pressed()), m_plot, SLOT(update()));

    // ...
}

void Form::someFunc ()
{
    // ...
    m_plot->update();
}

On Tuesday 12 April 2011 19:45:54 Doug Stewart wrote:
> I am just learning QT4 and want to do a plotting program in qt4 similar to
> the one I have running in C#
> 
> In C# I have a Panel added to a Form and I do all the drawing on this panel.
> In C# when I double click on the Panel it makes a function called
> Panel_paint.. etc.
> 
> Panel_paint gets called whenever It needs refreshed (from the OS or from an
> Invalidate call).
> I then put inside Panel_paint all the code to read an array and use the
> line_draw function to plot the graph.
> 
> Now in qt-creator I am trying to do the same steps
>   - I can't fined a Panel
>   - I did find graphicView  but am not sure how to use it
>    -I also see  slot   and select a signal but nothing for painting!
> 
> Could some one help me get to the point where I can plot an array full of
> numbers?
> Once I get past this hurdle The rest of QT4 looks fairly smooth sailing!
> 
> Doug Stewart
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110412/2ddd90a9/attachment.bin 


More information about the Qt-interest-old mailing list