四、QToolBar 工具栏

QToolBar *pToolbar;  
pToolbar = this->addToolBar(QStringLiteral("toolBar"));

pToolbar->setStyleSheet("background-color:#F0F8FF");

QList<QAction *> actions;

actions.append(pNew);

actions.append(pClose);

pToolbar->addActions(actions);


五、串口常用操作

1.查找可用的串口

foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())

    {

        QSerialPort serial;

        serial.setPort(info);                           //设置串口

        if(serial.open(QIODevice::ReadWrite))

        {

            cb_Port->addItem(serial.portName());        //显示串口name

            serial.close();

        }

    }

 

六、绘制曲线图

1.需要在项目.pro中添加 QT + = charts

2.需要用到的

QChart

QChartView

QValueAxis 坐标轴

QSplineSeries

QDataTimeAxis 时间轴 如果想要以时间为坐标轴的话就可直接用此类

3.创建坐标系(chart、X轴、Y轴)

QTimer *timerDrawLine;

QChart *chart;

QSplineSeries *lineSeries;

QSerialPort *serialPort;

QTimer *timer;

QTimer *timersend;


//初始化QChart的实例

chart = new QChart();

//初始化QSplineSeries的实例  
lineSeries = new QSplineSeries();

//设置曲线的名称

lineSeries->setName("随机数测试曲线");

//把曲线添加到QChart的实例chart中

chart->addSeries(lineSeries);

//声明并初始化X轴、两个Y轴

QValueAxis *axisX = new QValueAxis();

QValueAxis *axisY = new QValueAxis();

//设置坐标轴显示的范围

axisX->setMin(0);

axisX->setMax(MAX_X);

axisY->setMin(0);

axisY->setMax(MAX_Y);

//设置坐标轴上的格点

axisX->setTickCount(10);

axisY->setTickCount(10);

//设置坐标轴显示的名称

QFont font("Microsoft YaHei",8,QFont::Normal);//微软雅黑。字体大小8

axisX->setTitleFont(font);

axisY->setTitleFont(font);

axisX->setTitleText("X-Test");

axisY->setTitleText("Y-Test");

//设置网格不显示

axisY->setGridLineVisible(false);

//下方:Qt::AlignBottom,左边:Qt::AlignLeft

//右边:Qt::AlignRight,上方:Qt::AlignTop

chart->addAxis(axisX, Qt::AlignBottom);

chart->addAxis(axisY, Qt::AlignLeft);

//把曲线关联到坐标轴

lineSeries->attachAxis(axisX);

lineSeries->attachAxis(axisY);

//把chart显示到窗口上

QChartView *graphicsView = new QChartView;

graphicsView->setChart(chart);

graphicsView->setRenderHint(QPainter::Antialiasing);  
QGridLayout *mainLayout = new QGridLayout;

mainLayout->addWidget(graphicsView);//将chart放入到布局中

this->setLayout(mainLayout);//设置该窗口的布局上面的网格布局

4.绘制曲线图案例(通过产生随机数的方式)

#define MAX_X   50

DrawLine()

{

    static int count = 0;

    if(count > MAX_X)

    {

        //当曲线上最早的点超出X轴的范围时,剔除最早的点,

        lineSeries->removePoints(0,lineSeries->count() - MAX_X);

        // 更新X轴的范围

        chart->axisX()->setMin(count - MAX_X);

        chart->axisX()->setMax(count);

    }

    //增加新的点到曲线末端

    lineSeries->append(count, rand()%65);//随机生成0到65的随机数

	//如果项绘制双曲线,还可以在创建一个y轴

	lineSeries1->append(count, rand()%25);//随机产生0到25的随机数

    count ++;

}


timerDrawLine = new QTimer(); //创建一个定时器

timerDrawLine->start(1000); //间隔1000毫秒绘制部分线

connect(timerDrawLine, SIGNAL(timeout()), this, SLOT(DrawLine()));//将画线函数与定时器连接起来


5.两条曲线时,点击按钮,显示/隐藏某一条曲线

//点击一下隐藏,再点击一下显示

void Widget::on_button2_clicked()

{

    static int res = 0;

    if(!res)

    {

        mSeriesT->hide(); //隐藏mSeriesT曲线

        res = 1;

    }

    else

    {

        mSeriesT->show();//显示mSeriesT曲线

        res = 0;

    }


}