Description: The project met recently to do a UI design problem is the background display, take this opportunity to record it
demand:
1, qwt control to set the compass background
2, qwt removal of border controls
3, to ensure qwt painting does not affect the trajectory of the compass
Ideas:
1, qwt added canvas redefined drawing event (Event Monitor use) on the canvas. Ideas and failed! ! !
2, qwt add canvas, set the background image on the canvas. Ideas and failed! ! !
3, directly qwt background, canceled canvas (canvas covering qwt background). Success thinking results! ! !
Try 1 (1 idea): redefine the drawing event on canvas
(1) event handler
// --------- drawing compass navigation --------- // BOOL eventFilter (Watched QObject *, E * QEvent); // Drawing
bool Display_Navigator::eventFilter(QObject *watched, QEvent *e) { if (watched == ui->qwtPlot->canvas() ) { if (e->type() == QEvent::Paint)//QEvent::Paint) { paintOnWidget(ui->qwtPlot->canvas() ); qDebug()<<"eventFilter paint!"; return true; } } return QWidget::eventFilter(watched,e); //将事件传递给父类 }
(2) Draw redraw
void Display_Navigator :: paintOnWidget (the QWidget * W) / * * virtual function: refers to a member function you wish to override * (1) repaint () function or update () function is called; * (2) is now hidden member redisplayed; * / { QPainter Painter ( the this ); QFont font; font.setPointSize ( . 7 ); // set the font size 10 setFont (font); / * open Antialiasing * / painter.setRenderHint (QPainter :: Antialiasing, to true ); painter.setPen (the Qt :: White); painter.drawEllipse (QPointF ( 518 , 314 ), 255 , 255 );//画圆 //画刻度 QPen &pen = const_cast<QPen&>(painter.pen()); pen.setStyle(Qt::SolidLine); for(int i = 0 ;i <= 360;i+= 5) { if((i%5==0)&&(i%90!=0)) { QPointF ptStart = trans(i); QPointF ptEnd = trans(i,10); QPen pen; pen.setColor(QColor(255,255,0)); painter.setPen(pen); //旋转指定的角度 painter.rotate(i%5); painter.drawLine(ptStart, ptEnd); painter.save(); painter.restore(); } if((i%10==0)&&(i%90!=0)) { QPointF ptStart = trans(i); QPointF ptEnd = trans(i,20); QPen pen; pen.setColor(QColor(255,255,0)); painter.setPen(pen); painter.rotate(i%10); painter.drawLine(ptStart, ptEnd); painter.save(); painter.restore(); } if(i%90==0) { QPointF ptStart = trans(i); QPointF ptEnd = trans(i,20); QPen pen; pen.setColor(QColor(255,0,0)); painter.setPen(pen); painter.rotate(i%10); painter.drawLine(ptStart, ptEnd); painter.save(); painter.restore(); } } for(int i = 30 ;i <= 360;i+= 30) { QPen pen; pen.setColor(QColor(255,255,255)); painter.setPen(pen); QPointF pt1= trans2(i); painter.drawLine(QPointF(518,314), pt1); Pt QPointF= ));Trans (I); painter.save (); // offset coordinate origin in the need to draw the tick painter.translate (Pt); // rotation angle specified painter.rotate (I); // Videos scale painter.drawText (- 30 , - 32 , 55 , 20 is , the Qt :: AlignCenter, QString :: Number (I)); painter.restore (); IF (I == 90 ) { QPen PEN; pen.setColor (a QColor ( 255 , 255 , 255 painter.setPen (PEN); PT1 QPointF = a Trans2 (I); painter.drawLine (QPointF ( 518 , 314 ), PT1); QPointF Pt = Trans (I); painter.save (); // offset coordinate origin in the need to draw the tick painter. Translate (Pt); // rotation angle specified painter.rotate (I); pen.setColor (a QColor ( 0 , 255 , 0 )); painter.setPen (PEN); QFont font; font.setPointSize ( 14 ); painter.setFont(font); painter.drawText(-20,-5,60,30,Qt::AlignCenter, QString::fromUtf8( "E" )); painter.restore(); } if(i == 180) { QPen pen; pen.setColor(QColor(255,255,255)); painter.setPen(pen); QPointF pt1= trans2(i); painter.drawLine (QPointF ( 518 ,314 ), PT1); QPointF Pt = Trans (I); painter.save (); // offset needs to be drawn to scale coordinates of the origin position painter.translate (Pt); // rotation designated angle painter.rotate (I); pen.setColor (a QColor ( 0 , 255 , 0 )); painter.setPen (PEN); QFont font; font.setPointSize ( 14 ); painter.setFont (font); painter.drawText ( - 20 , - . 5 , 60 ,30 , the Qt :: AlignCenter, QString :: fromLatin1 ( " S " )); painter.restore (); } IF (I == 270 ) { QPen PEN; pen.setColor (a QColor ( 255 , 255 , 255 )); painter.setPen (PEN); QPointF PT1 = a Trans2 (I); painter.drawLine (QPointF ( 518 , 314 ), PT1); QPointF Pt =trans(i); painter.save (); // offset coordinate origin in the need to draw the tick painter.translate (Pt); // rotation angle specified painter.rotate (I); pen.setColor (a QColor ( 0 , 255 , 0 ) ); painter.setPen (PEN); QFont font; font.setPointSize ( 14 ); painter.setFont (font); painter.drawText ( - 20 is , - . 5 , 60 , 30 , the Qt :: AlignCenter, QString :: fromLatin1 ( " W ")); Painter.restore (); } IF (I == 360 ) { QPen PEN; pen.setColor (a QColor ( 255 , 255 , 255 )); painter.setPen (PEN); QPointF PT1 = a Trans2 (I); painter.drawLine (QPointF ( 518 , 314 ), PT1); QPointF Pt = Trans (I); // Sets the current font painter.save (); // offset needs to be drawn to scale coordinates of the origin position painter.translate (Pt); // rotation angle specified painter.rotate(i); pen.setColor(QColor(0,255,0)); painter.setPen(pen); QFont font; font.setPointSize(14); painter.setFont(font); painter.drawText(-20,-5,60,30,Qt::AlignCenter, QString::fromLatin1( "N")); painter.restore(); } } }
(3) obtaining an angle corresponding to the arc point
#define DegreeToArc (x) x * M_PI / 180.0 / * degrees to radians. * /
QPointF Display_Navigator::trans(int angle) { QPointF ptResult(QPointF(518,314) ); //[角度转弧度] float rad = DegreeToArc(static_cast<float>(angle)); ptResult.setX(518 + (255) * qSin(static_cast<qreal>(rad))); ptResult.setY(314 - (255) * qCos(static_cast<qreal>(rad))); return ptResult; } QPointF Display_Navigator::trans(int angle,float len) { QPointF ptResult(QPointF(518,314) ); //[角度转弧度] float rad = DegreeToArc(static_cast<float>(angle)); ptResult.setX(518 + (255+len) * qSin(static_cast<qreal>(rad))); ptResult.setY(314 - (255+len) * qCos(static_cast<qreal>(rad))); return ptResult; } QPointF Display_Navigator::trans2(int angle) { QPointF ptResult(QPointF(518,314)); //[角度转弧度] float rad = DegreeToArc(static_cast<float>(angle)); ptResult.setX(518 +(255-30 )* qSin(static_cast<qreal>(rad))); ptResult.setY(314 -(255-30) * qCos(static_cast<qreal>(rad))); return ptResult; }
(3) Installation Event
// ---------- Drawing Event Manager ---------- // ui-> qwtPlot-> Canvas () -> installEventFilter ( the this );
(4) Effects:
① did not add graphics events
② Add an event drawing
Analysis: You can add trigger event drawing on the canvas, but will cover the curve, covering Shan grid lines will not be displayed curve. Failed to set the background on the canvas! ! !
Try 2 (idea 3): Set qwt background
① Gets qwt actual size
void Display_Navigator :: getQwtplotSize () { qwtplotSize = ui-> qwtPlot-> size (); } // only control to display (Show ()) after acquiring the actual size, it can not be acquired in the initialization size
② add a background image for qwt
// add background QPixmap = QPixmap a pixmap ( " : /image/Image/compass.png " ) .scaled (qwtplotSize); // .scaled (ui-> qwtPlot_2-> size ()); QPalette Palette (ui-> qwtPlot -> Palette ()); palette.setBrush (QPalette :: Background, QBrush (a pixmap)); qDebug () << " qwtplot.size = " << ui-> qwtPlot-> size (); UI -> qwtPlot- > setPalette (Palette); UI -> qwtPlot-> setAutoFillBackground ( to true ); // set the form automatically filled bACKGROUND
Renderings:
Map there will be border.
③ removed border drawing control qwt
ui-> qwtPlot_2-> setStyleSheet (QString :: fromUtf8 ( " border: none; " )); // set the border-free
Set this statement must set the form automatically fill the background, or the background image is not loaded.
Finally, FIG molding effect:
--- end --- restore content