Draw the ant line in Qt and the ant line in Qt

Source: Internet
Author: User

Draw the ant line in Qt and the ant line in Qt
Abstract

If you have used a PS selection tool, you should know what the ant line is. It is a dotted line used to represent the selection. The key is dynamic!

A self-contained example in Qt has a variety of Trace demonstrations, but the final result can only be a static trace, which is not cool enough. So, you should implement the following by yourself.

Let's take a look at the final result:


It's just static. The final effect is exactly the same as that of the selection tool in PS.


Analysis

Input

One QRect

Output

Dynamic black and white ant Line tracing. (Black and white, not black and transparent)

Solution

Create a canvas-sized QImage, locate the edge pixels, and fill the pixels one by one. Finally, use painter. drawImage to draw the Image at the top.

You can set a timer for the animation effect through QTimer and repaint it continuously.


Code Implementation

Declare some member variables in the header file

int borderOffset;bool isBlackStart;QTimer *repaintTimer;

Timer Initialization

repaintTimer = new QTimer();repaintTimer->setInterval(380);repaintTimer->start();connect(repaintTimer, SIGNAL(timeout()), this, SLOT(updateSelectionBorder()));

Corresponding slot Functions

void Canvas::updateSelectionBorder(){borderOffset++;if (borderOffset > 4){borderOffset = 0;isBlackStart = !isBlackStart;}this->repaint();}


The most important border Function

void Canvas::paintSelectionBorder(QPainter &painter){int startX = selectionRect.startPoint().x();int startY = selectionRect.startPoint().y();//Init a transparent QImage.QSize scaledSize = m_scaleFactor * m_image.size();QImage transparentImage(scaledSize, QImage::Format_ARGB32);QColor transparent(0, 0, 0, 0);QColor black(0, 0, 0);QColor white(255, 255, 255);for (int i = 0; i < scaledSize.width(); i++)for (int j = 0; j < scaledSize.height(); j++){transparentImage.setPixel(i, j, transparent.rgba());}bool isDrawBlack = true;//Draw left&right border.for (int i = 0; i < selectionRect.height(); i++){if (i <=  borderOffset){transparentImage.setPixel(startX + selectionRect.width(), startY + i, isBlackStart ? black.rgb() : white.rgb());transparentImage.setPixel(startX, startY + i, isBlackStart ? black.rgb() : white.rgb());isDrawBlack = !isBlackStart;}else {transparentImage.setPixel(startX + selectionRect.width(), startY + i, \isDrawBlack ? black.rgb() : white.rgb());transparentImage.setPixel(startX, startY + i, \isDrawBlack ? black.rgb() : white.rgb());if ((i - borderOffset) % 5 == 0){isDrawBlack = !isDrawBlack;}}}//Draw top&bottom border;for (int i = 0; i < selectionRect.width(); i++){if (i <= borderOffset){transparentImage.setPixel(startX+i, startY, isBlackStart ? black.rgb() : white.rgb());transparentImage.setPixel(startX + i, startY + selectionRect.height(), isBlackStart ? black.rgb() : white.rgb());isDrawBlack = !isBlackStart;}else{transparentImage.setPixel(startX + i, startY, \isDrawBlack ? black.rgb() : white.rgb());transparentImage.setPixel(startX + i, startY + selectionRect.height(), \isDrawBlack ? black.rgb() : white.rgb());if ((i - borderOffset) % 5 == 0){isDrawBlack = !isDrawBlack;}}}painter.drawImage(0, 0, transparentImage);}


Close the job.


Thank you for your advice!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.