1.Qt家目录
QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/Yeecoh/log.txt", //默认搜索路径(改为家目录相对路径)
"C:\\Users\\zwc11\\Yeecoh", //原默认搜索路径(绝对路径)
//QFile file("C:/Users/zwc11/Yeecoh/log.txt"); //工位绝对路径
//QFile file("%HOMEPATH%/Yeecoh/log,txt"); //使用家目录环境变量,读取失败
QFile file(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/Yeecoh/log.txt"); //选择要读取的文件(使用Qt家目录语法)
2.Qt正则表达式:QRegExp类
①exactMatch()函数
QRegExp rx("(.*Debug.*)|(.*Info.*)|(.*Warning.*)|(.*Critical.*)|(.*Fatal.*)");
QString str(line);
if(rx.exactMatch(str)) { //判断目标与正则表达式是否匹配
void LogWidget::on_pushButton_clicked()
{
QString displayString;
//QFile file("C:/Users/zwc11/Yeecoh/log.txt"); //工位绝对路径
//QFile file("%HOMEPATH%/Yeecoh/log,txt"); //使用家目录环境变量,读取失败
QFile file(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/Yeecoh/log.txt"); //选择要读取的文件(使用Qt家目录语法)
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
//qDebug()<<"Can't open the file!";
}
while(!file.atEnd())
{
QByteArray line = file.readLine();
QRegExp rx("(.*Debug.*)|(.*Info.*)|(.*Warning.*)|(.*Critical.*)|(.*Fatal.*)");
QString str(line);
if(rx.exactMatch(str)) { //判断目标与正则表达式是否匹配
//qDebug()<< str;
displayString.append(str);
}
else{
//什么也不做,不显示内容
}
}
ui->textEdit->clear();
ui->textEdit->setPlainText(displayString);
}
3.解决汉字乱码问题u8
qDebug()<<u8"连接打印机失败";
QString str = u8"测试";