#include "dialogscence2.h" #include "ui_dialogscence2.h" #include //#include //#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "mainwindow.h" DialogScence2::DialogScence2(QWidget *parent) : QDialog(parent), ui(new Ui::DialogScence2) { ui->setupUi(this); // 产生一个定时器 QTimer *timer = new QTimer(this); //连接这个定时器的信号和槽,利用定时器的timeout(),从而触发clear()槽函数 connect(timer, &QTimer::timeout, this, &DialogScence2::timerProc); //开始定时器,并设定定时周期,每隔5秒就会重启定时器,会重复触发定时,除非你利用stop()将定时器关掉 timer->start(100); // 仅仅启动定时器一次 // timer->setSingleShot(true); InitDisplay(); //label->setPixmap(QPixmap("./pic.jpg")); // ui->label_27->setPixmap(QPixmap(":/Image/JPEG.webp")); ShowParams(); // ui->centralwidget->installEventFilter(this);//在label上安装事件过滤器,this指针指定当事件发生时调用当前类中的事件过滤器进行处理 QTimer::singleShot(100,this,&DialogScence2::DelayInit); } DialogScence2::~DialogScence2() { delete ui; } void DialogScence2::InitDisplay() { //设置具体阴影 QGraphicsDropShadowEffect * shadow_effect = new QGraphicsDropShadowEffect(this); shadow_effect->setOffset(0,0); //阴影颜色 shadow_effect->setColor(QColor(38,78,119,127)); //阴影半径 shadow_effect->setBlurRadius(20); ui->frame->setGraphicsEffect(shadow_effect); ui->frame_2->setGraphicsEffect(shadow_effect); ui->frame_3->setGraphicsEffect(shadow_effect); ui->frame_4->setGraphicsEffect(shadow_effect); ui->frame_5->setGraphicsEffect(shadow_effect); qDebug() << "Scence2 Show Charts"; ///* auto charview1 = new QChartView; ui->verticalLayout_3->addWidget(charview1); charview1->setStyleSheet("background: transparent"); auto chart1 = CreateBarChart(); chart1->setBackgroundVisible(false); charview1->setChart(chart1); //*/ /* auto charview6 = new QChartView; ui->horizontalLayout_11->addWidget(charview6); auto chart6 = createScatterChart(); charview6->setChart(chart6); */ qDebug() << "Scence2 Show Charts Done"; QStringList alerts; QString s1; alerts<label_Alert->setText(s1); // ui->label_Alert->installEventFilter(this); // ui->centralwidget->installEventFilter(this);//在label上安装事件过滤器,this指针指定当事件发生时调用当前类中的事件过滤器进行处理 } void DialogScence2::DelayInit() { resizeCtrls(); } void DialogScence2::myDraw(QWidget *widget) { QPainter painter(widget); painter.setRenderHint(QPainter::Antialiasing); //取得画布大小和中心方形。 int w = this->rect().width(); int h = this->rect().height(); int s; int wmargin=10; int hmargin=10; int ButtomMgn = 32; QRect rect1(wmargin,hmargin,w-wmargin*2,h-hmargin*2 -ButtomMgn); if (w>h) { s= h; wmargin=(w-s)/2; }else{ s= w; hmargin=(h-s)/2; } //画边框 QColor color1(0xFF6060); QColor color2(0x60F080); QColor color3(0x6080FF); QBrush brush1(QColor(0xFFB060)); QConicalGradient gradient; gradient.setCenter(rect1.center()); gradient.setAngle(360*16 - nPos*4); gradient.setColorAt(0, color1); gradient.setColorAt(0.3, color2); gradient.setColorAt(0.6, color3); gradient.setColorAt(1, color1); QPen pen0(gradient,8); painter.setPen(pen0); painter.drawRoundRect(rect1,5,5); } bool DialogScence2::eventFilter(QObject * obj, QEvent * eve) { if (eve->type()== QEvent::Resize) { if (obj == ui->label_Alert){ double height = ui->label_Alert->height(); auto font = ui->label_Alert->font(); font.setPointSize(height/5); ui->label_Alert->setFont(font); QString s1; s1=QString::number(height); ui->label_Alert->setText(s1); return true; } }else if(eve->type() == QEvent::Paint)//发生绘图事件, { if (obj == ui->frame ){ if(flag == 1)//标志位为1才在label上绘图,否者不绘图 { //myDraw(obj); //return true; } else { //return false; } } } else {} return QDialog::eventFilter(obj,eve);//其它绘图事件交给父类处理 } void DialogScence2::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Enter: break; case Qt::Key_F11: if (bfull==0) { oldsize = this->size(); // showMaximized(); // setWindowFlags ( Qt::FramelessWindowHint); showFullScreen(); bfull = 1; } break; case Qt::Key_Escape: if (bfull == 1){ // setWindowFlags (Qt::Window ); showNormal(); // this->resize(oldsize); bfull=0; resizeCtrls(); bNeedResize=2; } else {QDialog::keyPressEvent(event);} break; default: QDialog::keyPressEvent(event); } } void DialogScence2::timerProc() { QString s1; s1 = QDateTime::currentDateTime().toString(u8"M月dd ddd h:mm:ss"); //"yyyy-MM-dd HH:mm:ss" ui->label_Time_3->setText(s1); nPos+=1; // ui->centralwidget->repaint(); if (bNeedResize) { bNeedResize--; if (bNeedResize==0){ resizeCtrls(); } } this->repaint(); } void DialogScence2::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); //取得画布大小和中心方形。 int w = this->rect().width(); int h = this->rect().height(); int s; int wmargin=10; int hmargin=10; int ButtomMgn = 0; QRect rect1(wmargin,hmargin,w-wmargin*2,h-hmargin*2 -ButtomMgn); if (w>h) { s= h; wmargin=(w-s)/2; }else{ s= w; hmargin=(h-s)/2; } //画边框 // QPen pen0(QColor("#FFFFFF")); // painter.setPen(pen0); // painter.drawRoundRect(this->rect(),5,5); QColor color1(0xFF6060); QColor color2(0x60F080); QColor color3(0x6080FF); QBrush brush1(QColor(0xFFB060)); QConicalGradient gradient; gradient.setCenter(rect1.center()); gradient.setAngle(360*16 - nPos*4); gradient.setColorAt(0, color1); gradient.setColorAt(0.3, color2); gradient.setColorAt(0.6, color3); gradient.setColorAt(1, color1); QPen pen0(gradient,8); painter.setPen(pen0); painter.drawRoundRect(rect1,5,5); } void DialogScence2::resizeLabelFont(QLabel * label1,int lines, const QString & color) { QString s1; double height = label1->height(); height = label1->geometry().height(); // s1=QString::number(height); // label1->setText(s1); //auto font1=label1->font(); //font1.setPointSize(height/5); //label1->setFont(font1); QString colorseg; if (!color.isEmpty()) {colorseg= QString("color:%1;").arg(color);} int sheight=height/1.5/(lines+0.5); if (sheight>80) {sheight=80;} qDebug()<<"height" << height << "lines" << lines << "sHeight" << sheight; s1=QString("QLabel{font:%1px;%2}").arg(sheight).arg(colorseg); label1->setStyleSheet(s1); //label1->setScaledContents(true); } void DialogScence2::resizeCtrls() { /* *{ background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(40, 60, 100, 255), stop:1 rgba(20, 40, 80, 255)); font-size:18pt; font-style:MingLiU-ExtB; } QFrame{ background-color: rgb(0,30,80,160); border-radius:10px; border:1px solid rgb(100,169,139); } QLabel{ background:rgba(85,170,255,0); color:white; font-style:MingLiU-ExtB; font-size:22pt; } QLabel:hover{ background:rgba(85,170,255,0); color:white; font-style:MingLiU-ExtB; font-size:22pt; } QLabel:TimeDisplay{ background:rgba(85,170,255,0); color:white; font-style:MingLiU-ExtB; font-size:22pt; } */ resizeLabelFont(ui->label_46,1); resizeLabelFont(ui->label_Time_3,2); resizeLabelFont(ui->label,1); resizeLabelFont(ui->label_2,1); resizeLabelFont(ui->label_Alert,4,"yellow"); //resizeLabelFont(ui->label_Info,20); resizeLabelFont(ui->label_4,1); resizeLabelFont(ui->label_5,2); resizeLabelFont(ui->label_6,1); resizeLabelFont(ui->label_7,2); resizeLabelFont(ui->label_8,1); resizeLabelFont(ui->label_9,2); resizeLabelFont(ui->label_47,3); QString s1; QFont font1 = ui->plainTextEdit_Info->font(); double height = ui->plainTextEdit_Info->height(); s1=QString("color:white;font:%1px;background-color:rgb(0,30,80,160)").arg(int(height/15)); ui->plainTextEdit_Info->setStyleSheet(s1); font1.setPixelSize(height/5); ui->plainTextEdit_Info->setFont(font1); } void DialogScence2::resizeEvent(QResizeEvent *event) { QDialog::resizeEvent(event); resizeCtrls(); } void DialogScence2::ShowParams() { } void DialogScence2::setChartAttribute(QChart *chart, QString title) { // 创建默认坐标轴 chart->createDefaultAxes(); // 设置标题 chart->setTitle(title); // 设置data,在设置标题时使用 chart->setData(Qt::UserRole, title); } QChart * DialogScence2::CreateBarChart() { // 创建QChart对象 QChart *chart = new QChart(); // 创建饼图系列对象 QBarSeries *series = new QBarSeries(chart); qsrand(QDateTime::currentMSecsSinceEpoch() % 20000); // 添加图表值 for(int count = 0; count < 3; ++count) { // 创建曲线图系列对象 QBarSet *set = new QBarSet(QString(65 + count)+u8"班"); for(int index = 0; index < 4; ++index) { set->append(33 + rand() % 50); } series->append(set); } series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd); series->setLabelsVisible(true); chart->setTheme(QChart::ChartThemeBlueCerulean); chart->addSeries(series); chart->setAnimationOptions(QChart::SeriesAnimations); QString title = u8"班组加工数据分析汇总"; chart->setTitle(title); chart->setData(Qt::UserRole, title); QFont font1; font1.setPixelSize(24); //chart->setFont(QFont("宋体",32)); chart->setTitleFont(font1); //setChartAttribute(chart, ); //坐标轴// QStringList categories; categories <append(categories); chart->addAxis(axisX,Qt::AlignBottom); series->attachAxis(axisX); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0,100); chart->addAxis(axisY,Qt::AlignLeft); series->attachAxis(axisY); QFont font2; font2.setPixelSize(22); QFont font3; font3.setPixelSize(16); axisX->setLabelsFont(font2); axisY->setLabelsFont(font3); // axisX->setTitleFont(font2); // 将series添加到QChart对象中 // 设置通用属性 QFont font4; font4.setPixelSize(24); chart->legend()->setFont(font4); // chart->axisX()->setLabelsFont(font2); // chart->axisY()->setLabelsFont(font2); // chart->axes(Qt::Vertical).at(0)->setGridLineVisible(false); // chart->axes(Qt::Horizontal).at(0)->setGridLineVisible(false); // 返回QChart对象 // chart->legend()->setVisible(true); // chart->legend()->setAlignment(Qt::AlignBottom); return chart; } QChart *DialogScence2::createPieChart() { // 创建QChart对象 QChart *chart = new QChart(); // 创建饼图系列对象 QPieSeries *series = new QPieSeries(chart); // 遍历假数据容器将数据添加到series对象中 qsrand(QDateTime::currentMSecsSinceEpoch() % 20000); // 添加图表值 for(int index = 0; index != 5; ++index) { series->append(QString(65 + index), rand() % 100); } // 将series添加到QChart对象中 chart->addSeries(series); // 设置通用属性 setChartAttribute(chart, u8"饼图"); chart->legend()->setAlignment(Qt::AlignRight); // 返回QChart对象 return chart; } QChart *DialogScence2::createAreaChart() { // 创建QChart对象 QChart *chart = new QChart(); // 添加图表值 //! 创建区域图系列对象 //! 区域图的系列对象 QLineSeries *lowerSeries = nullptr; for(int count = 1; count != 3; ++count) { // 创建折线图系列对象 QLineSeries *upperSeries = new QLineSeries(chart); for(int index = 1; index != 5; ++index) { upperSeries->append(index , index * count * 5 + rand() % 5); } // 将series添加到QChart对象中 QAreaSeries *series = new QAreaSeries(upperSeries, lowerSeries); lowerSeries = upperSeries; chart->addSeries(series); } // 设置通用属性 setChartAttribute(chart, u8"面积图 "); // 返回QChart对象 return chart; } QChart *DialogScence2::createLineChart() { // 创建QChart对象 QChart *chart = new QChart(); // 添加图表值 for(int count = 0; count != 3; ++count) { // 创建折线图系列对象 QLineSeries *series = new QLineSeries(chart); for(int index = 0; index != 5; ++index) { series->append(index * count, index * count - rand() % 3); } // 将series添加到QChart对象中 chart->addSeries(series); } // 设置通用属性 setChartAttribute(chart, u8"折线图 "); // 返回QChart对象 return chart; } QChart *DialogScence2::createSpLineChart() { // 创建QChart对象 QChart *chart = new QChart(); // 添加图表值 for(int count = 0; count != 3; ++count) { // 创建曲线图系列对象 QSplineSeries *series = new QSplineSeries(chart); for(int index = 0; index != 5; ++index) { series->append(index * count, index * count - rand() % 8); } // 将series添加到QChart对象中 chart->addSeries(series); } // 设置通用属性 setChartAttribute(chart, u8"曲线图 "); // 返回QChart对象 return chart; } QChart *DialogScence2::createScatterChart() { // 创建QChart对象 QChart *chart = new QChart(); // 添加图表值 for(int count = 0; count != 3; ++count) { // 创建曲线图系列对象 QScatterSeries *series = new QScatterSeries(chart); for(int index = 0; index != 5; ++index) { series->append(index , index * count * 5 + rand() % 5); } // 将series添加到QChart对象中 chart->addSeries(series); } // 设置通用属性 setChartAttribute(chart, u8"散点图 "); // 返回QChart对象 return chart; } /* void DialogScence2::on_comboBox_currentIndexChanged(int index) { // 拿到chart容器中相同索引位置的QChart对象,并将其设置到view对象上 QChart *chart = m_chartVector[index]; m_chartView->setChart(chart); } void DialogScence2::on_legendGroupButtonClicked(QAbstractButton *button) { // 拿到发送信号的单选按钮 QRadioButton *btn = dynamic_cast(button); // 遍历图表容器,将legend的aign属性设为当前按钮对应的属性状态 foreach(QChart *chart, m_chartVector) { //! 拿到当前单选按钮的属性值并强转为Qt::Alignment类型 //! Qt::Alignment align = static_cast(btn->property("LegendValue").toUInt()); // 将类型值设置到当前图表上 chart->legend()->setAlignment(align); } } void DialogScence2::on_animationGroup_buttonClicked(QAbstractButton *button) { // 拿到发送信号的单选按钮 QRadioButton *btn = dynamic_cast(button); // 遍历图表容器,将chart的动画设为当前按钮对应的属性状态 foreach(QChart *chart, m_chartVector) { //! 拿到当前单选按钮的属性值并强转为Qt::AnimationOption //! 其属性值为对应动画的int值,可通过帮助查看具体值 QChart::AnimationOption animation = static_cast(btn->property("AnimationValue").toUInt()); // 将类型值设置到当前图表上 chart->setAnimationOptions(animation); } } void DialogScence2::on_otherGroup_buttonClicked(QAbstractButton *button) { // 拿到发送信号的复选按钮 QCheckBox *btn = dynamic_cast(button); // 判断所点击按钮的勾选状态 bool checked = btn->isChecked(); // 通过按钮动态属性判断按钮操作 switch(btn->property("btnType").toInt()) { case 0: { // 设置view的防锯齿,其还包含其他防锯齿选项,可查看帮助了解 m_chartView->setRenderHint(QPainter::Antialiasing, checked); break; } case 1: { foreach(QChart *chart, m_chartVector) { // 通过三目运算符判断标题是否获取data中的值 QString title = checked? chart->data(Qt::UserRole).toString(): ""; // 设置标题 chart->setTitle(title); } break; } default: break; } } */