Qt

Qtの基本的なデータ型



Basic Data Types Qt



1.1基本データ型QGlobal

  • qint8符号付き文字
  • qint16は短く署名しました
  • qint32符号付き整数
  • qint64 long long int(Windowsでは__int64)
  • qreal double
  • quint8 unsigned short
  • quint16 unsigned short
  • quint32 unsigned int
  • quint64 unsigned long long int(Windowsではunsigned __int64)

1.2一般的に使用される機能

  • T qAbs(const T&t)//絶対値を見つける
  • T&qMax(const T&t1、const T&t2)
  • T&qMin(const T&t1、const T&t2)
  • T&qBound(const T&t1、const T&t2、const T&t3)
qreal abs=-4.567 qreal myValue1,myValue2,myValue3,myValue4 qint32 int_a=6 qint32 int_b=4 qint32 int_c=7 myValue1=qAbs(abs) myValue2=qMax(int_a,int_b) myValue3=qMin(int_a,int_b) myValue4=qBound(int_a,int_c,int_b)

浮動小数点サイズを比較する

  • bool qFuzzyCompare(float p1、float p2)
  • bool qFuzzyCompare(double p1、double p2)
  • 前面が背面よりも大きい場合はtrueまたはfalse
qreal abs=-4.567 qreal abs2=3.24 Bool i=qFuzzyCompare(abs,abs2) //return false

乱数



  • Qsrandは、qrandによって生成された乱数の開始値であるシードを設定するために使用されます。たとえば、qsrand(10)、シードとして10を設定すると、qrandによって生成される乱数は[10,32767]の間にあり、qrand()の前にqsrand()が呼び出されない場合、qrand()は自動的に呼び出されます。 。 Qsrand(1)、つまり、システムはデフォルトで1を乱数の開始値として使用します。同じシードを使用して乱数を生成します
  • ここでは、時間など、固定されていないuint値をシードとして使用できます。
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())) for(int i=0 i<10 i++) { int test =qrand()%10 qDebug()<

環境変数を取得する

QByteArray env=qgetenv('path') qDebug()<

QSize(int、int)
QSizeF(qreal、qreal)



QSize size(100,10) QSize size1(100,10) size.rheight()+=4 size1.rwidth()+=4 QSizeF size2(100,10) size2.rwidth()+=4

結果サイズ(100,14)サイズ1(104,10)サイズ2(104.0,10.0)

QPoint(int、int)
QPointF(qreal、qreal)

QPoint size(100,10) QPoint size1(100,10) size.rx()+=4 size1.ry()+=4 QPointF size2(100,10) size2.rx()+=4
  • QRect(const QPoint&topleft、const QPoint&buttomRight)
  • QRect(const QPoint&topleft、const QSize&size)
  • QRect(int x、int y、int with、int height)
QRect r1(10,10,10,10) QRect r2(QPoint(10,10),QSize(11,16)) QRect r3(QPoint(10,10),QPoint(11,16))

デバッガーは3番目の形式に展開されます



QString:Qt文字列操作

2.1初期化

  • QString(const QChar unicode、int size = -1)
  • QString(QChar ch)
  • QString(int size、Qchar ch)
  • QString(QLatin1String str)
  • QString(const QString&other);
  • QString(QString && other)
  • QString(const char str)
  • QString(const QByteArray&ba)
QString str5('dsfafd') QString str2('a') QString str3(5,'s') QString str='TZ

2.2追加

  • prepend()
  • append()
  • push_pack()
  • push_front()
QString str ='NC' str.prepend('hello') str.append('EDU') str.push_back('goo') str.push_front('bad')

2.3交換
replace(1,0、str1)//パラメーター1:最初の位置から開始し、パラメーター2:基点から後ろまでのいくつかの位置、次にこの段落を削除し、置き換えられたstrを置き換えます

QString str ='NC' QString str1='wc' str.replace(1,0,str1) //Result 'NwcC' QString str ='say yes' QString str1='no' str.replace(4,3,str1) //Results say no str.replace(3,3,str1) //Results saynoy

2.4削除

  • ビットで削除remove(int、int)
  • 特定の文字列を削除しますremove(QChar( ‘y’)、Qt :: CaseInsensitive)
  • Qt :: CaseInsenstive //マクロでは大文字と小文字は区別されません。デフォルトでは大文字と小文字が区別されます。
QString str ='say yes' QString str1='no' str.remove(0,5) str1.remove(1,1) //Results es str.remove(QChar('y'),Qt::CaseInsensitive) //Results sa es

2.5QStringの挿入

str.insert(4,QString('ok'))

2.6NULLと判断|空の

bool i=true QString str2='' QString str1='no' i=QString().isEmpty()//true i=str2.isEmpty()//true i=str1.isEmpty()//false i=QString().isNull()//true i=QString('').isNull()//false i=QString('asd').isNull()//false / / Common usage if(!str.isEmpty()||!str.isNull()) { }

2.7文字列が表示される場所を見つける

indexOf(QString str, int index)//index indicates where to start looking, 0 is the starting point, and the string subscript is returned. int i QString str ='stick question' QString str1='ti' i=str.indexOf(str1) //1 i=str.indexOf(str1,2)//10 i=str.indexOf(str1,11)//-1

2.8文字列の出現回数を見つける

QString str ='Stick question' QString str1='sti' i=str.count(str1) //return 1 j=str.count(str1,Qt::CaseInsensitive)//return 2

2.9別の文字列が含まれているかどうかを確認する

bool x QString str ='Stick question' QString str1='sti' x=str.contains(str1)//true

2.10文字列の傍受

QString str ='Stick question' QString str1='sti' - Intercept to the left Str1=str.left(7) //start from the left - Intercept to the right i=str.right(5), // starting from the right - Interception j=str.mid(6,4)//Starting from the first argument, counting back 4

2.11デジタルから文字列へQString

Static method QString::number(uint n,int base=10) QString::number(uint n,16) QString::number(uint n,16).toUpper() QString::number(num)//qreal num QString::number(num,'g',8)//The 8 bits here are not counting decimals

2.12フォーマットされた出力

qreal x=123.123345678 QString str1='RainYa' QString str2 str2=QString('name is %1,age is %2').arg(str1).arg(x,0,'f',3) //qreal keeps a few decimals, the last parameter is determined

2.13 QStringおよびstd:string変換

QString str1='RainYa' std::string str2='helllo' std::string str=str1.toStdString() QString qstr=QString::fromStdString(str2) QByteArray ba('hello') ba.resize(5) ba[0]

QByteArray

3.1初期化

QByteArray ba(“ hello”)

3.2アクセス
  • ba.resize(5)
  • ba [0]
  • ba.at (0)
3.3QByteArrayおよびchar *変換
  • char *に変換
    ba.constData()
QByteArray ba('hello') const char* str=ba.constData()
  • QByteArrayに変換する
/ / Through the character set conversion char *p='abc' QString c=QString(p) QByteArray ba1=c.toUtf8() / / Through QByteArray:: fromRawData (const char *, int) QByteArray ba2(QByteArray::fromRawData(p,5))
3.4 / 0について理解する
QByteArray ba('hello') qDebug()<

文字列に変換すると、 0はここで終了します。 3を取得、ヘル

3.5 QVariant

QVariant変数には、最も一般的なQtデータ型コミュニティ(Union)のようなデータ型はありませんが、一度に1つのオブジェクトしか使用できず、一部のオブジェクトは複数値になる場合があります(文字列のリストなど)。
toT():Tを使用してデータ型を表し、QVariantを別の型に変換して値を取得できます。ここでは、オブジェクトを割り当てて、元のオブジェクトが変更されないようにします。

QVariant b('fdfdsgfe') //QVariant QString str=b.toString() //QString -------------- This function can not be used as a type conversion ------- QStringList str1 str1<<'ni'<<' hao' QVariant b(str1) QStringList str=b.toStringList()