qt/LedOK/gutil/qcore.h

54 lines
1.9 KiB
C
Raw Normal View History

2023-04-18 14:14:46 +08:00
#ifndef QCORE_H
#define QCORE_H
#include <QThread>
2023-09-19 11:49:20 +08:00
#include <QEventLoop>
#include <QTimer>
#define ToStr QString::number
inline QString toStr(int num, int base=10) {return QString::number(num, base);}
inline QString toStr(uint num, int base=10) {return QString::number(num, base);}
inline QString toStr(long num, int base=10) {return QString::number(num, base);}
inline QString toStr(ulong num, int base=10) {return QString::number(num, base);}
inline QString toStr(qlonglong num, int base=10) {return QString::number(num, base);}
inline QString toStr(qulonglong num, int base=10) {return QString::number(num, base);}
inline QString toStr(double num, char format='g', int precision=6) {return QString::number(num, format, precision);}
inline QString gUrlSuffix(const QString &url, int size, bool withDot = false) {
auto ss = url.lastIndexOf('/')+1;
if(ss==url.size()) return QString();
ss++;
auto ee = url.indexOf('?', ss);
if(ee==-1) ee = url.size();
int end = ee-size-1;
if(end<ss) end = ss;
for(int i=ee-1; i>=end; --i) if(url[i]=='.') return withDot ? url.mid(i, ee-i) : url.mid(i+1, ee-i-1);
return QString();
}
2023-04-18 14:14:46 +08:00
inline QString byteSizeStr(double size) {
2024-01-28 20:28:02 +08:00
const char *units[]{"B", "KB", "MB", "GB", "TB", "PB", "EB"};
2023-04-18 14:14:46 +08:00
auto i = 0;
2024-01-28 20:28:02 +08:00
for(; size >= 1024 && i < 7; i++) size /= 1024;
return (size > 99 ? QString::number(size, 'f', 0) : QString::number(size, 'g', 3))+" "+units[i];
2023-04-18 14:14:46 +08:00
}
2023-09-19 11:49:20 +08:00
inline void wait(int msec, QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents) {
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(msec);
loop.exec(flags);
2023-04-18 14:14:46 +08:00
}
template <typename Func>
inline QThread *ThreadStart(Func &&f) {
QThread* thread = QThread::create(f);
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
thread->start();
return thread;
}
#endif