2

I'm working on a windows program using MFC, currently I use std::string and std::wstring, the problems are I have to convert them from each other and handle encoding everywhere.

The QString API looks very decent, it can handle those problems just in one class, so my questions are:

  1. Can I use only QString without depending on whole QT library?
  2. Is there alternatives (A class handling both char and wchar, encoding, etc.)
8
  • 1
    You'll have to use QtCore to be able to use QString. That's the smallest library that I think can be built that has no other Qt dependencies. Commented Aug 2, 2019 at 5:04
  • 1
    I hope you already considered using the CString class which is already there in MFC. Commented Aug 2, 2019 at 6:24
  • @dorKKnight Seems that CString also has CStringA and CStringW underlying Commented Aug 2, 2019 at 6:29
  • 1
    Always use wide strings. Commented Aug 2, 2019 at 6:34
  • @MichaelChourdakis How to always use? 1) std::exception does not accept std::wstring, 2) Some library uses std::string. 3) Even winapi like GetProcAddress uses char* not wchar_t* Commented Aug 2, 2019 at 7:02

2 Answers 2

2
  1. Can I use only QString without depending on whole QT library? Yes you can. You havn't to use other Qt libraries.

  2. QString has a lot useful functions to convert the QString into a other type, like:

    std::wstring wString;
    wString = 'w';
    QString qString = QString::fromStdWString(wString);
    std::string sString = qString.toStdString();
    const char * cP = sString.c_str();
    qString = cP;
    wString = qString.toStdWString();

Sign up to request clarification or add additional context in comments.

Comments

1

MFC has its own implementation of the string class: CString https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/using-cstring?view=vs-2019

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.