Tabbas, bari mu fara da cikakken labarin kan canza QChar zuwa char a cikin yaren shirye-shirye na C++.
QChar nau'in bayanai ne da aka saba amfani dashi a cikin tsarin Qt don C++. Babban manufar yin amfani da wannan ita ce adana harafin Unicode 16-bit. Koyaya, akwai yanayi yayin haɓakawa inda zamu iya buƙace shi a cikin sigar 'char' na asali. Char a C++ shine mafi yawan nau'in bayanai na asali, yana riƙe da hali ɗaya kuma yana buƙatar byte ɗaya na ƙwaƙwalwar ajiya a kusan duk masu tarawa. Bari mu koyi yadda ake canza QChar zuwa char a C++.
Hanya mafi sauƙi don juyowa ita ce ta amfani da ayyukan 'toAscii()' da 'toLatin1()'. Koyaya, saboda sabuntawa a cikin nau'ikan Qt, 'toAscii()' an soke shi tun Qt 5.0. Don haka, an ba da shawarar amfani da 'toLatin1()' maimakon 'toAscii()', idan kuna amfani da sigar Qt daga baya fiye da 5.0.
QString str = "Sannu, Duniya!";
QChar qchar = str[0];
char ch = qchar.toLatin1 ();
Ta hanyar yin nunin harafi a cikin QString tare da fihirisa kamar str[0], muna samun harafin farko ta hanyar QChar. Sannan, aikin 'toLatin1()' yana canza QChar zuwa harafin Latin-1.
Binciken lambar
Lambar bayar a sama yana da sauqi qwarai. Yana canza QChar zuwa char kuma yana buƙatar ƙarancin layukan lamba.
Muna farawa ta hanyar ƙaddamar da QString tare da "Sannu, Duniya!". Sa'an nan, mu sanya da hali na farko na wannan kirtani zuwa m QChar. A ƙarshe, muna juyar da wannan QChar zuwa 'char' ta amfani da aikin 'toLatin1()' kuma adana sakamakon a cikin ma'auni na 'char'.
Dakunan karatu da Ayyuka
Biyu mabuɗin dakunan karatu ana amfani da su a cikin wannan jujjuyawar:
- QString: Ginin aji ne na kirtani wanda Qt ke bayarwa kuma ana amfani dashi don sarrafa kirtani.
- QChar: Ginin aji ne na char wanda Qt ke bayarwa kuma ana amfani dashi don sarrafa haruffan unicode.
Waɗannan ɗakunan karatu suna ba da jerin ayyuka waɗanda ke taimakawa sauƙaƙe ƙirƙira, gyare-gyare, da jujjuya nau'ikan bayanai a cikin Qt.
Amfani da dakunan karatu da suka dace shine muhimmin al'amari na shirye-shirye a cikin C++. Dakunan karatu masu zurfin tunani kamar QString da QChar bayar da ayyuka masu yawa, ta haka ne ke adana lokaci mai yawa da ƙoƙarin mai haɓakawa.
Tuna, ingantaccen shirye-shirye masu inganci ba wai yana nufin rubuta kowane yanki na lamba da injina kawai ba, har ma yana nuna yadda ya kamata a yi amfani da tsarin da dakunan karatu da ake da su don sa tsarin yin rikodin ya fi sauƙi da inganci.