先看效果图:
描述:同样没有做过多的界面美化 旨在使用自定义控件17,另外将自定义控件17的界面排列改成了QGridLayout,可以设置行列占比,其中使用控件多为QChart和自定义控件,若有条件美化,请美化。
关键代码:
//主界面
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// this->setWindowFlags(Qt::FramelessWindowHint);
ui->widget_5->setText(QString("车间加工数据统计"));
Form *form1 = new Form(ui->widget_5);
ui->widget_5->addChart(form1,1,1,1,1);
Form *form2 = new Form(ui->widget_5);
ui->widget_5->addChart(form2,1,2,1,2);
Form *form3 = new Form(ui->widget_5);
ui->widget_5->addChart(form3,2,1,2,1);
Form *form4 = new Form(ui->widget_5);
ui->widget_5->addChart(form4,2,2,2,2);
ui->widget_5->setDeepLineColor(QColor("#03B8FB").light(50));
form1->setDeepLineColor(QColor("#03B8FB").light(50));
form2->setDeepLineColor(QColor("#03B8FB").light(50));
form3->setDeepLineColor(QColor("#03B8FB").light(50));
form4->setDeepLineColor(QColor("#03B8FB").light(50));
//1
QPieSeries *series = new QPieSeries();
series->setHoleSize(0.35);
series->append("Protein 4.2%", 4.2);
QPieSlice *slice = series->append("Fat 15.6%", 15.6);
slice->setExploded();
slice->setLabelVisible();
series->append("Other 23.8%", 23.8);
series->append("Carbs 56.4%", 56.4);
QChartView *chartView = new QChartView(form1);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->chart()->setTitle("Donut with a lemon glaze (100g)");
chartView->chart()->addSeries(series);
chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean);
chartView->chart()->legend()->setFont(QFont("Arial", 7));
chartView->setBackgroundBrush(QBrush(QColor("#222A2C")));
form1->addChart(chartView,1,1,1,1);
//2
QBarSet *low2 = new QBarSet("Min");
QBarSet *high2 = new QBarSet("Max");
*low2 << -52 << -50 << -45.3 << -37.0 << -25.6 << -8.0
<< -6.0 << -11.8 << -19.7 << -32.8 << -43.0 << -48.0;
*high2 << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8
<< 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8;
QStackedBarSeries *series2 = new QStackedBarSeries();
series2->append(low2);
series2->append(high2);
QChart *chart2 = new QChart();
chart2->addSeries(series2);
chart2->setTitle("Temperature records in celcius");
chart2->setAnimationOptions(QChart::SeriesAnimations);
QStringList categories2 = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
QBarCategoryAxis *axis = new QBarCategoryAxis();
axis->append(categories2);
axis->setTitleText("Month");
chart2->createDefaultAxes();
chart2->setAxisX(axis, series2);
chart2->axisY(series2)->setRange(-52, 52);
chart2->axisY(series2)->setTitleText("Temperature [°C]");
chart2->legend()->setVisible(true);
chart2->legend()->setAlignment(Qt::AlignBottom);
QChartView *chartView2 = new QChartView(chart2,form2);
chartView2->setRenderHint(QPainter::Antialiasing);
chartView2->setBackgroundBrush(QBrush(QColor("#222A2C")));
chartView2->chart()->setTheme(QChart::ChartThemeBlueCerulean);
form2->addChart(chartView2,1,1,1,1);
//4
QBarSet *set0 = new QBarSet("Jane");
QBarSet *set1 = new QBarSet("John");
QBarSet *set2 = new QBarSet("Axel");
QBarSet *set3 = new QBarSet("Mary");
QBarSet *set4 = new QBarSet("Samantha");
*set0 << 1 << 2 << 3 << 4 << 5 << 6;
*set1 << 5 << 0 << 0 << 4 << 0 << 7;
*set2 << 3 << 5 << 8 << 13 << 8 << 5;
*set3 << 5 << 6 << 7 << 3 << 4 << 5;
*set4 << 9 << 7 << 5 << 3 << 1 << 2;
QHorizontalPercentBarSeries *series4 = new QHorizontalPercentBarSeries();
series4->append(set0);
series4->append(set1);
series4->append(set2);
series4->append(set3);
series4->append(set4);
QChart *chart4 = new QChart();
chart4->addSeries(series4);
chart4->setTitle("Simple horizontal percent barchart example");
chart4->setAnimationOptions(QChart::SeriesAnimations);
QStringList categories4;
categories4 << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
QBarCategoryAxis *axis4 = new QBarCategoryAxis();
axis->append(categories4);
chart4->createDefaultAxes();
chart4->setAxisY(axis4, series4);
chart4->legend()->setVisible(true);
chart4->legend()->setAlignment(Qt::AlignBottom);
QChartView *chartView4 = new QChartView(chart4,form4);
chartView4->setRenderHint(QPainter::Antialiasing);
chartView4->chart()->setTheme(QChart::ChartThemeBlueCerulean);
form4->addChart(chartView4,1,1,1,1);
chartView4->setBackgroundBrush(QBrush(QColor("#222A2C")));
//3
form3->setText(QString("加工优秀率"));
ProgressBarE *progressbare = new ProgressBarE(form3);
progressbare->setBGColor(QColor("#222A2C"));
progressbare->setMoreVision(true);
progressbare->setWaterColor(Qt::yellow);
progressbare->updateValue(60);
form3->addChart(progressbare,1,1,1,1);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QImage image(":/image/images/bg5.jpg");
painter.setBrush(QColor("#222A2C"));
painter.drawRect(rect());
// painter.drawImage(rect(),image);
}
//控件修改后
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
layout = new QGridLayout();
}
Form::~Form()
{
delete ui;
}
void Form::paintEvent(QPaintEvent *)
{
m_width = this->width();
m_height = this->height();
lineWidth = m_height/10;
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.save();
painter.setPen(Qt::NoPen);
painter.setBrush(bgBrush);
painter.drawRect(rect());
painter.restore();
setLayoutPosition();//设置layout位置
drawLines(&painter);
drawEc(&painter);
drawTitle(&painter,QPoint(eWidth/2,eWidth/2));//画标题框和标题
// layout->setSpacing(0);
// layout->setMargin(0);
}
void Form::drawTitle(QPainter* painter,QPoint point1)
{
if(this->text == "")
{
return;
}
int rectWidth = text.length()*30;
qDebug()<<"rectWidth:"<<rectWidth;
int rectHeight = 65;
QPoint pointStart((m_width-rectWidth)/2,point1.y());
painter->save();
QRect rectTitle(pointStart.x(),pointStart.y(),rectWidth,rectHeight);
QPen penRect = painter->pen();
penRect.setColor(deepLineColor);
penRect.setWidth(3);
painter->setPen(penRect);
painter->drawRoundRect(rectTitle,rectRadius,rectRadius);
painter->restore();
painter->save();
QFont fontText = painter->font();
fontText.setPixelSize(20);
painter->setFont(fontText);
painter->setPen(Qt::white);
painter->drawText(rectTitle,Qt::AlignCenter,text);
painter->restore();
}
void Form::drawEc(QPainter* painter)
{
QRect rect1(0,0,eWidth,eWidth);
QRect rect2(m_width-eWidth,0,eWidth,eWidth);
QRect rect3(0,m_height-eWidth,eWidth,eWidth);
QRect rect4(m_width-eWidth,m_height-eWidth,eWidth,eWidth);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(deepLineColor));
painter->drawEllipse(rect1);
painter->drawEllipse(rect2);
painter->drawEllipse(rect3);
painter->drawEllipse(rect4);
painter->restore();
}
void Form::drawLines(QPainter* painter)
{
QPoint point1(eWidth/2,eWidth/2);//左上
QPoint point2(eWidth/2,m_height-eWidth/2);//左下
QPoint point3(m_width-eWidth/2,m_height-eWidth/2);//右下
QPoint point4(m_width-eWidth/2,eWidth/2);//右上
painter->save();
painter->setPen(lightLineColor);
QPainterPath path;
path.moveTo(point1);
path.lineTo(point2);
path.lineTo(point3);
path.lineTo(point4);
path.lineTo(point1);
painter->drawPath(path);
painter->restore();
painter->save();
QPen pen = painter->pen();
pen.setColor(deepLineColor);
pen.setWidth(3);
painter->setPen(pen);
QLine line1(point1,QPoint(point1.x(),point1.y()+lineWidth));
QLine line2(point1,QPoint(point1.x()+lineWidth,point1.y()));
QLine line3(point2,QPoint(point2.x(),point2.y()-lineWidth));
QLine line4(point2,QPoint(point2.x()+lineWidth,point2.y()));
QLine line5(point3,QPoint(point3.x(),point3.y()-lineWidth));
QLine line6(point3,QPoint(point3.x()-lineWidth,point3.y()));
QLine line7(point4,QPoint(point4.x(),point4.y()+lineWidth));
QLine line8(point4,QPoint(point4.x()-lineWidth,point4.y()));
painter->drawLine(line1);
painter->drawLine(line2);
painter->drawLine(line3);
painter->drawLine(line4);
painter->drawLine(line5);
painter->drawLine(line6);
painter->drawLine(line7);
painter->drawLine(line8);
painter->restore();
}
void Form::setLayoutPosition()
{
QRect rect(eWidth,65+eWidth,m_width-eWidth-10,m_height-eWidth-65-10);
if(layout)
{
if(this->text == "")
{
layout->setGeometry(this->rect().adjusted(10,10,-10,-10));
}else
{
layout->setGeometry(rect);
}
}
}
void Form::addChart(QWidget* widget,int row,int col,int rowSpan,int colSpan)
{
if(widget)
{
widget->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
if(!layout)
{
layout = new QGridLayout();
}
layout->addWidget(widget,row,col,rowSpan,colSpan);
}
update();
}
void Form::setText(QString tempText)
{
this->text = tempText;
update();
}
void Form::setDeepLineColor(QColor color)
{
this->deepLineColor = color;
update();
}
void Form::setLightLineColor(QColor color)
{
this->lightLineColor = color;
update();
}
void Form::setBGColor(QColor color)
{
this->bgBrush.setColor(color);
update();
}
void Form::setBGColor(QGradient g)
{
this->bgBrush = QBrush(g);
update();
}