#include "dialogscence1.h" #include "ui_dialogscence1.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" DialogScence1::DialogScence1(QWidget *parent) : QDialog(parent), ui(new Ui::DialogScence1) { ui->setupUi(this); // 产生一个定时器 QTimer *timer = new QTimer(this); //连接这个定时器的信号和槽,利用定时器的timeout(),从而触发clear()槽函数 connect(timer, &QTimer::timeout, this, &DialogScence1::timerProc); //开始定时器,并设定定时周期,每隔5秒就会重启定时器,会重复触发定时,除非你利用stop()将定时器关掉 timer->start(100); // 仅仅启动定时器一次 // timer->setSingleShot(true); InitDisplay(); ShowParams(); // ui->centralwidget->installEventFilter(this);//在label上安装事件过滤器,this指针指定当事件发生时调用当前类中的事件过滤器进行处理 } DialogScence1::~DialogScence1() { delete ui; } void DialogScence1::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 DialogScence1::eventFilter(QObject * obj, QEvent * eve) { /* if(obj == ui->centralwidget && eve->type() == QEvent::Paint)//发生绘图事件,且是在label上发生的 { if(flag == 1)//标志位为1才在label上绘图,否者不绘图 { myDraw(ui->centralwidget); return true; } else return false; } else */ return QDialog::eventFilter(obj,eve);//其它绘图事件交给父类处理 } void DialogScence1::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; } else {QDialog::keyPressEvent(event);} break; default: QDialog::keyPressEvent(event); } } void DialogScence1::InitDisplay() { QPixmap pixmap1(":/Image/JPEG.webp"); int w=pixmap1.width(); int h=pixmap1.height(); w/=2;h/=2; // w = ui->label_27->width(); // h = ui->label_27->height(); //pixmap1= pixmap1.scaled(w,h,Qt::KeepAspectRatio,Qt::SmoothTransformation); //ui->label_27->setPixmap(pixmap1); qDebug() << "Scence1 Show Charts"; ui->frame_8->SetValue(32); ui->frame_8->SetTarget(50); ui->frame_8->SetRange(80); auto charview1 = new QChartView; // charview1->setStyleSheet("background: transparent"); ui->horizontalLayout_13->addWidget(charview1); auto chart1 = CreateBarChart(); QPen pen1 = chart1->backgroundPen(); pen1.setWidth(5); chart1->setBackgroundPen(pen1); chart1->setBackgroundVisible(false); charview1->setChart(chart1); auto charview2 = new QChartView; // charview2->setStyleSheet("background: transparent"); ui->horizontalLayout_14->addWidget(charview2); auto chart2 = createHorBarChart(); chart2->setBackgroundVisible(false); charview2->setChart(chart2); auto charview3 = new QChartView; ui->horizontalLayout_15->addWidget(charview3); auto chart3 = createAreaChart(); chart3->setBackgroundVisible(false); charview3->setChart(chart3); auto charview4 = new QChartView; ui->horizontalLayout_19->addWidget(charview4); auto chart4 = createLineChart(); chart4->setBackgroundVisible(false); charview4->setChart(chart4); auto charview5 = new QChartView; ui->horizontalLayout_20->addWidget(charview5); auto chart5 = createSpLineChart(); chart5->setBackgroundVisible(false); charview5->setChart(chart5); /* auto charview6 = new QChartView; ui->horizontalLayout_11->addWidget(charview6); auto chart6 = createScatterChart(); charview6->setChart(chart6); */ ui->frame_17->SetPosition(21); ui->frame_18->SetPosition(22); ui->frame_19->SetPosition(23); ui->frame_20->SetPosition(24); qDebug() << "Scence1 Show Charts Done"; } void DialogScence1::timerProc() { QString s1; s1 = QDateTime::currentDateTime().toString(u8"MM月dd ddd h:mm:ss"); //"yyyy-MM-dd HH:mm:ss" ui->label_Time_2->setText(s1); nPos+=1; // ui->centralwidget->repaint(); this->repaint(); } void DialogScence1::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 DialogScence1::ShowParams() { } void DialogScence1::setChartAttribute(QChart *chart, QString title) { // 创建默认坐标轴 chart->createDefaultAxes(); // 设置标题 chart->setTitle(title); // 设置data,在设置标题时使用 chart->setData(Qt::UserRole, title); } QChart * DialogScence1::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)+QString::fromLocal8Bit("班")); for(int index = 0; index < 5; ++index) { set->append(33 + rand() % 50); } series->append(set); } series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd); series->setLabelsVisible(true); // 将series添加到QChart对象中 chart->addSeries(series); // 设置通用属性 chart->setTheme(QChart::ChartThemeBlueCerulean); chart->setAnimationOptions(QChart::SeriesAnimations); setChartAttribute(chart, u8"周生产数据库"); QFont font1; QFont font2; font1.setPixelSize(24); font2.setPixelSize(20); //chart->setFont(QFont("宋体",32)); chart->setTitleFont(font1); chart->axisX()->setLabelsFont(font2); chart->axisY()->setLabelsFont(font2); chart->axes(Qt::Vertical).at(0)->setGridLineVisible(false); chart->axes(Qt::Horizontal).at(0)->setGridLineVisible(false); // chart->axes(Qt::Vertical).at(0)->setRange(0,100); // 返回QChart对象 return chart; } QChart *DialogScence1::createHorBarChart() { // 创建QChart对象 QChart *chart = new QChart(); // 创建饼图系列对象 QHorizontalBarSeries *series = new QHorizontalBarSeries(chart); qsrand(QDateTime::currentMSecsSinceEpoch() % 20000); // 添加图表值 for(int count = 0; count < 1; ++count) { // 创建曲线图系列对象 QString sBarName = QString(u8"AGV").arg(count); QBarSet *set = new QBarSet(sBarName); for(int index = 0; index < 3; ++index) { set->append(33 + rand() % 50); } series->append(set); } series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd); series->setLabelsVisible(true); // 将series添加到QChart对象中 chart->addSeries(series); // 坐标轴 QStringList categories; categories <append(categories); series->attachAxis(axisX); chart->addAxis(axisX,Qt::AlignLeft); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0,100); chart->addAxis(axisY,Qt::AlignBottom); series->attachAxis(axisY); // 设置通用属性 chart->setTheme(QChart::ChartThemeBlueCerulean); chart->setAnimationOptions(QChart::SeriesAnimations); QString title = u8"AGV调度数据"; chart->setTitle(title); chart->setData(Qt::UserRole, title); // setChartAttribute(chart, u8"AGV调度数据"); QFont font1; QFont font2; font1.setPixelSize(24); font2.setPixelSize(20); //chart->setFont(QFont("宋体",32)); chart->setTitleFont(font1); chart->axisX()->setLabelsFont(font2); chart->axisY()->setLabelsFont(font2); chart->axes(Qt::Vertical).at(0)->setGridLineVisible(false); chart->axes(Qt::Horizontal).at(0)->setGridLineVisible(false); // chart->axes(Qt::Vertical).at(0)->setRange(0,100); // 返回QChart对象 return chart; } QChart *DialogScence1::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 *DialogScence1::createAreaChart() { // 创建QChart对象 QChart *chart = new QChart(); // 添加图表值 //! 创建区域图系列对象 //! 区域图的系列对象 QLineSeries *lowerSeries = nullptr; for(int count = 1; count < 2; ++count) { // 创建折线图系列对象 QLineSeries *upperSeries = new QLineSeries(chart); /* for(int index = 0; index < 5; ++index) { upperSeries->append(index , index * 3 + rand() % 50); } */ upperSeries->append(1,0); upperSeries->append(1.5,0); upperSeries->append(2,0); upperSeries->append(2.5,0); upperSeries->append(3,32); upperSeries->append(3.5,0); upperSeries->append(4,0); upperSeries->append(4.5,0); upperSeries->append(5,0); // 将series添加到QChart对象中 QAreaSeries *series = new QAreaSeries(upperSeries, lowerSeries); lowerSeries = upperSeries; chart->addSeries(series); } // 坐标轴 QStringList categories; categories <append(categories); // series->attachAxis(axisX); chart->addAxis(axisX,Qt::AlignBottom); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0,100); chart->addAxis(axisY,Qt::AlignLeft); // series->attachAxis(axisY); // 设置通用属性 chart->setTheme(QChart::ChartThemeBlueCerulean); chart->setAnimationOptions(QChart::SeriesAnimations); QString title = u8"坩埚尺寸数据"; chart->setTitle(title); chart->setData(Qt::UserRole, title); QFont font1; QFont font2; font1.setPixelSize(24); font2.setPixelSize(20); chart->setTitleFont(font1); // 返回QChart对象 return chart; } QChart *DialogScence1::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); } // 坐标轴 QStringList categories; categories <append(categories); // series->attachAxis(axisX); chart->addAxis(axisX,Qt::AlignBottom); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0,100); chart->addAxis(axisY,Qt::AlignLeft); // series->attachAxis(axisY); // 设置通用属性 chart->setTheme(QChart::ChartThemeBlueCerulean); chart->setAnimationOptions(QChart::SeriesAnimations); QString title = u8"周产量统计"; chart->setTitle(title); chart->setData(Qt::UserRole, title); QFont font1; QFont font2; font1.setPixelSize(24); font2.setPixelSize(20); chart->setTitleFont(font1); // 设置通用属性 // setChartAttribute(chart, u8"折线图 "); // 返回QChart对象 return chart; } QChart *DialogScence1::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); } // 坐标轴 QStringList categories; categories <append(categories); // series->attachAxis(axisX); chart->addAxis(axisX,Qt::AlignBottom); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0,100); chart->addAxis(axisY,Qt::AlignLeft); // series->attachAxis(axisY); // 设置通用属性 chart->setTheme(QChart::ChartThemeBlueCerulean); chart->setAnimationOptions(QChart::SeriesAnimations); QString title = u8"月产量统计"; chart->setTitle(title); chart->setData(Qt::UserRole, title); QFont font1; QFont font2; font1.setPixelSize(24); font2.setPixelSize(20); chart->setTitleFont(font1); // 设置通用属性 // setChartAttribute(chart, u8"曲线图 "); // 返回QChart对象 return chart; } QChart *DialogScence1::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 DialogScence1::on_comboBox_currentIndexChanged(int index) { // 拿到chart容器中相同索引位置的QChart对象,并将其设置到view对象上 QChart *chart = m_chartVector[index]; m_chartView->setChart(chart); } void DialogScence1::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 DialogScence1::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 DialogScence1::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; } } */