Вызов функции С++ из Qml, Qt
Пишу в Qt5 на qml + c++.
Интересует решение проблемы: "Не могу вызвать правильно функцию отрисовки (рисует несколько вложенных окружностей в зависимости от параметров) по кнопке, а так же удалить изображение по другой кнопке." Одна из второстепенных проблем: не знаю как передавать указатель на rectangle (на форме несколько rectangle, рисовать надо только в одном из них).
Код класса на С++
Код:
#ifndef CIRCLE1_H
#define CIRCLE1_H
#include <QObject>
#include <QPainter>
#include <QtQuick>
class circle1 : public QObject
{
Q_OBJECT
Q_PROPERTY(int radius READ radius WRITE setRadius NOTIFY RadiusChanged)
Q_PROPERTY(int wavelength READ wavelength WRITE setWavelength NOTIFY WavelengthChanged)
Q_PROPERTY(int distance_to_the_screen READ distance_to_the_screen WRITE setDistance_to_the_screen NOTIFY Distance_to_the_screenChanged)
Q_PROPERTY(int distance_to_the_screen2 READ distance_to_the_screen2 WRITE setDistance_to_the_screen2 NOTIFY Distance_to_the_screen2Changed)
private:
int _wavelength;
int _radius;
int _distance_to_the_screen;
int _distance_to_the_screen2;
public:
explicit circle1(QObject *parent = 0);
// //Запуск класса
// void start(int &a,int &b,int &c,int &d);
//Вывод изображения
Q_INVOKABLE void draw(QPainter *painter);
//Get-функции
int radius();
int wavelength();
int distance_to_the_screen();
int distance_to_the_screen2();
//Set-функции
void setWavelength(int &a);
void setRadius(int &a);
void setDistance_to_the_screen(int &a);
void setDistance_to_the_screen2(int &a);
signals:
//Сигналы для Set-функций
void RadiusChanged();
void WavelengthChanged();
void Distance_to_the_screenChanged();
void Distance_to_the_screen2Changed();
void PaintChanged();
public slots:
};
#endif // CIRCLE1_H
#define CIRCLE1_H
#include <QObject>
#include <QPainter>
#include <QtQuick>
class circle1 : public QObject
{
Q_OBJECT
Q_PROPERTY(int radius READ radius WRITE setRadius NOTIFY RadiusChanged)
Q_PROPERTY(int wavelength READ wavelength WRITE setWavelength NOTIFY WavelengthChanged)
Q_PROPERTY(int distance_to_the_screen READ distance_to_the_screen WRITE setDistance_to_the_screen NOTIFY Distance_to_the_screenChanged)
Q_PROPERTY(int distance_to_the_screen2 READ distance_to_the_screen2 WRITE setDistance_to_the_screen2 NOTIFY Distance_to_the_screen2Changed)
private:
int _wavelength;
int _radius;
int _distance_to_the_screen;
int _distance_to_the_screen2;
public:
explicit circle1(QObject *parent = 0);
// //Запуск класса
// void start(int &a,int &b,int &c,int &d);
//Вывод изображения
Q_INVOKABLE void draw(QPainter *painter);
//Get-функции
int radius();
int wavelength();
int distance_to_the_screen();
int distance_to_the_screen2();
//Set-функции
void setWavelength(int &a);
void setRadius(int &a);
void setDistance_to_the_screen(int &a);
void setDistance_to_the_screen2(int &a);
signals:
//Сигналы для Set-функций
void RadiusChanged();
void WavelengthChanged();
void Distance_to_the_screenChanged();
void Distance_to_the_screen2Changed();
void PaintChanged();
public slots:
};
#endif // CIRCLE1_H
Код:
void circle1::draw(QPainter *painter)
{
painter->save();
double m;
QPen pen = QPen(Qt::NoPen);
QBrush black_brush = QBrush(Qt::black);
QBrush red_brush = QBrush(Qt::red);
painter->setPen(pen);
m=100000*(_radius*_radius)/_wavelength;
m=m*(1/_distance_to_the_screen+1/(_distance_to_the_screen2-_distance_to_the_screen));
int k=int(m);
if(k!=0)
{
m=120/k;
for(int i=1;i<=k;i++)
{
if(i%2==1)
{
painter->setBrush(red_brush);
painter->drawEllipse(2+m*(i-1),2+m*(i-1),210-m*(i-1)*2,210-m*(i-1)*2);
}
else
{
painter->setBrush(black_brush);
painter->drawEllipse(2+m*(i-1),2+m*(i-1),210-m*(i-1)*2,210-m*(i-1)*2);
}
}
}
else
{
int i;
painter->setBrush(red_brush);
painter->drawEllipse(2+m*(i-1),2+m*(i-1),210-m*(i-1)*2,210-m*(i-1)*2);
}
painter->restore();
emit PaintChanged();
}
{
painter->save();
double m;
QPen pen = QPen(Qt::NoPen);
QBrush black_brush = QBrush(Qt::black);
QBrush red_brush = QBrush(Qt::red);
painter->setPen(pen);
m=100000*(_radius*_radius)/_wavelength;
m=m*(1/_distance_to_the_screen+1/(_distance_to_the_screen2-_distance_to_the_screen));
int k=int(m);
if(k!=0)
{
m=120/k;
for(int i=1;i<=k;i++)
{
if(i%2==1)
{
painter->setBrush(red_brush);
painter->drawEllipse(2+m*(i-1),2+m*(i-1),210-m*(i-1)*2,210-m*(i-1)*2);
}
else
{
painter->setBrush(black_brush);
painter->drawEllipse(2+m*(i-1),2+m*(i-1),210-m*(i-1)*2,210-m*(i-1)*2);
}
}
}
else
{
int i;
painter->setBrush(red_brush);
painter->drawEllipse(2+m*(i-1),2+m*(i-1),210-m*(i-1)*2,210-m*(i-1)*2);
}
painter->restore();
emit PaintChanged();
}
Код:
...
Rectangle{
id: for_start;
x: 600
anchors.top: rectangle1.bottom
anchors.topMargin: 11
anchors.right: parent.right
anchors.rightMargin: 424
Button{
id:start
text:"Start"
onClicked: {
Circle1.radius = slider2.value
Circle1.wavelength = slider1.value
Circle1.distance_to_the_screen = slider3.value
Circle1.distance_to_the_screen2 = slider4.value
Circle1.draw(rectangle2)
}
}
}
...
Rectangle{
id: for_start;
x: 600
anchors.top: rectangle1.bottom
anchors.topMargin: 11
anchors.right: parent.right
anchors.rightMargin: 424
Button{
id:start
text:"Start"
onClicked: {
Circle1.radius = slider2.value
Circle1.wavelength = slider1.value
Circle1.distance_to_the_screen = slider3.value
Circle1.distance_to_the_screen2 = slider4.value
Circle1.draw(rectangle2)
}
}
}
...
Проблема решена!