Qt代码片段

DrawText写竖排文字

painter.drawText(10,50,30,150,Qt::AlignTop | Qt::TextSingleLine | Qt::TextWordWrap,"测试文字");

painter.save();//显将画笔属性进行保存
painter.translate(100,100);//将原点移动到要画文字的地方
painter.rotate(90);//将画笔翻转90度
painter.drawText(0,0,"测试文字");//绘制文字内容
painter.restore();//恢复画笔原有属性

串口相关的代码块

//添加模块
QT       += core gui serialport 
// 查找可用的串口
foreach(const QSerialPortInfo &inf0, QSerialPortInfo::availablePorts())
{
    ui->comboBox_port->addItem(inf0.portName());
}

// 用到的类有QSerialPort   QSerialPortInfo
serialPort->setPortName(ui->comboBox_port->currentText());
bool ok = serialPort->open(QIODevice::ReadWrite);


serialPort->setBaudRate(ui->comboBox_bit->currentText().toInt());
serialPort->setStopBits(QSerialPort::OneStop);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);

serialPort.close();

// 数据读写

qint64 readData(char *data, qint64 maxSize) override;
qint64 readLineData(char *data, qint64 maxSize) override;
qint64 writeData(const char *data, qint64 maxSize) override;

Qt绘制折线图

   QChartView *charView = new QChartView(this);
    QChart *chart = new QChart();
    //chart->setTitle(u8"支架位移");
    charView->setChart(chart);
    QDateTimeAxis *xAxis = new QDateTimeAxis;
    xAxis->setFormat("MM:dd hh:mm");
    QDateTime temp_StartTime(QDate(2024, 1, 11), QTime(0, 0, 0)); //前面是年月日,后面是小时、分钟、秒
    QDateTime temp_EndTime(QDate(2024, 1, 13), QTime(0, 0, 0));//
    xAxis->setRange(temp_StartTime, temp_EndTime);
    xAxis->setTickCount(7);
    QValueAxis *yAxis = new QValueAxis;
    yAxis->setRange(0, 150);
    yAxis->setTickCount(7);
    // 创建折线系列
    QLineSeries *series = new QLineSeries;
    //series->setMarkerSize(5);
    int y = 0;
    for(int i = 1; i <= 60 * 24 * 2; i++)
    {
        qint64  time = temp_StartTime.toMSecsSinceEpoch();
        time +=  i * 60 * 1000;
        // qDebug() << time;
        y++;
        if(y > 150) { y = 0; }
        series->append(time, y);
    }
    // 将系列添加到图表
    chart->addAxis(xAxis, Qt::AlignBottom);
    chart->addAxis(yAxis, Qt::AlignLeft);
    chart->addSeries(series);
    series->attachAxis(xAxis);
    series->attachAxis(yAxis);
    charView->resize(1000, 800);

Qt 计算未来几天和过去几天额时间

QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString str = time.toString("yyyy-MM-dd"); //设置显示格式
QString BeforeDaystr=time.addDays(-1).toString("yyyy-mm-dd");//获取前一天时间

QString BeforeDaystr=time.addDays(+4).toString("yyyy-mm-dd");//将-1更改为+4,获得的时间为当前时间四天后的时间
#转载于https://www.cnblogs.com/ruandahua/p/10916866.html
posted @ 2023-12-23 14:05  huameixiao  阅读(38)  评论(0编辑  收藏  举报