About Qt drawLine Questions
Solved
General and Desktop
-
I draw adjacent lines on Qt 5.5.1 , found it is very strange., please refer to the bmp, Line1 and Line2, Line3 used the userdefine function , Line4 is used drawLine.I want to know why Qt drawn adjacent line will be like this? I can make it as normal as Line1 and Line2, Line3?
My function is as follows:drawLineDDA(painter,110, 120.0, 111, 200.0 ); drawLineDDA(painter,111, 200.0, 112, 110.0 ); drawLineBresenham(painter,114, 120.0, 115, 200.0 ); drawLineBresenham(painter,115, 200.0, 116, 110.0 ); drawLineMiddle(painter,118, 120.0, 119, 200.0 ); drawLineMiddle(painter,119, 200.0, 120, 110.0 ); painter.drawLine(122, 120.0, 123, 200.0); painter.drawLine(123, 200.0, 124, 110.0 ); void Widget::drawLineDDA(QPainter &painter,int x0, int y0, int xEnd, int yEnd) { int dx=xEnd-x0,dy=yEnd-y0,steps,k; float xIncrement,yIncrement,x=x0,y=y0; if(fabs(dx)>=fabs(dy)) steps=fabs(dx); else steps=fabs(dy); xIncrement=float(dx)/float(steps); yIncrement=float(dy)/float(steps); painter.drawPoint(round(x),round(y)); for(k=0;k<steps;k++){ x+=xIncrement; y+=yIncrement; painter.drawPoint(round(x),round(y)); } } void Widget::drawLineBresenham(QPainter &painter, int x1,int y1,int x2, int y2) { int dx = x2 - x1; int dy = y2 - y1; int ux = ((dx > 0) << 1) - 1; int uy = ((dy > 0) << 1) - 1; int x = x1, y = y1, eps; eps = 0;dx = abs(dx); dy = abs(dy); if (dx > dy) { for (x = x1; x != x2; x += ux) { painter.drawPoint(x,y); eps += dy; if ((eps << 1) >= dx) { y += uy; eps -= dx; } } } else { for (y = y1; y != y2; y += uy) { painter.drawPoint(x,y); eps += dx; if ((eps << 1) >= dy) { x += ux; eps -= dy; } } } } void Widget::drawLineMiddle(QPainter &painter, int x0, int y0, int x1, int y1) { int a,b,d1,d2,d,x,y;float m; if (x1<x0){d=x0,x0=x1,x1=d;d=y0,y0=y1,y1=d;} a=y0-y1,b=x1-x0;if (b==0) m=-1*a*100; else m=(float)a/(x0-x1);x=x0,y=y0; painter.drawPoint(x,y); if (m>=0 && m<=1) {d=2*a+b;d1=2*a,d2=2*(a+b); while (x<x1) { if (d<=0) { x++,y++,d+=d2;} else { x++,d+=d1; } painter.drawPoint(x,y); }} else if (m<=0 && m>=-1) {d=2*a-b;d1=2*a-2*b,d2=2*a; while (x<x1) { if (d>0) { x++,y--,d+=d1;} else { x++,d+=d2; } painter.drawPoint(x,y); } } else if (m>1) {d=a+2*b;d1=2*(a+b),d2=2*b; while (y<y1) { if (d>0) { x++,y++,d+=d1;} else { y++,d+=d2; } painter.drawPoint(x,y); } } else {d=a-2*b;d1=-2*b,d2=2*(a-b); while (y>y1) { if (d<=0) { x++,y--,d+=d2;} else { y--,d+=d1; } painter.drawPoint(x,y); }} }
-
Hi and welcome to devnet,
You should propose a minimal compile example showing your problem ?
-
1,Create a new project with Qt Widgets Application. Class name: Widget; Base Class: QWidget; Generate Form: not choose
2,use the code in widget.cpp:#include "widget.h" #include<QPainter> #include<QtMath> #include <QLabel> Widget::Widget(QWidget *parent) : QWidget(parent) { setGeometry(200, 200, 600, 400); QLabel *label1 = new QLabel(this); label1->setText("drawLineDDA\nLine1"); label1->move(100,10); QLabel *label2 = new QLabel(this); label2->setText("drawLineBresenham\nLine2"); label2->move(220,10); QLabel *label3 = new QLabel(this); label3->setText("drawLineMiddle\nLine3"); label3->move(340,10); QLabel *label4 = new QLabel(this); label4->setText("Qt drawLine\nLine4"); label4->move(460,10); } Widget::~Widget() { } void Widget::paintEvent(QPaintEvent *) { QPainter painter(this); drawLineDDA(painter,110, 60.0, 111, 120.0 ); drawLineDDA(painter,111, 120.0, 112, 50.0 ); drawLineBresenham(painter,220, 60.0, 221, 120.0 ); drawLineBresenham(painter,221, 120.0, 222, 50.0 ); drawLineMiddle(painter,340, 60.0, 341, 120.0 ); drawLineMiddle(painter,341, 120.0, 342, 50.0 ); painter.drawLine(460, 60.0, 461, 120.0); painter.drawLine(461, 120.0, 462, 50.0 ); } void Widget::drawLineDDA(QPainter &painter,int x0, int y0, int xEnd, int yEnd) { int dx=xEnd-x0,dy=yEnd-y0,steps,k; float xIncrement,yIncrement,x=x0,y=y0; if(fabs(dx)>=fabs(dy)) steps=fabs(dx); else steps=fabs(dy); xIncrement=float(dx)/float(steps); yIncrement=float(dy)/float(steps); painter.drawPoint(round(x),round(y)); for(k=0;k<steps;k++){ x+=xIncrement; y+=yIncrement; painter.drawPoint(round(x),round(y)); } } void Widget::drawLineBresenham(QPainter &painter, int x1,int y1,int x2, int y2) { int dx = x2 - x1; int dy = y2 - y1; int ux = ((dx > 0) << 1) - 1; int uy = ((dy > 0) << 1) - 1; int x = x1, y = y1, eps; eps = 0;dx = abs(dx); dy = abs(dy); if (dx > dy) { for (x = x1; x != x2; x += ux) { painter.drawPoint(x,y); eps += dy; if ((eps << 1) >= dx) { y += uy; eps -= dx; } } } else { for (y = y1; y != y2; y += uy) { painter.drawPoint(x,y); eps += dx; if ((eps << 1) >= dy) { x += ux; eps -= dy; } } } } void Widget::drawLineMiddle(QPainter &painter, int x0, int y0, int x1, int y1) { int a,b,d1,d2,d,x,y;float m; if (x1<x0){d=x0,x0=x1,x1=d;d=y0,y0=y1,y1=d;} a=y0-y1,b=x1-x0;if (b==0) m=-1*a*100; else m=(float)a/(x0-x1);x=x0,y=y0; painter.drawPoint(x,y); if (m>=0 && m<=1) {d=2*a+b;d1=2*a,d2=2*(a+b); while (x<x1) { if (d<=0) { x++,y++,d+=d2;} else { x++,d+=d1; } painter.drawPoint(x,y); }} else if (m<=0 && m>=-1) {d=2*a-b;d1=2*a-2*b,d2=2*a; while (x<x1) { if (d>0) { x++,y--,d+=d1;} else { x++,d+=d2; } painter.drawPoint(x,y); } } else if (m>1) {d=a+2*b;d1=2*(a+b),d2=2*b; while (y<y1) { if (d>0) { x++,y++,d+=d1;} else { y++,d+=d2; } painter.drawPoint(x,y); } } else {d=a-2*b;d1=-2*b,d2=2*(a-b); while (y>y1) { if (d<=0) { x++,y--,d+=d2;} else { y--,d+=d1; } painter.drawPoint(x,y); }} }
3, widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget { Q_OBJECT void drawLineDDA(QPainter &painter, int x0, int y0, int xEnd, int yEnd); void drawLineBresenham(QPainter &painter, int x1, int y1, int x2, int y2); void drawLineMiddle(QPainter &painter, int x0, int y0, int x1, int y1); public: Widget(QWidget *parent = 0); ~Widget(); protected: void paintEvent(QPaintEvent *); }; #endif // WIDGET_H
-
Add
painter.setRenderHint(QPainter::Qt4CompatiblePainting);
before your call todrawLine
.