문자열을 QString으로 변경하는 방법은 무엇입니까?
가장 기본적인 방법은 무엇입니까?
문자열로 말하면 std::string
이 방법으로 할 수 있음을 의미 합니다.
QString QString :: fromStdString (const 표준 :: 문자열 및 str)
std::string str = "Hello world";
QString qstr = QString::fromStdString(str);
문자열로 Ascii 인코딩 const char *
을 의미 하는 경우이 방법을 사용할 수 있습니다.
QString QString :: fromAscii (const char * str, int 크기 = -1)
const char* str = "Hello world";
QString qstr = QString::fromAscii(str);
당신이 경우 const char *
읽을 수 있습니다 시스템 인코딩으로 인코딩 QTextCodec :: codecForLocale () 당신은이 방법을 사용한다 :
QString QString :: fromLocal8Bit (const char * str, int 크기 = -1)
const char* str = "zażółć gęślą jaźń"; // latin2 source file and system encoding
QString qstr = QString::fromLocal8Bit(str);
당신이있는 경우 const char *
의 UTF8 다음 인코딩 된 것으로이 방법을 사용해야합니다 :
QString QString :: fromUtf8 (const char * str, int 크기 = -1)
const char* str = read_raw("hello.txt"); // assuming hello.txt is UTF8 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const char*
QString qstr = QString::fromUtf8(str);
const ushort *
UTF16 인코딩 문자열 을 포함하는 방법도 있습니다 .
QString QString :: fromUtf16 (const ushort * 유니 코드, int 크기 = -1)
const ushort* str = read_raw("hello.txt"); // assuming hello.txt is UTF16 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const ushort*
QString qstr = QString::fromUtf16(str);
STL 호환성을 컴파일하는 경우, QString
가 정적 메서드 을 변환하는 std::string
A를을 QString
:
std::string str = "abc";
QString qstr = QString::fromStdString(str);
다른 방법 :
std::string s = "This is an STL string";
QString qs = QString::fromAscii(s.data(), s.size());
이것은 끝에 추가 할 장소가없는 경우 자체를 복사 .c_str()
할 수있는 사용하지 않는 이점 이 있습니다 .std::string
'\0'
std::string s = "Sambuca";
QString q = s.c_str();
경고 :에 가 std::string
포함되어 있으면 작동하지 않습니다 \0
.
답변을 따를 때 문제가 발생 하여이 질문을 보았으므로 여기에 솔루션을 게시하십시오.
위의 예제는 모두 ASCII 값만 포함하는 문자열이있는 샘플을 보여줍니다.이 경우 모든 것이 제대로 작동합니다. 그러나 Windows에서 문자열을 처리 할 때 whcih는 독일어 움라우트와 같은 다른 문자를 포함 할 수 있습니다. 이러한 솔루션은 작동하지 않습니다
이러한 경우 올바른 결과를 제공하는 유일한 코드는
std::string s = "Übernahme";
QString q = QString::fromLocal8Bit(s.c_str());
그러한 문자열을 다룰 필요가 없다면 위의 답변이 잘 작동합니다.
또한 원하는 것을 변환하기 위해 QVariant 클래스를 사용할 수 있습니다.
예를 들면 다음과 같습니다.
std::string str("hello !");
qDebug() << QVariant(str.c_str()).toString();
int test = 10;
double titi = 5.42;
qDebug() << QVariant(test).toString();
qDebug() << QVariant(titi).toString();
qDebug() << QVariant(titi).toInt();
산출
"hello !"
"10"
"5.42"
5
문자열에서와 같이 C 문자열 char*
또는 C ++ std::string
개체를 의미합니까?
어느 쪽이든 QT 참조에 설명 된 것과 동일한 생성자를 사용합니다.
일반 C 문자열의 경우 기본 생성자를 사용하십시오.
char name[] = "Stack Overflow";
QString qname(name);
의 경우 버퍼 std::string
를 가져 와서 생성자 char*
에게 전달합니다 QString
.
std::string name2("Stack Overflow");
QString qname2(name2.c_str());
참고 URL : https://stackoverflow.com/questions/1814189/how-to-change-string-into-qstring
'IT' 카테고리의 다른 글
java.lang.NullPointerException : null 객체 참조에서 가상 메소드 'int android.view.View.getImportantForAccessibility ()'를 호출하려고 시도했습니다. (0) | 2020.06.24 |
---|---|
더 작은 경우 UIScrollView의 컨텐츠 중심 (0) | 2020.06.24 |
파이썬에서 열거 형에 대한 일반적인 관행은 무엇입니까? (0) | 2020.06.24 |
“com.google.firebase.provider.FirebaseInitProvider”클래스를 찾지 못했습니까? (0) | 2020.06.24 |
HTML5 양식 유효성 검사 기본 오류 메시지를 어떻게 변경하거나 제거 할 수 있습니까? (0) | 2020.06.23 |