我想从文本文件中读取字符串(数据)并将数据写入QDoubleSpinBox。因此我使用了:
void GUIsubclassKuehniGUI::LoadDirectory()
{
QString loadedDirectory = QFileDialog::getExistingDirectory(this,
"/home",tr("Create Directory"),
QFileDialog::DontResolveSymlinks);
ui.PathDirectory -> setText(loadedDirectory);
QFileInfo GeoDat1 = loadedDirectory + "/1_geo.m4";
QFileInfo GeoDat2 = loadedDirectory + "/2_geo.m4";
QString Value;
if (GeoDat1.exists() == true)
{
QFile GEO (loadedDirectory + "/1_geo.m4");
if(GEO.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream Stream (&GEO);
QString Text;
do
{
Text = Stream.readLine();
QString startWith = "start";
QString endWith = "stop" ;
int start = Text.indexOf(startWith, 0, Qt::CaseInsensitive);
int end = Text.indexOf(endWith, Qt::CaseInsensitive);
if (start != -1)
Value = Text.mid(start + startWith.length(), end - ( start + startWith.length() ) );
qDebug() << Value << (start + startWith.length()) << (end - (start + startWith.length()));
double ValueNumber = Value.toDouble();
ValueNumber = ui.ValueQDoubleSpinBox->value();
}
while(!Text.isNull());
GEO.close();
}
}
else if (GeoDat2.exists() == true)
{
...
}
}
编译时我没有收到任何错误信息,但是当我使用方法LoadDirectory时,QString“Value”搜索文件“/1_geo.m4”的方法“QString :: indexOf”和“QString :: mid”存在我用QFileInfo :: exists()证明没有写入QDoubleSpinBox“ValueQDoubleSpinBox”。有人能告诉我它为什么不起作用吗?问候
答案 0 :(得分:3)
恕我直言以下几行:
double ValueNumber = Value.toDouble();
ValueNumber = ui.ValueQDoubleSpinBox->value(); // get value from spinbox
必须更改为:
double ValueNumber = Value.toDouble();
ui.ValueQDoubleSpinBox->setValue(ValueNumber); // set value of spinbox
详细信息:http://qt-project.org/doc/qt-4.8/qdoublespinbox.html#value-prop