C++ std::ostream到QString?
有没有办法将std::ostream转换为QString 如果有用,请允许我展开: 我正在用C++/Qt编写一个程序,我过去(不是)只使用std::cout来处理/调试异常,例如:C++ std::ostream到QString?,c++,qt,exception,C++,Qt,Exception,有没有办法将std::ostream转换为QString 如果有用,请允许我展开: 我正在用C++/Qt编写一个程序,我过去(不是)只使用std::cout来处理/调试异常,例如: std::cout << "Error in void Cat::eat(const Bird &bird): bird has negative weight" << std::endl; 我的问题是,我一直在重载运算符您可以使用如下方法: std::stringstream ou
std::cout << "Error in void Cat::eat(const Bird &bird): bird has negative weight" << std::endl;
我的问题是,我一直在重载运算符您可以使用如下方法:
std::stringstream out;
// ^^^^^^
out << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
throw(QString::fromStdString(out.str()));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
std::stringstream out;
// ^^^^^^
outstd::stringstream
类可以接收重载的输入尝试从字符串部分创建一个std::stringstream
,然后在QString
的构造函数中使用ss.str()
?即throw(QString(ss.str())
应该是QString::fromStdString(ss.str())
-抱歉
std::cout << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
std::ostream out;
out << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
throw(QString(out));
std::stringstream out;
// ^^^^^^
out << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
throw(QString::fromStdString(out.str()));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <sstream>
#include <QtCore/QString>
int main() {
int value=2;
std::stringstream myError;
myError << "Here is the beginning error text, and a Bird: " << value;
throw(QString::fromStdString(myError.str()));
}