0

我想从文本文件中读取字符串(数据)并将数据写入 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 方法时,使用文件“/1_geo.m4”的方法“QString::indexOf”和“QString::mid”搜索了 QString“Value”,我证明了它的存在QFileInfo::exists() 没有写入 QDoubleSpinBox“ValueQDoubleSpinBox”。有人可以告诉我为什么它不起作用吗?问候

4

1 回答 1

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

于 2012-03-07T14:44:15.653 回答