您的位置:

如何使用QLinearGradient来创建漂亮的渐变效果

在图形设计当中,渐变效果一般指的是颜色的平滑过渡。QLinearGradient是Qt框架中提供的渐变类,可以用于填充封闭多边形、矩形和椭圆形,以及线性填充窗口区域或QPainterPath。使用QLinearGradient可以轻松地创建漂亮的渐变效果。

一、QLinearGradient的基本用法

使用QLinearGradient可以创建线性渐变,主要是根据坐标系中的起点和终点确定渐变的方向,从而创建渐变。对于线性渐变,可以控制渐变的起点和终点,通过这两个点控制渐变的方向和长度。

// 可以通过以下代码创建一个从左上角到右下角的线性渐变
QLinearGradient linearGradient(QPoint(0, 0), QPoint(400, 400));

如果想要水平方向的渐变效果,可以通过设置终点的x坐标大于起点的x坐标来实现。

// 可以通过以下代码创建一个水平方向的线性渐变
QLinearGradient linearGradient(QPoint(0, 200), QPoint(400, 200));

如果想要垂直方向的渐变效果,可以通过设置终点的y坐标大于起点的y坐标来实现。

// 可以通过以下代码创建一个垂直方向的线性渐变
QLinearGradient linearGradient(QPoint(200, 0), QPoint(200, 400));

在创建完QLinearGradient对象之后,可以调用它的setColorAt()函数为渐变设置颜色。

// 可以通过以下代码为渐变设置颜色
linearGradient.setColorAt(0.0, QColor(Qt::white));
linearGradient.setColorAt(1.0, QColor(Qt::black));

二、QLinearGradient的高级用法

QLinearGradient还提供了一些高级用法,主要是通过设置渐变的模式和扩散方式来扩展渐变的效果。

QGradient::Spread定义了渐变的扩散方式,它有以下几种扩散方式:

  • QGradient::Pad: 默认值,表示在渐变区域边缘处不做处理,超出的部分保持原有颜色
  • QGradient::Repeat: 对于超出渐变区域的部分,重复渐变中的颜色。
  • QGradient::Reflect: 对于超出渐变区域的部分,镜像反射渐变中的颜色。
// 可以通过以下代码设置渐变的扩散方式
linearGradient.setSpread(QGradient::Repeat);

QGradient::CoordMode定义了坐标系中渐变的模式,它有以下几种模式:

  • QGradient::LogicalMode: 默认值,表示以QPainter绘制的坐标系为参考
  • QGradient::StretchToDeviceMode: 表示以设备坐标系为参考
  • QGradient::ObjectBoundingMode: 表示以对象边界框为参考
// 可以通过以下代码设置渐变的模式
linearGradient.setCoordinateMode(QGradient::ObjectBoundingMode);

同时,我们还可以将多个渐变对象连接起来,从而实现更加丰富的效果。

// 可以通过以下代码连接多个渐变对象
QLinearGradient linearGradient1(QPoint(0, 0), QPoint(400, 400));
linearGradient1.setColorAt(0.0, QColor(Qt::white));
linearGradient1.setColorAt(1.0, QColor(Qt::black));

QLinearGradient linearGradient2(QPoint(0, 400), QPoint(400, 0));
linearGradient2.setColorAt(0.0, QColor(Qt::black));
linearGradient2.setColorAt(1.0, QColor(Qt::white));

QLinearGradient linearGradient3(QPoint(200, 0), QPoint(200, 400));
linearGradient3.setColorAt(0.0, QColor(255, 0, 0));
linearGradient3.setColorAt(0.5, QColor(0, 255, 0));
linearGradient3.setColorAt(1.0, QColor(0, 0, 255));

QLinearGradient linearGradient4(QPoint(0, 200), QPoint(400, 200));
linearGradient4.setColorAt(0.0, QColor(255, 0, 0));
linearGradient4.setColorAt(0.5, QColor(0, 255, 0));
linearGradient4.setColorAt(1.0, QColor(0, 0, 255));

QLinearGradient linearGradient5(QPoint(0, 0), QPoint(400, 0));
linearGradient5.setColorAt(0.0, QColor(Qt::white));
linearGradient5.setColorAt(1.0, QColor(Qt::black));

QLinearGradient linearGradient6(QPoint(0, 400), QPoint(400, 400));
linearGradient6.setColorAt(0.0, QColor(Qt::black));
linearGradient6.setColorAt(1.0, QColor(Qt::white));

三、使用QLinearGradient填充图形

通过QLinearGradient和QPainter可以填充各种图形,从而实现渐变效果。

// 可以通过以下代码为矩形填充线性渐变
QPainter painter(this);
painter.setBrush(QBrush(linearGradient));
painter.drawRect(QRect(0, 0, 400, 400));

也可以通过QPainterPath和setBrush函数为一个多边形、封闭曲线来设置填充颜色和渐变效果。

// 可以通过以下代码为一个多边形设置渐变效果
QPainterPath path;
path.moveTo(200, 0);
path.lineTo(400, 400);
path.lineTo(0, 400);
path.lineTo(200, 0);

painter.setBrush(QBrush(linearGradient));
painter.drawPath(path);

四、总结

使用QLinearGradient可以轻松创建漂亮的渐变效果,主要需要掌握以下知识:

  • 创建渐变对象需要指定起点和终点坐标
  • 可以通过setColorAt()函数为渐变对象设置颜色
  • QGradient::Spread定义了渐变的扩散方式,QGradient::CoordMode定义了坐标系中渐变的模式
  • 可以连接多个渐变对象来实现更加丰富的效果
  • 可以通过QPainter和QPainterPath将渐变填充到图形或路径中
如何使用QLinearGradient来创建漂亮的渐变效果

2023-05-17
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)