Realization of window selection of pixels when QT draws vector diagram

Realization of window selection of pixels when QT draws vector diagram

I am writing a program to select objects. The scene is to use the mouse to pull out a transparent window, and then determine which objects are selected. The intuitive idea is to use fillRect and make the filled color transparent. Development environment: VS2010+QT5.5.1

ready

Add label control in Widget as drawing window. Write the drawing content in the paintEvent function of the label. A lot of programs have been written, and if they are not good, they will upload and share with you, and only post relevant parts of the code.

problem

When drawing a window with Painter's FillRect, the several filling styles provided by the brush are not satisfied. Part of the code is as follows:
QPen p(QColor(0,0,192,128));
p.setWidth(1.);
painter.setPen§;
QBrush b(QColor(0,128,128,128),Qt::SolidPattern); // SolidPattern is not transparent;
painter.setBrush (b);
painter.fillRect(aRect,Qt::SolidPattern); //Also available Qt::Dense7Pattern

Qt::SolidPattern is opaque, and Dense7Pattern is dot filling. Although you can see the pixels to be selected, it is not a universal method after all.

Solution

用drawPolygon代替fillRect。
QPen p(QColor(0,0,192,128));
p.setWidth(1.);
painter.setPen§;
QBrush b(QColor(0,128,128,128),Qt::SolidPattern); // SolidPattern 不透明;
painter.setBrush(b);
QPolygonF apg;
QPointF tmpP(aRect.x(),aRect.y());apg<<tmpP;
tmpP.setX(tmpP.x()+aRect.size().width());apg<<tmpP;
tmpP.setY(tmpP.y()+aRect.size().height());apg<<tmpP;
tmpP.setX(tmpP.x()-aRect).size().width());apg<<tmpP;
painter.drawPolygon(apg,Qt::WindingFill);
Cyan is the window pulled out with the mouse

to sum up

The fillRect and fillPolygon of QT5 use different filling mechanisms. The color transparency setting of pen and brush does not work for fillRect.

Guess you like

Origin blog.csdn.net/imz2y/article/details/101204652