Qt常用类
向控制台输出文本
第一个例子,我们采用STL的方式:
console.cpp
#include <iostream> int main()
{
std::cout << "console application\n";
}
第二个例子我们用QT4编程库
console2.cpp
#include <QTextStream> int main()
{
QTextStream out(stdout);
out << "console application\n";
}
Output
console application
QFile
写一行字符串到文件中
file.cpp
#include <QTextStream>
#include <QFile> 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ér
mit nem cselekednék,
tengerből a vizet
kanállal lemerném. S a tenger fenekéről
apró gyöngyöt szednék,
s a régi szeretőmnek
gyöngykoszorút kötnék.
szerelem.cpp
#include <QTextStream>
#include <QFile> 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ér
mit nem cselekednék,
tengerből a vizet
kanállal lemerném. S a tenger fenekéről
apró gyöngyöt szednék,
s a régi szeretőmnek
gyöngykoszorút kötnék.
QList
Qt容器类之一
mlist.cpp
#include <QTextStream>
#include <QList> int main()
{
QTextStream out(stdout); QList<QString> list; list << "Balzac" << "Tolstoy" << "Guldbrassen"
<< "London" << "Galsworthy" << "Sienkiewicz"; qSort(list); for (int i = 0; i < list.size(); ++i) {
out << list.at(i) << endl;
} }
Output
Balzac
Galsworthy
Guldbrassen
London
Sienkiewicz
Tolstoy
QDir
管理文件目录
home.cpp
#include <QTextStream>
#include <QDir> int main()
{
QTextStream out(stdout);
QString home = QDir::homePath();
out << home << endl;
}
Output
/home/vronskij
输出应用程序所在路径中扩展名是.c的全部文件名字
filters.cpp
#include <QTextStream>
#include <QDir> 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.c
anim.c~
QTime
输出当前时间
mtime.cpp
#include <QTextStream>
#include <QTime> 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 <QTextStream> 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 <QTextStream> 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 <QTextStream> 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 <QTextStream> 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 <QTextStream> 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.