Qt常⽤类
向控制台输出⽂本
第⼀个例⼦,我们采⽤STL的⽅式:console.cpp
#include int main(){ std::cout << \"console application\\n\";} 第⼆个例⼦我们⽤QT4编程库console2.cpp #include QTextStream out(stdout); out << \"console application\\n\";} Output console application QFile 写⼀⾏字符串到⽂件中file.cpp #include QFile data(\"myfile\"); if (data.open(QFile::WriteOnly)) { QTextStream out(&data); out << \"You make me want to be a better man.\" << endl; }} Output $ cat myfile You make me want to be a better man. 下⾯的例⼦我们输出⼀段⽂本到控制台szerelem S a régi szeretőmérmit nem cselekednék,tengerből a vizetkanállal lemerném.S a tenger fenekérőlapró gyöngyöt szednék,s a régi szeretőmnekgyöngykoszorút kötnék. szerelem.cpp #include #include QFile data(\"szerelem\"); QString line; if (data.open(QFile::ReadOnly)) { QTextStream in(&data); QTextStream out(stdout); out.setCodec(\"UTF-8\"); in.setCodec(\"UTF-8\"); do { line = in.readLine(); out << line << endl; } while (!line.isNull()); }} Output S a régi szeretőmérmit nem cselekednék,tengerből a vizetkanállal lemerném.S a tenger fenekérőlapró gyöngyöt szednék,s a régi szeretőmnekgyöngykoszorút kötnék. QList Qt容器类之⼀mlist.cpp #include int main(){ QTextStream out(stdout); QList list << \"Balzac\" << \"Tolstoy\" << \"Guldbrassen\" << \"London\" << \"Galsworthy\" << \"Sienkiewicz\"; qSort(list); for (int i = 0; i < list.size(); ++i) { out << list.at(i) << endl; }} Output BalzacGalsworthyGuldbrassenLondon SienkiewiczTolstoy QDir 管理⽂件⽬录home.cpp #include int main(){ QTextStream out(stdout); QString home = QDir::homePath(); out << home << endl;} Output /home/vronskij 输出应⽤程序所在路径中扩展名是.c的全部⽂件名字filters.cpp #include int main(){ QTextStream out(stdout); QDir dir; QStringList filters; filters << \"*.c\" << \"*.c~\"; dir.setNameFilters(filters); QFileInfoList list = dir.entryInfoList(); for (int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); out << QString(\"%1\").arg(fileInfo.fileName()); out << endl; } } Output $ ls -F anim* anim.c anim.c~ filters*$ ./filters anim.canim.c~ QTime 输出当前时间mtime.cpp #include int main(){ QTextStream out(stdout); QTime qtime = QTime::currentTime(); QString stime = qtime.toString(Qt::LocalDate); out << stime << endl;} Output $ ./time 10:30:33 PM QString 字符串连接concat.cpp #include QString a = \"Disziplin \"; QString b = \"ist \"; QString c = \"Macht.\\n\"; QTextStream out(stdout); out << a + b + c;} Output $ ./concat Disziplin ist Macht. 字符串追加append.cpp #include QString string = \"Whether I shall \"; string.append(\"turn out to be the hero of my own life, \\n\"); string.append(\"or whether that station will be held by anybody else, \\n\"); string.append(\"these pages must show.\\n\"); QTextStream out(stdout); out << string;} Output $ ./append Whether I shall turn out to be the hero of my own life, or whether that station will be held by anybody else, these pages must show. 参数替换arg.cpp #include int main(){ QString string = \"What if I gave you %1 red roses?\"; int num = 21; QTextStream out(stdout); out << string.arg(num) << endl; } Output $ ./str3 What if I gave you 21 red roses? 输出字符串长度size.cpp #include int main(){ QString string = \"The history of my life.\"; QTextStream out(stdout); out << \"The string has \" + QString::number(string.size()) + \" characters.\" << endl; } Output ./size The string has 23 characters. 字符串⼤⼩写转换uplow.cpp #include int main(){ QString string = \"The history of my life.\"; QTextStream out(stdout); out << string.toLower() << endl; out << string.toUpper() << endl; } Output $ ./uplow the history of my life. THE HISTORY OF MY LIFE. 因篇幅问题不能全部显示,请点此查看更多更全内容