Hello!
I am trying to paint a simple plot (sin) on a screen.
Maybe my method is a bit wierd I don't know, this is my first paint-expirience.
Hower, I encountered a problem.
Here is my code:
.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QPainter>
{
Q_OBJECT
const double pi = 3.14159;
public:
~Widget();
private:
};
#endif // WIDGET_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QPainter>
class Widget : public QWidget
{
Q_OBJECT
const double pi = 3.14159;
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QLabel *Hi;
QPainter *paint;
virtual void paintEvent(QPaintEvent * );
};
#endif // WIDGET_H
To copy to clipboard, switch view to plain text mode
.cpp
#include "widget.h"
#include <QWidget>
#include <cmath>
{
this->setGeometry(100,100,800,500);
}
Widget::~Widget()
{
}
{
paint
-> setPen
(QPen(Qt
::red));
for(int i=0;i<1000;i++)
{
qreal x=sin(pi/6+i);
qreal y=sin(pi/6+i);
}
}
#include "widget.h"
#include <QWidget>
#include <cmath>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setGeometry(100,100,800,500);
paint = new QPainter(this);
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent * )
{
paint-> setPen(QPen(Qt::red));
for(int i=0;i<1000;i++)
{
qreal x=sin(pi/6+i);
qreal y=sin(pi/6+i);
paint->drawPoint(QPointF(x,y));
}
}
To copy to clipboard, switch view to plain text mode
At first, I just tried to paint in .cpp without void Widget::paintEvent(QPaintEvent * ) like this:
#include "widget.h"
#include <QWidget>
#include <cmath>
{
this->setGeometry(100,100,800,500);
paint
-> setPen
(QPen(Qt
::red));
for(int i=0;i<1000;i++)
{
qreal x=sin(pi/6+i);
qreal y=sin(pi/6+i);
}
}
Widget::~Widget()
{
}
#include "widget.h"
#include <QWidget>
#include <cmath>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setGeometry(100,100,800,500);
paint = new QPainter(this);
paint-> setPen(QPen(Qt::red));
for(int i=0;i<1000;i++)
{
qreal x=sin(pi/6+i);
qreal y=sin(pi/6+i);
paint->drawPoint(QPointF(x,y));
}
}
Widget::~Widget()
{
}
To copy to clipboard, switch view to plain text mode
It didn't work. Then I looked throught the Internet and found out that one can paint only in void Widget::paintEvent(QPaintEvent * ), and besides this method must be virtual. I have changed this, it didn't help either.
then I found that paint(this) should be changed to this paint.begin(viewport());
done and viewport() "no matching fucntion to call"
What seems to be the problem here? and could someone help me to fix that?
Thank you in advance!
Bookmarks