将 QString 转换为 std::string [英] Converting QString to std::string

查看:62
本文介绍了将 QString 转换为 std::string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过其他几篇关于将 QString 转换为 std::string 的帖子,应该很简单.但不知何故,我收到了一个错误.

我的代码是用 cmake 编译成 VS 项目的(我用的是 VS express),所以 QT 库没有问题,我写的 GUI 除了这部分也能用.

我有一个 QComboBox cb 保存某些对象的名称,还有一个 QLineEdit lineEdit 允许我指定我要查找的对象的名称.当我按下 go 按钮时,它应该运行一个经过测试并正常工作的函数,并将来自 QComboBoxlineEdit 的输入作为参数.

这是点击 go 按钮时的代码:

void gui::on_go_clicked(){std::string str(cb->currentText().toStdString());//std::cout <<字符串<<"\n";//QString qstr = lineEdit->text();//std::cout<text().toStdString();updateDB(str, lineEdit->text().toStdString());}

第一行,创建 str,工作正常.IE.库函数或 toStdString() 没有问题.但是当它执行我的函数时,程序崩溃了,这不是因为函数,而是因为它试图转换 lineEdit->text().toStdString() 的部分.>

这只是当我在 lineEdit 框中写下test"这个词时.我已经看到其他关于 unicode 的答案,我曾简单尝试过,但我可以假设用户不会在 lineEdit 框中放置任何特殊字符,除了_"和.",这不应该是 unicode.

解决方案

第一行,创建 str,工作正常.IE.没有问题库函数或 toStdString().但是当它执行我的功能时,程序崩溃了,不是因为功能,而是因为它尝试转换 lineEdit->text().toStdString().

的部分

简化您的测试并验证 QString.toStdString() 是否符合您的预期:

因此:

QString text = "用户输入的精确文本";std::cout <<text.toStdString() <<std::endl;//或者...qDebug() <<text.toStdString().c_str();

是否产生了预期的结果?

如果是这种情况,则说明您的函数有问题.updateDB 看起来如何(函数签名?)?它的输入是什么?

来自 Qt 帮助文件:

<块引用>

使用 toUtf8() 函数将 Unicode 数据转换为 8 位字符.

因此,如果您没有更改小部件的区域设置 (lineEdit) 或它的父部件,那么事情应该可以正常工作(或者您应该看到一些信息丢失的东西).这个功能我用过很多次都没有问题...

I've seen several other posts about converting QString to std::string, and it should be simple. But somehow I'm getting an error.

My code is compiled into a VS project using cmake (I'm using VS express), so there's no issue with the QT libraries, and the GUI that I wrote works besides this part.

I have a QComboBox cb that holds the names to some objects, and a QLineEdit lineEdit that allows me to specify the name of the object that I am looking for. It should run a function that is tested and working when I press a go button, with the input from the QComboBox and the lineEdit as arguments.

Here's the code for when the go button is clicked:

void gui::on_go_clicked(){
    std::string str(cb->currentText().toStdString());
    //std::cout << str << "\n";
    //QString qstr = lineEdit->text();
    //std::cout<<lineEdit->text().toStdString();
    updateDB(str, lineEdit->text().toStdString());
}

The first line, creating str, works fine. I.E. there's no problem with library functions or toStdString(). But when it executes my function, the program breaks, and it's not becuase of the function, it's because of the part where it tries to convert lineEdit->text().toStdString().

This is only when I write the word "test" in the lineEdit box. I've seen other answers talking about unicode, which I tried briefly, but I can assume that the user will not be putting any special characters in the lineEdit box, barring '_' and '.', which shouldn't be unicode.

解决方案

The first line, creating str, works fine. I.E. there's no problem with library functions or toStdString(). But when it executes my function, the program breaks, and it's not becuase of the function, it's because of the part where it tries to convert lineEdit->text().toStdString().

Simplify your test and verify that QString.toStdString() does what you expect:

Therefore:

QString text = "Precise text that user would input";
std::cout << text.toStdString() << std::endl; //or...
qDebug() << text.toStdString().c_str();

Is the expected result produced?

If this is the case, it means your function has a problem. How does the updateDB look (function signature?)? What are its inputs?

From Qt help files:

The Unicode data is converted into 8-bit characters using the toUtf8() function.

Therefore, if you haven't changed the locale of your widget (lineEdit) or it's parents, things should just work (or you should see something with information loss). I've used this function many times without trouble...

这篇关于将 QString 转换为 std::string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆