自己写的一个小小的电子罗盘的一个小程序,不过是项目的一部分,只可以贴绘制部分代码
效果如下图
首先开始自己写的时候,虽然知道Qt 的坐标系是从左上角开始的,所以,使用了算法,在绘制后,在移动回来,但是只有45度的时候可以实现,其它角度都是有问题的。
和前辈请教,先移动坐标在绘制,在使用过程中,忘记调用
painter1.save();
函数,到时失败,仔细检查是才发现少些了一行。
上核心代码
void MainWidget::draw_Direction()
{
QPainter painter1(ui->label_Direction);
painter1.setRenderHint(QPainter::Antialiasing,true);
painter1.setPen(QPen(QColor(Qt::blue),1.5,Qt::SolidLine,Qt::FlatCap)); /*
* move point
QTransform t_y;
t_y.rotate(-90,Qt::YAxis);
painter1.setTransform(t_y);
int temp =30;
QTransform t_z;
t_z.rotate(temp,Qt::ZAxis);
painter1.setTransform(t_z);
painter1.translate(0+135*qCos(30*PI/180.0),0-135*qSin(30*PI/180.0));
painter1.translate(0-39+96,0-39-96); painter1.translate((0-(((135/(qCos(45*PI/180.0)))*qSin((temp+45)*PI/180.0)-135)*(qSin(temp*PI/180.0)))+(135-((135/(qCos(45*PI/180.0)))*qCos((temp+45)*PI/180.0)))),((0-(((135/(qCos(45*PI/180.0)))*qSin((temp+45)*PI/180.0)-135)*(qCos(temp*PI/180.0))))-(135-((135/(qCos(45*PI/180.0)))*qSin((temp+45)*PI/180.0))))); painter1.translate(0-((TEMP_R*qSin((temp+45)*PI/180.0))-135)*qCos((temp)*PI/180.0)+(TEMP_R*qCos((temp)*PI/180.0))* qCos((temp)*PI/180.0),0-((TEMP_R*qSin((temp+45)*PI/180.0))-135)*qSin((temp)*PI/180.0)-(TEMP_R*qCos((temp)*PI/180.0))* qSin((temp)*PI/180.0)); qDebug()<<(TEMP_R*qCos((temp)*PI/180.0))* qSin((temp)*PI/180.0);
*/ painter1.translate(135,135);
painter1.save();
painter1.rotate(num_Angle); for(int i = 0;i<360;)
{
if((i%90) == 0)
{
painter1.drawLine(0+(95*qSin(i*PI/180.0)),0-(95*qCos(i*PI/180.0)),0+(100*qSin(i*PI/180.0)),0-(100*qCos(i*PI/180.0)));
i=i+30;
}
painter1.drawLine(0+(95*qSin(i*PI/180.0)),0-(95*qCos(i*PI/180.0)),0+(110*qSin(i*PI/180.0)),0-(110*qCos(i*PI/180.0)));
i=i+30; }
for(int j = 0;j<360;)
{
if((j%30) == 0 || (j%90) == 0)
{
j= j+10;
}
painter1.drawLine(0+(100*qSin(j*PI/180.0)),0-(100*qCos(j*PI/180.0)),0+(110*qSin(j*PI/180.0)),0-(110*qCos(j*PI/180.0)));
j=j+10;
}
painter1.setPen(QPen(QColor(Qt::blue),3,Qt::SolidLine,Qt::FlatCap));
QRect rect1(-7,-113,15,15);
QRect rect1_1(-7,-95,15,15);
QRect rect2(-112,-9,15,15);
QRect rect3(-7,103,15,15);
QRect rect4(100,-9,15,15);
QString str_N = "N";
QString str_S = "S";
QString str_W = "W";
QString str_E = "E";
QFont font("宋体",16,QFont::Bold);
painter1.setFont(font);
painter1.drawText(rect1,Qt::AlignCenter,str_N);
painter1.drawText(rect2,Qt::AlignCenter,str_W);
painter1.drawText(rect3,Qt::AlignCenter,str_S);
painter1.drawText(rect4,Qt::AlignCenter,str_E);
QPixmap pixmap_pointer;
pixmap_pointer.load(":/Global/Source/Global/Pointer.png");
painter1.drawPixmap(rect1_1,pixmap_pointer);
painter1.restore();
QRect rect_pixmap(-15,-15,30,30);
QPixmap pixmap_plane;
pixmap_plane.load(":/Global/Source/Global/Plane.png");
painter1.drawPixmap(rect_pixmap,pixmap_plane);
}
至于好多人不知道怎么在Lable上面绘图,可以参考我以前写的