#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QLabel>
#include <QString>
#include <QByteArray>
int main(int argc, char *argv[])
{
QApplication a(argc, argv); // 创建一个QApplicatio对象管理应用程序的资源
//MainWindow w;
//w.show();
//QLabel *label = new QLabel("Hello Qt!"); // 每个QT类都有一个与该类同名的头文件,创建QLabel窗口对象
//label->setGeometry(400, 100, 100, 20); //设置控件显示的位置
//label->show(); // 使QLabel创建的窗口可见
// 调试输出
qDebug()<<"QT调试信息输出";
int data_int = 8888;
qDebug()<<data_int;
float data_float = 123.888;
qDebug()<<data_float;
// 字符串转整型、浮点型
QString str_1("12345");
qDebug()<<"int :"<<str_1.toInt(); // str_1.setNum(888) 整型转字符串
qDebug()<<"long :"<<str_1.toLong();
QString str_2("123.45");
qDebug()<<"float :"<<str_2.toFloat();
qDebug()<<"int :"<<str_2.toInt(); // 字符串有小数转整型会失败, 返回0
// 字符串转字节数组
QString str("12345");
QByteArray text = str.toLocal8Bit();
qDebug()<<text.data();
// 字符串转char类型字符串
QString str1("123456789");
char *ch1;
QByteArray by = str1.toLatin1();
ch1 = by.data();
qDebug()<<ch1;
QString str2("123456中文转换输出");
std::string stdstr = str2.toStdString();
const char * ch2 = stdstr.c_str();
qDebug()<<ch2;
// 字符串拼接
QString x = "free";
QString y = "dom";
x.append(y); // x += y;
qDebug()<<x;
qDebug()<<x.size();
qDebug()<<x.count();
// 字符串比较
int compare1 = QString::compare("aUtO", "Auto", Qt::CaseInsensitive); // 忽略大小写 =0
int compare2 = QString::compare("auto", "Car", Qt::CaseSensitive); // 不忽略大小写 >0
int compare3 = QString::compare("auto", "Car", Qt::CaseInsensitive); // <0
qDebug()<<compare1;
qDebug()<<compare2;
qDebug()<<compare3;
// 字符串查找
QString seekstr = "Peter Pan";
qDebug()<<seekstr.contains("peter", Qt::CaseInsensitive); // 忽略大小写 return true
QString seekstr1 = "123123123";
qDebug()<<seekstr1.count("123");
QString seekstr2 = "sticky question";
QString seekstr3 = "sti";
qDebug()<<seekstr2.indexOf(seekstr3);
qDebug()<<seekstr2.indexOf(seekstr3, 1);
qDebug()<<seekstr2.indexOf(seekstr3, 10);
qDebug()<<seekstr2.indexOf(seekstr3, 11);
// 单个字符填充字符串
QString fillstr = ("Berlin");
fillstr.fill('A', 2); // 指定填充2个 fillstr.fill('z');
qDebug()<<fillstr;
// 插入字符串
QString instr = "Meal";
instr.insert(1, QString("ontr")); // 指定第一个位置插入字符串
qDebug()<<instr;
// 从左向右截取字符串
QString cutoutleft = "Pineapple";
qDebug()<<cutoutleft.left(4);
// 从中间截取字符串
QString cutoutmid = "Nine pineapples";
qDebug()<<cutoutmid.mid(5,4);
qDebug()<<cutoutmid.mid(5);
// 删除某个字符
QString s = "Mintreal";
s.remove(1, 4);
qDebug()<<s;
// 替换某些字符
QString replacestr = "no";
QString sourcestr = "Say yes";
sourcestr.replace(4, 3, replacestr);
qDebug()<<sourcestr;
// 切割字符串
QString csv = "forename,middlename,surname,phone";
QString path = "/usr/local/bin/myapp";
QString::SectionFlag flag = QString::SectionSkipEmpty;
qDebug()<<csv.section(',', 2, 2);
qDebug()<<path.section('/', 3, 4);
qDebug()<<path.section('/', 3, 3, flag);
return a.exec(); // 应用程序将控制权传递给QT让程序进入消息循环,等待可能的菜单、工具条、鼠标等的输入,进行响应
}
运行结果
QT调试信息输出
8888
123.888
int : 12345
long : 12345
float : 123.45
int : 0
12345
123456789
123456中文转换输出
“freedom”
7
7
0
30
-2
true
3
0
10
10
-1
“AA”
“Montreal”
“Pine”
“pine”
“pineapples”
“Meal”
“Say no”
“surname”
“bin/myapp”
“myapp”