Convert text in other encoding qt - qt

Alert: I am using QByteArray.
I want to ask about which other conversions exist. I am normally using toLatin1 but i would try with others. Example:
datoss = "|#|" + ui->textocuenta->text().toLatin1() + "|#|";
I repeat again, i am trying to use other conversion. Only that.

See QString class info. Convert your QByteArray to QString, there are multiple conversions:
CFStringRef toCFString() const
QString toCaseFolded() const
QString toHtmlEscaped() const
QByteArray toLatin1() const
QByteArray toLocal8Bit() const
NSString * toNSString() const
std::string toStdString() const
std::u16string toStdU16String() const
std::u32string toStdU32String() const
std::wstring toStdWString() const
ushort toUShort(bool *ok = Q_NULLPTR, int base = 10) const
QVector<uint> toUcs4() const
QByteArray toUtf8() const
int toWCharArray(wchar_t *array) const
If you are looking for a way to convert your indexed data from your QWidget in to a QByteArray, just use:
const QString indexed = QString("|#|%1|#|").arg(ui->textocuenta->text());
datoss = indexed.toLatin1();

Related

What JS objects are available in Qt Creator JS: variable expansion?

Qt Creator's wizards and configuration settings supports variable expansion, including evaluation of JavaScript expressions, e.g. the C++ class wizard file template contains:
%{JS: Cpp.openNamespaces('%{Class}')}
Cpp seems to be a global object. Where in the Qt Creator sources are those defined, and what's available?
These are not documented anywhere.
The JavaScript variable expansion is performed by the JsExpander in the core plugin. The expander can register QObject instances and expose them as properties of the global JS object.
To find all of those global objects, search for registerQObjectForJs method invocations. See the github search results for this method.
As of Qt Creator 4.2.1, and until at least 4.6, the following are the only objects registered:
Util - exposing Internal::UtilsJsExtension,
Cpp - exposing CppTools::Internal::CppToolsJsExtension,
Modeling - exposing ModelEditor::Internal::JsExtension,
QtSupport - exposing QtSupport::CodeGenerator,
Vcs - exposing VcsBase::Internal::VcsJsExtension.
The method parameter types are mapped to JavaScript types by QJSEngine. E.g. to obtain the Qt includes, one could have the following substitution:
%{JS: QtSupport.qtIncludes([ '%{Base}' ], [ '%{Base}' ])}
given the signature
QString qtIncludes(const QStringList &qt4, const QStringList &qt5)
The method list follows.
Util
QString toNativeSeparators(const QString &in) const;
QString fromNativeSeparators(const QString &in) const;
QString baseName(const QString &in) const;
QString fileName(const QString &in) const;
QString completeBaseName(const QString &in) const;
QString suffix(const QString &in) const;
QString completeSuffix(const QString &in) const;
QString path(const QString &in) const;
QString absoluteFilePath(const QString &in) const;
QString relativeFilePath(const QString &path, const QString &base) const;
// File checks:
bool exists(const QString &in) const;
bool isDirectory(const QString &in) const;
bool isFile(const QString &in) const;
// MimeDB:
QString preferredSuffix(const QString &mimetype) const;
// Generate filename:
QString fileName(const QString &path,
const QString &extension) const;
// Generate temporary file:
QString mktemp(const QString &pattern) const;
// Generate a ascii-only string:
QString asciify(const QString &input) const;
Cpp
// Generate header guard:
QString headerGuard(const QString &in) const;
// Fix the filename casing as configured in C++/File Naming:
QString fileName(const QString &path, const QString &extension) const;
// Work with classes:
QStringList namespaces(const QString &klass) const;
QString className(const QString &klass) const;
QString classToFileName(const QString &klass,
const QString &extension) const;
QString classToHeaderGuard(const QString &klass, const QString &extension) const;
QString openNamespaces(const QString &klass) const;
QString closeNamespaces(const QString &klass) const;
Modeling
QString fileNameToElementName(const QString &file);
QString elementNameToFileName(const QString &element);
QtSupport
// Ui file related:
// Change the class name in a UI XML form
QString changeUiClassName(const QString &uiXml, const QString &newUiClassName);
QString uiClassName(const QString &uiXml);
// Generic Qt:
QString qtIncludes(const QStringList &qt4, const QStringList &qt5);
Vcs
bool isConfigured(const QString &vcsId) const;
QString displayName(const QString &vcsId) const;
It is also useful to note that several JS variables exposed via the macroexpander see:
https://github.com/qt-creator/qt-creator/blob/master/src/plugins/coreplugin/coreplugin.cpp#L169

Using QList as the input for QwtPlotCurves setSamples

I have two QList (XList, YList) which saves the dynamic data. I want to use these as the input of the QwtPlotCurves by setSamples. After I check the documentation:
void setSamples (const double *xData, const double *yData, int size)
void setSamples (const QVector< double > &xData, const QVector< double > &yData)
void setSamples (const QVector< QPointF > &)
It seems do not support QList. Is there workaround to this or do I have to overload it?
Julio
There is method in QList that returns a const QVector.
So:
setSamples( XList.toVector(), YList.toVector() )
Check QVector QList::toVector () const

QString with special characters to const char*

I m trying to convert QString with special characters to const char* but I did not succeed. my function is:
void class::func(const QString& Name) // fileName = "â.tmp"
{
qDebug()<< Name; // display "â.tmp"
const char* cfileName = Name.toAscii().data();
qDebug() << cfileName; // display "a?.tmp"
}
qDebug()<< fileName display the true value that is "â.tmp" but after converting it to const a char*, I do not succeed to have the right value.
In the second time I try to use const char* cfileName = QString::fromUtf8(fileName.toAscii().data()); but I did not still have the right value, it display the same thing: "a?.tmp". How can I fix this thank you
due to convert QString to const char* :
QString str("hi lor!");
const char *s = str.toStdString().c_str();
msg.setText(QString::fromUtf8(s));
msg.exec();
EDIT: using QByteArray QString::toUtf8 () const is much better
QString string = "â.tmp";
const char* encodedString = string.toUtf8().data();
ORIGIONAL:
You probably need to use a codec, see http://qt-project.org/doc/qt-4.8/qtextcodec.html
something like this should work:
QString string = "â.tmp";
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QByteArray encodedString = codec->fromUnicode(string);
the documentation does not say what encoding types QDebug & QDebug::operator<< ( const char * s ) supports, it may be platform dependent, try verifying a correct conversion another. The problem may be in qDebug() or the stream it writes to.

How to use Qlist to create a user profile model with about 15 different fields

I know some professional developers are shaking there heads with disappointment... Im just trying to wrap my head around how to use Qlist to store multiple types of data within one table. And I know I will use QTableModel to set this data right? Building a billing app. Basic ideas or links will be fine... Thanks guys
Here is a good read for the best practices for Model/View Programming in Qt.
The examples and links at the bottom of the above link are nice, too.
As for storing multiple types, I would look at QVariant and QSettings; using QVariant is referenced many times in the Model/View Programming in Qt.
QVariant is very robust and works well with many different types. It is core in reading values out of QSettings. Be sure to read the description of QSettings for more ideas.
QBitArray toBitArray () const
bool toBool () const
QByteArray toByteArray () const
QChar toChar () const
QDate toDate () const
QDateTime toDateTime () const
double toDouble ( bool * ok = 0 ) const
QEasingCurve toEasingCurve () const
float toFloat ( bool * ok = 0 ) const
QHash<QString, QVariant> toHash () const
int toInt ( bool * ok = 0 ) const
QLine toLine () const
QLineF toLineF () const
QList<QVariant> toList () const
QLocale toLocale () const
qlonglong toLongLong ( bool * ok = 0 ) const
QMap<QString, QVariant> toMap () const
QPoint toPoint () const
QPointF toPointF () const
qreal toReal ( bool * ok = 0 ) const
QRect toRect () const
QRectF toRectF () const
QRegExp toRegExp () const
QSize toSize () const
QSizeF toSizeF () const
QString toString () const
QStringList toStringList () const
QTime toTime () const
uint toUInt ( bool * ok = 0 ) const
qulonglong toULongLong ( bool * ok = 0 ) const
QUrl toUrl () const
Another good option is QString. It has a number of built in conversions also:
QByteArray toAscii () const
QString toCaseFolded () const
double toDouble ( bool * ok = 0 ) const
float toFloat ( bool * ok = 0 ) const
int toInt ( bool * ok = 0, int base = 10 ) const
QByteArray toLatin1 () const
QByteArray toLocal8Bit () const
long toLong ( bool * ok = 0, int base = 10 ) const
qlonglong toLongLong ( bool * ok = 0, int base = 10 ) const
QString toLower () const
short toShort ( bool * ok = 0, int base = 10 ) const
std::string toStdString () const
std::wstring toStdWString () const
uint toUInt ( bool * ok = 0, int base = 10 ) const
ulong toULong ( bool * ok = 0, int base = 10 ) const
qulonglong toULongLong ( bool * ok = 0, int base = 10 ) const
ushort toUShort ( bool * ok = 0, int base = 10 ) const
QVector<uint> toUcs4 () const
QString toUpper () const
QByteArray toUtf8 () const
int toWCharArray ( wchar_t * array ) const
When saving numerical values to a QString, be sure to use QString::number().

How to convert LPTSTR to QString

Hi can anyone help me to convert LPTSTR to QString
You will see in the docs that Qstring provides static function to convert from both ascii and Unicode strings:
QString fromAscii ( const char *
ascii, int len = -1 )
QString fromLatin1 ( const char *
chars, int len = -1 )
QString fromUtf8 ( const char * utf8,
int len = -1 )
QString fromLocal8Bit ( const char *
local8Bit, int len = -1 )
QString fromUcs2 ( const unsigned
short * str )
Check whether you are using ascii or unicode and pick your poison.
QString::fromWCharArray is what worked for me.
To convert QString to LPTSTR or LPCTSTR:
QString src;
LPTSTR dest=(LPTSTR)src.utf16();
to convert from LPTSTR or LPCTSTR to QString :
src=QString::fromUtf16(dest);
The solution expects that your LPTSTR array is null-terminated.
STRRET str; // it had been filled by a winapi function
// str.pOleStr is LPWSTR, i.e. wchar_t* field of the union STRRET
// LPWSTR is the same as LPTSTR (see further)
QString result{QString::fromWCharArray(str.pOleStr)};
As shown below, STRRET is a union, one of representations of which (named pOleStr) has type wchar_t*.
As far as I understand, when one gets this STRRET filled by a Winapi function (like e.g. IShellFolder::GetDisplayNameOf()), it's a normal null-terminated wide string. So it can be supplied to QString::fromWCharArray(const wchar_t *string). The function will preserve the input wchar_t-array as it is and will just create a new copy inside a new QString. So overall, I think the method in my answer is quite safe as far as you know that your const wchar_t * is ended with a \0-wide-character.
// shtypes.h
typedef struct _STRRET
{
UINT uType;
/* [switch_is][switch_type] */ union
{
/* [case()][string] */ LPWSTR pOleStr;
/* [case()] */ UINT uOffset;
/* [case()] */ char cStr[ 260 ];
} DUMMYUNIONNAME;
} STRRET;
// winnt.h
typedef _Null_terminated_ WCHAR *NWPSTR, *LPWSTR, *PWSTR;
typedef LPWSTR PTSTR, LPTSTR;
Use QString::fromUcs2 to convert strings.
This is woking fine
QString str("ddddd");
LPCTSTR lstr = (LPCTSTR)str.data();

Resources