您好,欢迎来到欧得旅游网。
搜索
您的当前位置:首页Qt学习之路(1)------Qt常用类用法说明

Qt学习之路(1)------Qt常用类用法说明

来源:欧得旅游网
Qt学习之路(1)------Qt常⽤类⽤法说明

Qt常⽤类

向控制台输出⽂本

第⼀个例⼦,我们采⽤STL的⽅式:console.cpp

#include

int main(){

std::cout << \"console application\\n\";}

第⼆个例⼦我们⽤QT4编程库console2.cpp

#include int main(){

QTextStream out(stdout);

out << \"console application\\n\";}

Output

console application

QFile

写⼀⾏字符串到⽂件中file.cpp

#include #include int main(){

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 int main(){

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 #include

int main(){

QTextStream out(stdout); QList list;

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 #include

int main(){

QTextStream out(stdout);

QString home = QDir::homePath(); out << home << endl;}

Output

/home/vronskij

输出应⽤程序所在路径中扩展名是.c的全部⽂件名字filters.cpp

#include #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 #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 int main(){

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 int main(){

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.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- ovod.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务