无法读取我的地图数据

时间:2022-08-07 23:07:21

i have a problem with reading my map data or possibly even writing it in.

我在阅读地图数据时遇到问题,甚至可能写入。

the app i'm making is a basic currency converter.

我正在制作的应用程序是基本的货币转换器。

I download the exchange rates from a website, save it into txt file, which i then parse to save the values into a map and use my calculator class to do the conversion.

我从网站下载汇率,将其保存到txt文件中,然后我将其解析以将值保存到地图中并使用我的计算器类进行转换。

the downloader class works flawless (i got it of official QT forums) so i wont post it since the problem aint there.

下载程序类完美无缺(我得到了官方QT论坛)所以我不会发布它,因为那里的问题。

the code: main.cpp

代码:main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMap<QString,double> currency_map;

    downloader d;
    d.Do_download();

    parser p;
    p.read_line(currency_map);

    p.print_map(currency_map);// this line works, and it prints out the map

    MainWindow w(currency_map);
    w.show();

    return a.exec();
};

parser.cpp im pretty sure it works well because the print_map function does its job.

parser.cpp我非常确定它运行良好,因为print_map函数可以完成它的工作。

void parser::process_line(QString line, QMap<QString, double> &my_map)
{

    QStringList temporary_list;

    for(int i = 0; i< currency_list.size();i++)
        {
        if(line.contains(currency_list.at(i),Qt::CaseInsensitive))
            {

            temporary_list=line.split(" ",QString::SkipEmptyParts);

            temporary_list.replaceInStrings(",",".");
            my_map.insert(currency_list.at(i),temporary_list[6].toDouble());
        }
    }

}

int parser::read_line(QMap<QString, double> &my_map)
{

    QFile file("C:/Qt/test/downloaded.txt");

    if(!file.exists())
        {
        QMessageBox msgBox;
        msgBox.setText("There is no such file");
        msgBox.exec();
        return 1;
    }    
    if(!file.open(QIODevice::ReadOnly  | QIODevice::Text))
    {
        QMessageBox msgBox;
        msgBox.setText("Error while opening file");
        msgBox.exec();
        return 1;
    }

    QTextStream in_stream(&file);
    QString line=in_stream.readLine();

    while (!line.isNull())
        {
        process_line(line, my_map);
        line = in_stream.readLine();
    }
    return 0;
}

void parser::print_map(QMap<QString, double> &my_map)
{
   QMapIterator<QString, double> i(my_map);
    while(i.hasNext())
        {
        i.next();
        qDebug()<< i.key() << ": " << i.value();
    }   
}

now i have a calculator class :

现在我有一个计算器类:

.h

class Calculator
{
public:
    explicit Calculator(QMap<QString,double> &currency_map);
    void multiply();
    void getValues(QString strFrom, QString strTo);
    double getTotal();
private:
    double total, firstCurr, secondCurr;
    QMap<QString,double> &map;

};

.cpp

#include "calculator.h"

Calculator::Calculator(QMap<QString,double> &currency_map):map(currency_map)
{
    total = 0;
    firstCurr = 0;
    secondCurr= 0;
}

void Calculator::getValues(QString strFrom, QString strTo)
{
    QMap<QString, double>::iterator i;
    for(i=map.begin();i!=map.end();i++)
        {

            if(!i.key().compare(strFrom))
                firstCurr=i.value();
            if(!i.key().compare(strTo))
                secondCurr = i.value();
    }
        //firstCurr = 2;
        //secondCurr = 3;
}

void Calculator::multiply()
{
    total = firstCurr * secondCurr;
}

double Calculator::getTotal()
{
    return total;
}

then i create a Calculator object in my mainWindow class .h

然后我在mainWindow类.h中创建一个Calculator对象

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MainWindow(QMap<QString,double> &currency_map, QWidget *parent = 0);

        ~MainWindow();


    private slots:
        void on_convert_button_clicked();

    private:
        Ui::MainWindow *ui;
        Calculator calc;

    };

.cpp

MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),calc(currency_map)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_convert_button_clicked()
{
    calc.getValues(ui->from_Combox->currentText(),ui->to_Combox->currentText());
    calc.multiply();
    ui->lcdNumber->display(calc.getTotal());
}

but i cant seem to get any values from the map. the wierd thing also is when i debugg(i use visual studio) its always shows the map as empty, which i cant grasp since the print function works.

但我似乎无法从地图中获取任何值。奇怪的是,当我调试(我使用visual studio)时,它总是将地图显示为空,由于打印功能起作用,我无法掌握。

any help would be appreciated. thx

任何帮助,将不胜感激。谢谢

1 个解决方案

#1


0  

Your calculator has a member variable which is a reference to a QMap of currency values. If the QMap it references goes out of scope, then the reference becomes invalid. Change the declaration of the member variable in your calculator class to this:

您的计算器有一个成员变量,它是对QMap货币值的引用。如果它引用的QMap超出范围,则引用变为无效。将计算器类中成员变量的声明更改为:

QMap<QString,double> map;

This will copy the map instead of making a reference to it. I would also put a breakpoint in the calculator constructor and verify that the map still has valid data at that point.

这将复制地图而不是引用它。我还会在计算器构造函数中放置一个断点,并验证该映射在该点仍然具有有效数据。

#1


0  

Your calculator has a member variable which is a reference to a QMap of currency values. If the QMap it references goes out of scope, then the reference becomes invalid. Change the declaration of the member variable in your calculator class to this:

您的计算器有一个成员变量,它是对QMap货币值的引用。如果它引用的QMap超出范围,则引用变为无效。将计算器类中成员变量的声明更改为:

QMap<QString,double> map;

This will copy the map instead of making a reference to it. I would also put a breakpoint in the calculator constructor and verify that the map still has valid data at that point.

这将复制地图而不是引用它。我还会在计算器构造函数中放置一个断点,并验证该映射在该点仍然具有有效数据。