[英]Qt doesn't read a string properly in Linux
好吧,我会尽力解释。 我有一个名为Calc的类,用于评估字符串或const char *数字表达式,例如“ 2 *(3/5)”或类似内容。
现在,我试图在Qt中构建一个计算器,使用这些类来评估QLineEdit的输入。
该类的工作方式如下:
Calc(const std::string &sExpresion);
Calc(const char* cExpresion);
和
void anadirExpresion(const std::string &sExpresion);
void anadirExpresion(const char* cExpresion);
我尝试像这样在Qt中使用它:
void Calculadora::evaluar()
{
QLocale locale;
QByteArray cadena = ui->pantalla->text().toLocal8Bit();
const char* expresionAEvaluar = cadena.data();
qDebug()<<"Cadena a evaluar: "<<cadena;
std::cout<<"Cadena: "<<expresionAEvaluar<<std::endl;
evaluador.anadirExpresion(expresionAEvaluar); //evaluador is the class Calc instance
float resultado=evaluador.CalcularExpresion();
qDebug()<<"Resultado: "<<resultado;
ui->pantalla->setText(QString::number(resultado));
}
这是尝试将Class与const char *表达式一起使用
这是尝试使用std :: string表达式
void Calculadora::evaluar()
{
//std::string cadena = ui->pantalla->text().toStdString();
//qDebug()<<"Cadena a evaluar: "<<ui->pantalla->text();
std::cout<<"Cadena: "<<ui->pantalla->text().toStdString()<<std::endl;
evaluador.anadirExpresion(ui->pantalla->text().toStdString());
float resultado=evaluador.CalcularExpresion();
qDebug()<<"Resultado: "<<resultado;
ui->pantalla->setText(QString::number(resultado));
}
问题是我在“。”之后丢失了数据。 字符。 如果我的计算公式为“ 2.3”,则计算器的计算结果为“ 2”。此外,此问题仅在Linux中有效,因为在Windows下的Qt中它可以正常运行。 它在Linux和Windows下的控制台模式下也可以正常工作。
同样,qDebug()和std :: cout在Qt控制台中的输出显示正确的字符串。
建议?
对不起,我的英语不好,谢谢你。
***更新:
最终,我认为该错误是因为在Linux下Qt下编译的函数atof()无法正常工作。 我听起来没什么意思,但是,我遵循“ 2.5”值来查找更改的位置,然后更改了该位置(成为Calc的成员函数)
EP[t].OP.operando=atof(aux);
std::cout<<"changed? "<<aux<<std::endl;
std::cout<<"changed: "<<EP[t].OP.operando<<std::endl;
在控制台下,atof()函数之后的EP [t] .OP.operando值为“ 2.5”。 在Windows中也是如此。 但是在Linux中,我得到“ 2”值。
现在我再试一次:
char cad[]="2.5";
EP[t].OP.operando=atof(cad);
std::cout<<"changed: "<<EP[t].OP.operando<<std::endl;
在控制台下,EP [t] .OP.operando值= 2.5,但在Qt下为2
也许是个错误? 有人确认吗?
谢谢
我怀疑语言环境会插入。您的系统语言环境可能是某种欧洲语言,其中小数点分隔符是逗号,而不是点。
要验证我的暂停状态,您可以:
,
)作为小数点分隔符,或 您可以在此变量的日志值中打印( 请参阅文档 ):
qDebug() << "Decimal separator: " << localeconv()->decimal_point;
如果我是对的,那么这不是错误,而是功能:)。
void MainWindow::processText(const QString &txt) { std::string s = txt.toStdString(); std::stringstream stream(s); double x; x = std::atof(s.c_str()); emit atofResultString(QString::number(x)); if (stream >> x) { emit stdstreamResult(QString::number(x)); } }
我已经在具有波兰语区域设置的Ubuntu 12.04上对此进行了测试。 结果是atof
考虑了系统区域设置。
完成后,您能否告诉我们evaluar()
函数是否工作正常:
QByteArray cadena = ui->pantalla->text().toLocal8Bit(); const char* expresionAEvaluar = cadena.data();
如果它正常工作,那么我建议您也对std::string
使用相同的函数。 这可以使用std::string::c_str()
。
因此您可以编写ui->pantalla->text().toStdString().c_str()
。
但是,当您使用toLocal8bit()
函数时,在QString
和const char*
之间进行转换效果最佳,因为您已经完成了一次。
谢谢您的回答。
我试图更改程序的环境,添加了QLocale指针,并将其添加到构造函数中:
valor = new QLocale (QLocale::C,QLocale::UnitedStates);//valor is a QLocale pointer
我已明确为我的国家更改了此值:
valor = new QLocale (QLocale::Spanish,QLocale::Spain);
我也尝试在“。”之间切换。 和“,”表示两种环境下的小数点(西班牙和美国)
在又一次头痛之后,您给了我一个主意,即从stringstream对象获取float valor,而不使用atof()函数。
std::stringstream ss(aux);
ss>>EP[t].OP.operando;
现在工作正常。 实际上,我还没有解决问题(语言环境问题),但是我解决了该问题。 我是否必须将帖子标记为“已解决”?
再次感谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.