Aşağıdaki kod ile QTextStream ile yapılan temel işlemleri, nasıl codec ayarlandığını ve C++ benzeri manipulator'ların bir kısmını inceleyebiliriz.
#include < QtCore/QCoreApplication > #include < QTextStream > #include < QTextCodec > #include < QFile > QTextStream cout(stdout); QTextStream cin(stdin); int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString str; QTextStream fout(&str); cout.setCodec("8859-9"); int testValue=0x33; QFile file("in.txt"); if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) return 1; fout.setDevice(&file); const QTextCodec * codec=fout.codec(); if(codec->name()!="8859-9") { cout << "Ön tanımlı codec 8859-9 değil 8859-9 olarak ayarlanıyor." << endl; fout.setCodec("8859-9"); } fout.setIntegerBase(8); fout << "Test Value : " << testValue << " [setIntegerBase(8)]" << reset << endl; fout << showbase << forcesign << forcepoint << uppercasebase << "Test Value : " << bin << testValue << "[showbase forcesign forcepoint uppercasebase bin]" << "\n" << uppercasedigits << lowercasebase << scientific << hex << "Test Value : " << testValue << " [lowercasebase uppercasedigits scientific hex]\n" << reset << dec << "Test Value : " << testValue << "[reset]" << flush; qSetFieldWidth(30); // Bu kod nedense sonuç üzerinde etki etmedi fout << "\n" << 334 << endl; fout.setFieldWidth(35); fout.setPadChar(QChar(128)); //€ karakteri doldurma karakteri olarak seçiliyor. fout << right << "33\n" << left << "33\n" << "\n" << flush; //Dikkat edilirse her karakter için setFieldWidth çalışıyor \n için bile fout << ws << "\n Skip Whitespace" << flush; //Yukarıdaki ws ile boşlukların gözükmemesini umuyordum. Fakat gözüküyor.! return a.exec(); }
Aşağıda dosya çıktısı bu programın sonucu
Test Value : 63 [setIntegerBase(8)] Test Value : +0B110011[showbase forcesign forcepoint uppercasebase bin] Test Value : +0x33 [lowercasebase uppercasedigits scientific hex] Test Value : 51[reset] 334 33 33 Skip Whitespace