Qt第十九天
阿新 • • 發佈:2018-12-21
mianwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QChart> //using namespace QtCharts; QT_CHARTS_USE_NAMESPACE #include <QLineSeries> #include <QValueAxis> #include <QTime> #include <QChart> #include <QtMath> #include <QFontDialog> #include <QColorDialog> #include "qwdialogpen.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT private: QLineSeries *curSeries;//當前序列 QValueAxis *curAxis;//當前座標軸 void createChart();//建立圖表 void prepareData();//更新資料 void updateFromChart();//從圖表更新的介面 public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_actDraw_triggered(); void on_btnSetTitle_clicked(); void on_btnSetTitleFont_clicked(); void on_btnSetMargin_clicked(); void on_chkPointVisible_clicked(bool checked); void on_chkPointLabelVisible_clicked(bool checked); void on_btnSeriesName_clicked(); void on_btnSeriesColor_clicked(); void on_chkLegendVisible_clicked(bool checked); void on_btnSetAxisRange_clicked(); void on_spinTickCount_valueChanged(int arg1); void on_spinMinorTickCount_valueChanged(int arg1); void on_radioX_clicked(); void on_radioY_clicked(); void on_chkBoxVisible_clicked(bool checked); void on_chkGridLineVisible_clicked(bool checked); void on_chkMinorTickVisible_clicked(bool checked); void on_radioButton_clicked(); void on_radioButton_2_clicked(); void on_radioButton_3_clicked(); void on_radioButton_4_clicked(); void on_btnLegendFont_clicked(); void on_btnLegendlabelColor_clicked(); void on_btnAxisSetTitle_clicked(); void on_btnAxisSetTitleFont_clicked(); void on_chkBoxAxisTitle_clicked(bool checked); void on_pushButton_clicked(); void on_btnAxisLabelColor_clicked(); void on_btnAxisLabelFont_clicked(); void on_btnGridLineColor_clicked(); void on_pushButton_10_clicked(); void on_chkAxisLineVisible_clicked(bool checked); void on_btnAxisLinePen_clicked(); void on_btnAxisLinePenColor_clicked(); void on_btnMinorColor_clicked(); void on_btnMinorPen_clicked(); void on_radioSeries0_clicked(bool checked); void on_radioSeries1_clicked(); void on_radioSeries0_clicked(); void on_chkSeriesVisible_clicked(bool checked); void on_btnSeriesPen_clicked(); void on_sliderSeriesOpacity_valueChanged(int value); void on_btnSeriesLabColor_clicked(); void on_btnSeriesLabFont_clicked(); void on_radioSeriesLabFormat0_clicked(); void on_radioSeriesLabFormat1_clicked(); void on_cBoxAnimation_currentIndexChanged(int index); void on_cBoxTheme_currentIndexChanged(int index); void on_actZoomIn_triggered(); void on_actZoomOut_triggered(); void on_actZoomReset_triggered(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
qwdialogpen.h
#ifndef QWDIALOGPEN_H #define QWDIALOGPEN_H #include <QDialog> #include <QPen> #include <QColor> namespace Ui { class QWDialogPen; } class QWDialogPen : public QDialog { Q_OBJECT QPen m_pen; public: void setPen(QPen pen);//設定QPen,用於對話方塊的顯示 QPen getPen();//獲取對話方塊設定的QPen屬性 static QPen getPen(QPen inipen,bool &ok);//靜態函式 public: explicit QWDialogPen(QWidget *parent = nullptr); ~QWDialogPen(); private: Ui::QWDialogPen *ui; }; #endif // QWDIALOGPEN_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); createChart(); prepareData(); updateFromChart(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::createChart()//建立圖表 { QChart *chart=new QChart(); chart->setTitle("圖表名稱"); ui->chartView->setChart(chart); ui->chartView->setRenderHint(QPainter::Antialiasing); //新建序列 QLineSeries *series0=new QLineSeries(); QLineSeries *series1=new QLineSeries(); series0->setName("sin曲線"); series1->setName("cos曲線"); curSeries=series0; //設定序列0畫筆 QPen pen; pen.setWidth(2); pen.setStyle(Qt::SolidLine); pen.setColor(Qt::blue); series0->setPen(pen); //設定序列1畫筆 pen.setStyle(Qt::DashLine); pen.setColor(Qt::red); pen.setWidth(1); series1->setPen(pen); //新增序列 chart->addSeries(series0); chart->addSeries(series1); //建立X座標軸 QValueAxis *axisX=new QValueAxis; curAxis=axisX; axisX->setRange(0,10);//範圍 axisX->setLabelFormat("%.1f");//標籤格式 axisX->setTickCount(11);//主分隔個數 axisX->setMinorTickCount(4);//小分隔個數 axisX->setTitleText("time(secs)");//標題 //建立Y座標軸 QValueAxis *axisY=new QValueAxis; axisY->setRange(-2,2); axisY->setLabelFormat("%.2f"); axisY->setTickCount(5); axisY->setMinorTickCount(4); axisY->setTitleText("Value"); //設定座標軸 chart->setAxisX(axisX,series0); chart->setAxisY(axisY,series0); chart->setAxisX(axisX,series1); chart->setAxisY(axisY,series1); } void MainWindow::prepareData()//為序列生成資料 { QLineSeries *series0=(QLineSeries*)ui->chartView->chart()->series().at(0);//獲取序列0 QLineSeries *series1=(QLineSeries*)ui->chartView->chart()->series().at(1);//獲取序列1 //清除資料 series0->clear(); series1->clear(); qsrand(QTime::currentTime().second());// qreal t=0,y1,y2,intv=0.1; qreal rd; int cnt=100; for(int i=0;i<cnt;i++) { rd=(qrand()%10)-5;//隨機範圍-5-5 y1=qSin(t)+rd/50; series0->append(t,y1);//序列新增資料點 y2=qCos(t)+rd/50; series1->append(t,y2); t+=intv; } } void MainWindow::updateFromChart()//從列表獲取資料更新介面顯示 { QChart *aChart=ui->chartView->chart();//獲取chart ui->editTitle->setText(aChart->title());//設定圖表標題 QMargins mg=aChart->margins();//獲取邊距 ui->spinMarginTop->setValue(mg.top()); ui->spinMarginLeft->setValue(mg.left()); ui->spinMarginRight->setValue(mg.right()); ui->spinMarginBottom->setValue(mg.bottom()); } void MainWindow::on_actDraw_triggered()//重新整理 { prepareData(); } void MainWindow::on_btnSetTitle_clicked()//設定標題 { QString str=ui->editTitle->text(); ui->chartView->chart()->setTitle(str); } void MainWindow::on_btnSetTitleFont_clicked()//設定字型 { QFont font=ui->chartView->chart()->titleFont(); bool ok=false; font=QFontDialog::getFont(&ok,font); if(ok) { ui->chartView->chart()->setTitleFont(font); } } void MainWindow::on_btnSetMargin_clicked()//設定邊距 { QMargins mg; mg.setTop(ui->spinMarginTop->value()); mg.setLeft(ui->spinMarginLeft->value()); mg.setRight(ui->spinMarginRight->value()); mg.setBottom(ui->spinMarginBottom->value()); ui->chartView->chart()->setMargins(mg); } void MainWindow::on_chkPointVisible_clicked(bool checked)//資料點可見性 { curSeries->setPointsVisible(checked); } void MainWindow::on_chkPointLabelVisible_clicked(bool checked)//資料點標籤可見 { curSeries->setPointLabelsVisible(checked); } void MainWindow::on_btnSeriesName_clicked()//設定序列名稱 { QString str=ui->editSeriesName->text(); curSeries->setName(str); } void MainWindow::on_btnSeriesColor_clicked()//序列曲線的顏色 { QColor color=curSeries->color(); color=QColorDialog::getColor(color); if(color.isValid()) { curSeries->setColor(color); } } void MainWindow::on_chkLegendVisible_clicked(bool checked)//圖例是否可見 { ui->chartView->chart()->legend()->setVisible(checked); } void MainWindow::on_btnSetAxisRange_clicked()//設定座標軸範圍 { curAxis->setRange(ui->spinAxisMin->value(),ui->spinAxisMax->value()); } void MainWindow::on_spinTickCount_valueChanged(int arg1)//主分隔個數改變 { curAxis->setTickCount(arg1); } void MainWindow::on_spinMinorTickCount_valueChanged(int arg1)//小分隔個數 { curAxis->setMinorTickCount(arg1); } void MainWindow::on_radioX_clicked()//獲取當前座標軸 { if (ui->radioX->isChecked()) curAxis=(QValueAxis*)ui->chartView->chart()->axisX(); else curAxis=(QValueAxis*)ui->chartView->chart()->axisY(); //獲取座標軸的各種屬性,顯示到介面上 ui->spinAxisMin->setValue(curAxis->min()); ui->spinAxisMax->setValue(curAxis->max()); ui->editAxisTitle->setText(curAxis->titleText()); ui->chkBoxAxisTitle->setChecked(curAxis->isTitleVisible()); ui->editAxisLabelFormat->setText(curAxis->labelFormat()); ui->chkBoxLabelsVisible->setChecked(curAxis->labelsVisible()); ui->chkGridLineVisible->setChecked(curAxis->isGridLineVisible()); ui->chkAxisLineVisible->setChecked(curAxis->isLineVisible()); ui->spinTickCount->setValue(curAxis->tickCount()); ui->chkAxisLineVisible->setChecked(curAxis->isLineVisible()); ui->spinMinorTickCount->setValue(curAxis->minorTickCount()); ui->chkMinorTickVisible->setChecked(curAxis->isMinorGridLineVisible()); } void MainWindow::on_radioY_clicked()//同x座標軸 { on_radioX_clicked(); } void MainWindow::on_chkBoxVisible_clicked(bool checked)//座標軸是否可見 { curAxis->setVisible(checked); } void MainWindow::on_chkGridLineVisible_clicked(bool checked)//軸的網格線是否可見 { curAxis->setGridLineVisible(checked); } void MainWindow::on_chkMinorTickVisible_clicked(bool checked)//次級刻度是否可見 { curAxis->setMinorGridLineVisible(checked); } void MainWindow::on_radioButton_clicked()//圖例的位置上 { ui->chartView->chart()->legend()->setAlignment(Qt::AlignTop); } void MainWindow::on_radioButton_2_clicked()//圖例的位置下 { ui->chartView->chart()->legend()->setAlignment(Qt::AlignBottom); } void MainWindow::on_radioButton_3_clicked()//圖例的位置左 { ui->chartView->chart()->legend()->setAlignment(Qt::AlignLeft); } void MainWindow::on_radioButton_4_clicked()//圖例的位置右 { ui->chartView->chart()->legend()->setAlignment(Qt::AlignRight); } void MainWindow::on_btnLegendFont_clicked()//圖例字型 { QFont font=ui->chartView->chart()->legend()->font(); bool ok=false; font=QFontDialog::getFont(&ok,font); if(ok) { ui->chartView->chart()->legend()->setFont(font); } } void MainWindow::on_btnLegendlabelColor_clicked()//圖例顏色 { QColor color=ui->chartView->chart()->legend()->color(); color=QColorDialog::getColor(color); if(color.isValid()) { ui->chartView->chart()->legend()->setColor(color); } } void MainWindow::on_btnAxisSetTitle_clicked()//座標軸標題 { QString str=ui->editAxisTitle->text(); curAxis->setTitleText(str); } void MainWindow::on_btnAxisSetTitleFont_clicked()//座標軸字型 { QFont font=curAxis->titleFont(); bool ok=false; font=QFontDialog::getFont(&ok,font); if(ok) { curAxis->setTitleFont(font); } } void MainWindow::on_chkBoxAxisTitle_clicked(bool checked)//軸標題是否可見 { curAxis->setTitleVisible(checked); } void MainWindow::on_pushButton_clicked()//設定座標軸刻度標籤的文字格式 { curAxis->setLabelFormat(ui->editAxisLabelFormat->text()); } void MainWindow::on_btnAxisLabelColor_clicked()//標籤文字顏色 { QColor color=curAxis->labelsColor(); color=QColorDialog::getColor(color); if (color.isValid()) curAxis->setLabelsColor(color); } void MainWindow::on_btnAxisLabelFont_clicked()//標籤文字字型 { QFont font=curAxis->labelsFont(); bool ok=false; font=QFontDialog::getFont(&ok,font); if(ok) { curAxis->setLabelsFont(font); } } void MainWindow::on_btnGridLineColor_clicked()//網格線顏色 { QColor color=curAxis->gridLineColor(); color=QColorDialog::getColor(color); if (color.isValid()) curAxis->setGridLineColor(color); } void MainWindow::on_pushButton_10_clicked()//網格線pen { QPen pen; pen=curAxis->gridLinePen(); bool ok=false; pen=QWDialogPen::getPen(pen,ok); if (ok) curAxis->setGridLinePen(pen); } void MainWindow::on_chkAxisLineVisible_clicked(bool checked)//刻度是否可見 { curAxis->setLineVisible(checked); } void MainWindow::on_btnAxisLinePen_clicked()//linepen { QPen pen; pen=curAxis->linePen(); bool ok=false; pen=QWDialogPen::getPen(pen,ok); if (ok) curAxis->setLinePen(pen); } void MainWindow::on_btnAxisLinePenColor_clicked()//LinePenColor { QColor color=curAxis->linePenColor(); color=QColorDialog::getColor(color); if (color.isValid()) curAxis->setLinePenColor(color); } void MainWindow::on_btnMinorColor_clicked()//次級網格顏色 { QColor color=curAxis->minorGridLineColor(); color=QColorDialog::getColor(color); if (color.isValid()) curAxis->setMinorGridLineColor(color); } void MainWindow::on_btnMinorPen_clicked()//次級網格線pen { QPen pen; pen=curAxis->minorGridLinePen(); bool ok=false; pen=QWDialogPen::getPen(pen,ok); if (ok) curAxis->setMinorGridLinePen(pen); } void MainWindow::on_radioSeries0_clicked(bool checked)//獲取當前資料序列 { } void MainWindow::on_radioSeries1_clicked() { on_radioSeries0_clicked(); } void MainWindow::on_radioSeries0_clicked() { if (ui->radioSeries0->isChecked()) curSeries=(QLineSeries *)ui->chartView->chart()->series().at(0); else curSeries=(QLineSeries *)ui->chartView->chart()->series().at(1); //獲取序列的屬性值,並顯示到介面上 ui->editSeriesName->setText(curSeries->name()); ui->chkSeriesVisible->setChecked(curSeries->isVisible()); ui->chkPointVisible->setChecked(curSeries->pointsVisible()); ui->sliderSeriesOpacity->setValue(curSeries->opacity()*10); ui->chkPointLabelVisible->setChecked(curSeries->pointLabelsVisible()); } void MainWindow::on_chkSeriesVisible_clicked(bool checked)//序列可見 { this->curSeries->setVisible(checked); } void MainWindow::on_btnSeriesPen_clicked()//序列Pen { QPen pen; pen=curSeries->pen(); bool ok=false; pen=QWDialogPen::getPen(pen,ok); if (ok) curSeries->setPen(pen); } void MainWindow::on_sliderSeriesOpacity_valueChanged(int value)//透明度 { curSeries->setOpacity(value/10.0); } void MainWindow::on_btnSeriesLabColor_clicked()//序列點標籤顏色 { QColor color=curSeries->pointLabelsColor(); color=QColorDialog::getColor(color); if (color.isValid()) curSeries->setPointLabelsColor(color); } void MainWindow::on_btnSeriesLabFont_clicked()//序列點標籤字型 { QFont font; font=curSeries->pointLabelsFont(); bool ok=false; font=QFontDialog::getFont(&ok,font); if (ok) curSeries->setPointLabelsFont(font); } void MainWindow::on_radioSeriesLabFormat0_clicked()//格式 { curSeries->setPointLabelsFormat("@yPoint"); } void MainWindow::on_radioSeriesLabFormat1_clicked()//格式 { curSeries->setPointLabelsFormat("@xPoint,@yPoint"); } void MainWindow::on_cBoxAnimation_currentIndexChanged(int index)//動畫 { ui->chartView->chart()->setAnimationOptions(QChart::AnimationOptions(index)); } void MainWindow::on_cBoxTheme_currentIndexChanged(int index)//主題 { ui->chartView->chart()->setTheme(QChart::ChartTheme(index)); } void MainWindow::on_actZoomIn_triggered()//放大 { ui->chartView->chart()->zoom(1.2); } void MainWindow::on_actZoomOut_triggered()//縮小 { ui->chartView->chart()->zoom(0.8); } void MainWindow::on_actZoomReset_triggered()//復原 { ui->chartView->chart()->zoomReset(); }
qwdialogpen.cpp
#include "qwdialogpen.h"
#include "ui_qwdialogpen.h"
QWDialogPen::QWDialogPen(QWidget *parent) :
QDialog(parent),
ui(new Ui::QWDialogPen)
{
ui->setupUi(this);
ui->comboBox->clear();
ui->comboBox->addItem("SolidLine",1);
ui->comboBox->addItem("DashLine",2);
ui->comboBox->addItem("DotLine",3);
ui->comboBox->setCurrentIndex(1);
}
QWDialogPen::~QWDialogPen()
{
delete ui;
}
/*
* Qt的標準對話方塊一般都有靜態函式
* 使用靜態函式無需管理對話方塊的建立和刪除
*/
/**
* @brief QWDialogPen::getPen
* @param inipen
* @param ok
* @return
*/
QPen QWDialogPen::getPen(QPen inipen,bool &ok)
{
QWDialogPen *dlg=new QWDialogPen;
dlg->setPen(inipen);//設定初始化QPen
QPen pen;
int ret=dlg->exec();//彈出對話方塊
if(ret==QDialog::Accepted)
{
pen=dlg->getPen();
ok=true;
}
else
{
pen=inipen;
ok=false;
}
delete dlg;//刪除對話方塊
return pen;//返回QPen
}
/**
* @brief QWDialogPen::setPen
* @param pen
*/
void QWDialogPen::setPen(QPen pen)//設定QPen,並重新整理顯示介面
{
m_pen=pen;
ui->spinBox->setValue(pen.width());//設定線寬初始值為現在QPen的線寬
int i=static_cast<int>(pen.style());//獲取線形並轉換為索引值
ui->comboBox->setCurrentIndex(i);//comboBox賦值
QColor color;
color=pen.color();
//設定按鈕背景色
ui->pushButton->setAutoFillBackground(true);
QString str=QString::asprintf("background-color:rgb(%d,%d,%d);",color.red(),color.green(),color.blue());
ui->pushButton->setStyleSheet(str);
}
/**
* @brief QWDialogPen::getPen
* @return
*/
QPen QWDialogPen::getPen()//獲得設定的屬性
{
m_pen.setWidth(ui->spinBox->value());//線寬
m_pen.setStyle(Qt::PenStyle(ui->comboBox->currentIndex()));//線形
//顏色
QColor color=ui->pushButton->palette().color(QPalette::Button);
m_pen.setColor(color);
return m_pen;
}
執行結果