0

I have a class TimeGraph with a qwtplot. This qwtplot is displayed on a QGraphicsView. A qwtplot has method resize en resizeEvent but I don't understand how use them.

TimeGraph::TimeGraph(QWidget* parent, int in_display_time, QwtText title)
{
    display_time = in_display_time;

    graph.setParent(parent);
    graph.setFixedSize(parent->width() - 20, parent->height() - 20);
    graph.move(QPoint(10, 10));

    grid.attach(&graph);
    curve.attach(&graph);
    curve.setPen(QPen(Qt::red, 2));
    curve.setRenderHint(QwtPlotItem::RenderAntialiased);

    if (title.text() != NULL)
        graph.setTitle(title);

    graph.setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(QDateTime(QDate(0, 0, 0), QTime(0, 0, 0, 0))));
    graph.setAxisScale(QwtPlot::xBottom, - display_time, 0);
    graph.setAxisLabelRotation(QwtPlot::xBottom, -20.0);
    graph.setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);

    graph.show();
}

And the header

struct Data { QVector<double> x; QVector<double> y;};

class TimeGraph
{
private :
    QwtPlot graph;
    QwtPlotCurve curve;
    QwtPlotGrid grid;
    Data data;
    int display_time;

public:
    TimeGraph(QWidget* parent, int in_display_time, QwtText title = QwtText());
    void addPoint(QDateTime date, double y);
    void resize(QRect zone);
};

And I create my graph as this :

graph_O2 = new TimeGraph(ui->graphicsView_graph_o2, 120);

I would my graph resize itself when the graphicsView is resized. How I can do ?

artoon
  • 729
  • 2
  • 14
  • 41

3 Answers3

0

Is it really a must have to put qwt plot on top of qgraphicsview? I didn't use Qwt for a while, but AFAIK all it's drawing done using straight paint in paintEvent. (that one of the reasons why it's so fast). Most proper way to use Qwt controls on QGraphicsView (up to me) is to add widget item with ignore transformation flags. And regarding resize if you put Qwt inside usual widget it will handle all resize properly on itself.. if you have some complicated structure with custom resizing you should handle resize event on parent widget or through eventFilter if parent class is not available.

evilruff
  • 3,947
  • 1
  • 15
  • 27
  • No, the qwt plot don't must really be on QGraphicsWiew. But I don't know how do. My qwt plot must be created by the code because the pluggin qwt for the QtDesigner doesn't work with my version. Without QGraphicsView how do to place my plot in a QGroupBox where I want ? – artoon Apr 16 '13 at 09:36
  • I made another answer with sample (see above) – evilruff Apr 16 '13 at 10:03
  • Ok, I have my TimeGraph in my Widget. Now, my class TimeGraph must inherit of QWidget ? and redefine paintEvent ? – artoon Apr 16 '13 at 10:12
  • yes, that's also a way to go, only thing you should fix in your code, that graph.setParent(parent), should became to be graph.setParent(this) and in TimeGraph constructor, use QWidget(parent). so Widget on a form will be a parent for your widget and all Qwt controls will be a children of your control. Then you dont need a paint event at all – evilruff Apr 16 '13 at 10:20
0

Best way I would recommend is following - make a UI with everything you need on it except qwt control. For qwt control just reserve a space using Widget or Frame (on your taste). You can set all types of size constrains you need). You can check it with from preview. Set a Widget/Frame name to something you like lets say m_fmQwtPlotContainer. (that you can do in QtDesigner - objectName). then you can write something like that:

QWidget * pParent = findChild<QWidget*>("m_fmQwtPlotContainer"); 
QVBoxLayout * pLayout = new QVBoxLayout(pParent); 
pParent->setLayout( pLayout ); 
..then your code using pParent as a parent for your widget..
TimeGraph * pGraph = new TimeGraph( pParent );
pLayout->insertItem( pGraph );    

so you will have your form with TimeGraph inserted in a place you like

evilruff
  • 3,947
  • 1
  • 15
  • 27
  • Ok, I have my TimeGraph in my Widget. Now, my class TimeGraph must inherit of QWidget ? and redefine paintEvent ? – artoon Apr 16 '13 at 10:18
0

I have test two methods of my class TimeGraph

class TimeGraph : public QWidget { ... }

and

TimeGraph::TimeGraph(int in_display_time, QwtText title) : QWidget() { display_time = in_display_time;

graph.setParent(this);
//graph.setMaximumSize(parent->width() - 20, parent->height() - 20);
graph.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

... }

with :

ui->widget_graph_O2 = new TimeGraph(120);

The graph isn't displayed because it hasn't size.

And if I test the second method

class TimeGraph {...}

and

TimeGraph::TimeGraph(QWidget* parent, int in_display_time, QwtText title) { display_time = in_display_time;

graph.setParent(parent);
//graph.setMaximumSize(parent->width() - 20, parent->height() - 20);
graph.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); ... }

with

graph_O2 = new TimeGraph(ui->widget_graph_four, 120);

The graph isn't resized with the frame.

What did I forget?

artoon
  • 729
  • 2
  • 14
  • 41
  • use method 2) change graph.setParent(parent) -> graph.setParent(this) and add QWidget(parent) after constructor.. – evilruff Apr 16 '13 at 12:31
  • But I have to set a size and policySize on my TimeGraph and on my widget of the ui? Because when I use the second method with your modifications nothing display. However if I don't change graph.setParent(parent) my graph is displayed – artoon Apr 16 '13 at 12:57
  • but how you add your widget to a form? – evilruff Apr 16 '13 at 13:08
  • With the designer where I want. – artoon Apr 16 '13 at 13:12
  • This widget have a QVBoxLayout. I tried insertWidget(), addWidget() but nothing change. I havn't manage with insertItem because it must have in parameter a QLayoutItem. – artoon Apr 16 '13 at 14:18
  • you should use addWidget(), also don't forget to call show() on your widget.. btw it's not that good idea to use value based Qwt components.. – evilruff Apr 16 '13 at 15:38
  • Ok, thank you for yours answers. I resolved my problem doing inherited TimeGraph of qwtplot. Thanks – artoon Apr 19 '13 at 12:24