日志信息
如果不是通过IDE进行程序调试,而是直接执行可执行程序,在这种情况下是没有日志输出窗口的,因此也就看不到任何的日志输出。为了在直接点击程序后有日志输出,需要在项目配置文件中添加:
重新编译后可以直接将日志信息打印到终端窗口。
字符串类型
QByteArray
Qt中,QByteArray
可以看做是char*
的升级版本。在使用这种类型的时候可通过这个类的构造函数申请一块动态内存,用于存储需要处理的字符串数据。
QString
QString
也封装了字符串, 但是内部的编码为UTF-8, UTF-8属于Unicode字符集, 它固定使用多个字节 (Window一般为2字节, Linux一般为3字节) 来表示一个字符,这样可以将世界上几乎所有语言的常用字符收录其中。
1 2 3 4 5 6 7 8 9
| QString str = QString("(%1)有(%2)个徒弟,分别是(%3), (%4), (%5)") .arg("唐僧").arg(3).arg("孙悟空").arg("猪八戒").arg("沙僧");
qDebug() << "字符串:" << str;
QString temp1 = "这是一串字符。。。,"; QByteArray temp2 = "这是一串字符。。。,"; qDebug() << "QString length: " << temp1.length(); qDebug() << "QByteArray length: " << temp2.length();
|
QVariant
示例程序:
mainwindow.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
struct Person { int id; QString name; }; Q_DECLARE_METATYPE(Person)
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
QVariant dataPlus(QVariant a, QVariant b);
private: Ui::MainWindow *ui; }; #endif
|
mainwindow.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this);
int value = dataPlus(10, 20).toInt(); QString str = dataPlus("hello ", "world").toString();
qDebug() << "int: " << value; qDebug() << "str: " << str;
Person p; p.id = 250; p.name = "张三";
#if 0 QVariant v; v.setValue(p); #else QVariant v = QVariant::fromValue(p); #endif
if (v.canConvert<Person>()) { Person temp = v.value<Person>(); qDebug() << "id: " << temp.id << ", name: " << temp.name; } }
MainWindow::~MainWindow() { delete ui; }
QVariant MainWindow::dataPlus(QVariant a, QVariant b) { QVariant res; if (a.typeId() == QMetaType::Int && b.typeId() == QMetaType::Int) { res = QVariant(a.toInt() + b.toInt()); } else if (a.typeId() == QMetaType::QString && b.typeId() == QMetaType::QString) { res.setValue(a.toString() + b.toString()); } return res; }
|
QPoint
和QLine
1 2 3 4 5 6 7 8
| QLine line(QPoint(100, 200), QPoint(150, 210)); QLine newLine = line.translated(20, 30);
qDebug() << "Before translate: " << line; qDebug() << "After translate: " << newLine;
|
QDate
1 2 3 4 5
| QDate d = QDate::currentDate(); qDebug() << "year: " << d.year() << ", month: " << d.month() << ", day: " << d.day(); QString str2 = d.toString("yyyy-MM-dd"); qDebug() << "date str:" << str2;
|
QTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QTime> #include <QElapsedTimer>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this);
QTime currentTime = QTime::currentTime(); qDebug() << "hour: " << currentTime.hour() << ", minute: " << currentTime.minute() << ", second: " << currentTime.second() << ", millisecond: " << currentTime.msec(); QString strtm = currentTime.toString("hh:mm:ss.zzz"); qDebug() << "time formatted: " << strtm;
QElapsedTimer tt; tt.start(); randNumbers(100); qint64 ms = tt.elapsed(); qDebug() << "函数执行用时:" << ms << "毫秒"; }
void MainWindow::randNumbers(int counts) { srand(time(NULL)); for (int i = 0; i < counts; ++i) { int num = rand() % 10000; qDebug() << num; } }
MainWindow::~MainWindow() { delete ui; }
|
QDateTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| QDateTime dt = QDateTime::currentDateTime(); QString strdt = dt.toString("yyyy/MM/dd hh:mm:ss ap"); qDebug() << strdt; QString strdt2 = dt.toString("yyyy/MM/dd HH:mm:ss"); qDebug() << strdt2;
QDate d2 = dt.date(); qDebug() << "year: " << d2.year() << ", month: " << d2.month() << ", day: " << d2.day(); QTime t2 = dt.time(); qDebug() << "hour: " << t2.hour() << ", minute: " << t2.minute() << ", second: " << t2.second() << ", millisecond: " << t2.msec();
|
输出:
1 2 3 4
| "2024/05/14 12:45:16 pm" "2024/05/14 12:45:16" year: 2024 , month: 5 , day: 14 hour: 12 , minute: 45 , second: 16 , millisecond: 700
|