PDA

View Full Version : problem with QwtPlotCanvas::paintEvent



MickyJhon
31st January 2014, 06:59
hello all,
i want to paint some thing (in this example , i do nothing) to a QwtPlotCanvas, so i subclass MyCanvas, in class MyCanvas constructor function , i configure object and implement paintEvent, However, the displaying QwtPlotCanvas is not correct as my except . 9996

my code is listed as following.

in mycanvas.h

#ifndef MYCANVAS_H
#define MYCANVAS_H
#include <qwt_plot_canvas.h>
#include<QPainter>

class MyCanvas:public QwtPlotCanvas
{
public:
MyCanvas();
void paintEvent(QPaintEvent *event);
};
#endif // MYCANVAS_H
in mycanvas.cpp

#include "mycanvas.h"

MyCanvas::MyCanvas()
{
this->setBorderRadius( 15 );
}
void MyCanvas::paintEvent(QPaintEvent *event)
{
}
in mainwindow.h

#define MAINWINDOW_H
#include <QMainWindow>
#include"mycanvas.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MyCanvas * canvas;
};
#endif // MAINWINDOW_H

in mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwt_plot_canvas.h>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
canvas(new MyCanvas)
{
ui->setupUi(this);
ui->qwtPlot->setCanvas(canvas);
}
MainWindow::~MainWindow()
{
delete ui;
}

any suggestions?

Uwe
31st January 2014, 07:37
Your code has to be:


void MyCanvas::paintEvent(QPaintEvent *event)
{
QwtPlotCanvas::paintEvent( event );

// now do your stuff
}

But I'm very sure that you are trying to do something the wrong way. In almost 100% of the use cases it is better to derive a new type of QwtPlotItem that can be attached to the plot scene.

Uwe

MickyJhon
31st January 2014, 09:38
Thank you very much, Uwe, Your advice is very useful . ;)


Your code has to be:


void MyCanvas::paintEvent(QPaintEvent *event)
{
QwtPlotCanvas::paintEvent( event );

// now do your stuff
}

But I'm very sure that you are trying to do something the wrong way. In almost 100% of the use cases it is better to derive a new type of QwtPlotItem that can be attached to the plot scene.

Uwe