这是在Qt的刚开始创建项目的时候所与遇到的问题,希望能帮我解决一下
首先 把这个形状叫做A
方便描述(因为我也不知道这是啥形状)
目标是由三个这种矩形组成的滑动条 如下图
绘制这个形状首先要知道对应A所在的矩形
然后根据这个矩形就可以通过这个函数makeSemicircleEdgeRectanglePath获取A的painterPath了
makeSemicircleEdgeRectanglePath(QRect rect)
{
int x = rect.x();
int y = rect.y();
int w = rect.width();
int h = rect.height();
int r = h/2;
QPoint leftCenter(x+r,y+r); //左侧半圆圆心
QPoint rightCenter(x+w-r,y+r); //右侧半圆圆心
QPoint leftTop(x+r,y); //左上角顶点
QPoint rightBottom(x+w-r,y+h); //右下角顶点
QRect leftSemicircleRect(x,y,h,h); //左半圆所在矩形
QRect rightSemicircleRect(x+w-h,y,h,h);//右半圆所在矩形
QPainterPath semicircleEdgeRectanglePath;
semicircleEdgeRectanglePath.moveTo(leftCenter);
semicircleEdgeRectanglePath.arcTo(leftSemicircleRect,90,180);
semicircleEdgeRectanglePath.lineTo(rightBottom);
semicircleEdgeRectanglePath.moveTo(rightCenter);
semicircleEdgeRectanglePath.arcTo(rightSemicircleRect,270,180);
semicircleEdgeRectanglePath.lineTo(leftTop);
semicircleEdgeRectanglePath.closeSubpath();
return semicircleEdgeRectanglePath;
}
//使用函数
QRect rect(x,y,w,h);
//painter.setPen(Qt::SolidLine);
painter.setPen(Qt::NoPen);
QLinearGradient linearGradient(x,y,x+w,y+h);
linearGradient.setColorAt(0.0,QColor(COLOR_LINE_BEGIN));
linearGradient.setColorAt(1.0,QColor(COLOR_LINE_END));
painter.setBrush(linearGradient);
QPainterPath linePath = makeSemicircleEdgeRectanglePath(rect);
painter.drawPath(linePath);
运行程序得到的效果却并不理想
查看官方文档知道当我从右下角moveTo到rightCenter时就已经形成了一段封闭路径
semicircleEdgeRectanglePath.moveTo(rightCenter);
moveTo() function implicitly starts a new subpath, and closes the previous one.The
回答: 在Qt中创建项目的基本操作包括:选择“文件”—“新建文件或项目”—“应用程序”—“QT Widgets Application”,选择默认设置,根据提示就可以得到一个项目。如果需要建立一个基于QWidget的UI类,可以选择这个选项。在创建项目的过程中,可能会遇到编码问题,可以在源文件的开头加入#pragma execution_character_set("utf-8")来指定文件的编码,并使用UE编辑器将文件另存为UTF-8编码。在开发过程中,如果遇到数据头不正确的问题,可以对报文进行判断,符合要求的报文头继续后续动作,不符合要求的剔除,并循环直到报文头正确。如果需要计算文字颜色以便能够在已知背景色的情况下清晰绘制文字,可以通过计算背景色的灰度值再根据灰度值来计算合适的前景色。如果需要禁用QTableView或者QTableWidget的列拖动功能,可以使用如下代码:
ui->tableView->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
另外,QtCreator是跨平台的Qt官方IDE,使用智能提示与调试功能不如VS,但QtDesigner设计UI界面操作方便,集成了Qt的帮助文档。可以通过Qt官方文档或者其他资源来了解所有控件属性的作用。同时,其他模式是不会检测断点的。对于按钮控件的背景颜色,可以为每个按钮添加他自己的样式表,加入background-image来使它们能够与整体背景区分开来。