数据类型和数据操作类

本文探讨了数据类型的使用,重点介绍了QBitArray、QBrush、QByteArray、QColor、QData、QDate、QDateTime、QEasingCurve、QKeySequence以及QUrl等数据操作类,并提供了相关实例,如设置QColor的颜色,获取当前日期和时间等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据类型和数据操作类

比较和判断变量的函数
函数作用
qAbs(const T& value)返回绝对值
qBound(min,mid,max)返回mid
qMax()
qMIn()
qRound()四舍五入
数据类

QBitArray:

/*
 * count()数组的长度,size
 * fill(bool,start,end)填充数组
 * resize(int)重置长度
 * clear()长度变为0
 * clearBit(int)让int位置的值变成0
 * isEmpty()当count=0时,为true
 * 可以参与&|!名算
 *
*/
MyFileSystem::MyFileSystem(QWidget *parent)
    :QWidget(parent)
{
   QBitArray a1(5);//a1=[0 0 0 0 0]
   qDebug()<<"数组的长度"<<a1.count()<<a1;
   a1.fill(true,0,a1.count());//a1=[1 1 1 1 1]
   qDebug()<<"fill之后的变化"<<a1;
   a1.resize(3);
   qDebug()<<a1.count()<<a1;
   a1.clearBit(0);
   qDebug()<<a1.count()<<a1<<a1.isEmpty();
}

QBrush:
在这里插入图片描述

void MyFileSystem::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QRadialGradient gradient(50, 50, 50, 50, 50);
    gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));
    gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));
    QBrush brush(gradient);
    painter.setBrush(brush);
    painter.setPen(Qt::darkCyan);
    painter.drawRect(0,0,100,100);

    painter.setBrush(Qt::NoBrush);
    painter.setPen(Qt::darkGreen);
    painter.drawRect(40,40,100,100);
}

QByteArray

MyFileSystem::MyFileSystem(QWidget *parent)
    :QWidget(parent)
{
   //1.构造
    QByteArray a1(5,'x');               //a1=[x x x x x] size=5
    QByteArray a2("hello the world");   //a2="hello the word" size=15
    QByteArray a3(a2);                  //a3=a2

   //2.增删查改大小
    a3.append(a1);                      //a3=hello the worldxxxxx
    a3.clear();
    a3.append(5,'a');
    a3.append("zylg");                  //a3=aaaaazylg
    a3.clear();
    a3.insert(0,"zylg");
    a3.insert(0,3,'x');
    a3.insert(7,a1);                    //a3=xxxzylgxxxxx"
    a3.push_back("zy");                 ////a3=xxxzylgxxxxxzy"
    a3.push_front("zy");                //a3=zyxxxzylgxxxxxzy"
    a3.replace("xxx","");               //a3=zyzylgxxzy
    a3.replace(0,2,"zylg");             //a3=zylgzylgxxzy
    a3.remove(2,3);                     //a3=zyylgxxzy"
    qDebug()<<a3<<a3.capacity()<<a3.length();
   //3.位置
    a3.clear();
    a3.append("zylgzy");
    a3.at(1);                           //return y
    qDebug()<<a3.indexOf("zy",3);       //从三个开始算,zy的位置在哪,4
    qDebug()<<a3.lastIndexOf("zy");
    qDebug()<<a3.left(4);               //zylg
    qDebug()<<a3.right(4);              //lgzy

   //4.其他(转换,包换,截取 )
    for(auto it=a3.begin();it!=a3.end();it++)   //迭代器
    {
        qDebug()<<*it;
    }
    if(a3.contains("zy"))qDebug()<<"contains"; //包含zy
    qDebug()<<a3.count("zy");                  //zy的个数
    char *data=a3.data();                      //a3的数据
    qDebug()<<data;
    if(a3.endsWith("zy"))qDebug()<<"结尾包含zy";//以zy结尾
    if(a3.startsWith("zy"))qDebug()<<"以zy开头";
    a3.setNum(43,16);                          //添加43的十六进制,a3=2b
    QByteArray ba("  lots\t of\nwhitespace\r\n ");
    ba = ba.simplified();                      // ba == "lots of whitespace";


//    a3.resize(20);a3.fill('x',10);             //a3=xxxxxxxxxx
    qDebug()<<a3;

    //toHex() 大小写 toInt toFloat等 rePeat
}

QColor
QColor color(r,g,b,255)
QData
QDate data=QDate::currentDate();
QDateTime da=QDateTime::currentDateTime();
QEasingCurve

    QPropertyAnimation animation;
    animation.setStartValue(0);
    animation.setEndValue(1000);
    animation.setDuration(1000);
    animation.setEasingCurve(QEasingCurve::InOutQuad);

QKeySequence

QKeySequence(QKeySequence::Print);
QKeySequence(tr("Ctrl+P"));
QKeySequence(tr("Ctrl+p"));
QKeySequence(Qt::CTRL + Qt::Key_P)

QMenu *file = new QMenu(this);
file->addAction(tr("&Open..."), this, SLOT(open()),
                  QKeySequence(tr("Ctrl+O", "File|Open")));

QUrl

QUrl url("http://www.example.com/List of holidays.xml");
// url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml"
QUrl url = QUrl::fromEncoded("http://qt-project.org/List%20of%20holidays.xml");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值