qt 语法使用技巧

16进制
connect(ui.toolButton_39, &QPushButton::clicked, [=]() {
		//qDebug() << "aa";

		broadcasting_type = 2;
		udp->close();udp->open(QIODevice::ReadOnly);

			
	/* 这样转为 16 进制前面有 0 如: 0000002e  ========== ok
		QString ipAddress = ".IPADR:192.168.1.92";
		QByteArray inputBytes = ipAddress.toUtf8();
		QString hexString;
		for (int i = 0; i < inputBytes.size(); ++i) {
			hexString.append(QStringLiteral("%1 ").arg(inputBytes.at(i), QT_POINTER_SIZE * 2, 16, QLatin1Char('0')));
		}
		qDebug() << hexString.trimmed();
	*/
	/* 将16进制字符串换为16进制数据发送 =========== ok
		QString ts = "8101060105050301FF"; // 云台向上
		QByteArray byteData;
		for (int i = 0; i < ts.length(); i += 2) {
			bool ok;
    		quint8 byteValue = ts.mid(i, 2).toInt(&ok, 16); // 将每两个16进制字符转换为一个字节
			if (ok) byteData.append(byteValue);
			else { qDebug() << "无效的十六进制字符串"; return;}
		}
		udp->writeDatagram(byteData,QHostAddress("192.168.1.91"),52381);
	*/

		// 普通字符串转为16进制字符串
		QString ipAddress = "IPADR:192.168.1.92";
		QByteArray inputBytes = ipAddress.toUtf8();
		QString hexString;
		for (int i = 0; i < inputBytes.size(); ++i) {
			QString hexValue = QStringLiteral("%1").arg(inputBytes.at(i), QT_POINTER_SIZE * 2, 16, QLatin1Char('0'));
			hexValue.remove(QRegularExpression("^0+")); // 去除前面的零
			hexString.append(hexValue);
			if (i < inputBytes.size() - 1) hexString.append(' '); // 在每个值之间添加空格
		}
		hexString.prepend("ff"); // prepend 在字符串的前面添加字符串,直接改变本体
		//qDebug() << "普通字符串转为16进制字符串: " << hexString.trimmed(); 
		

		hexString="02 4d 41 43 3a 32 38 2d 42 37 2d 37 43 2d 38 46 2d 33 33 2d 33 39 ff 49 50 41 44 52 3a 31 39 32 2e 31 36 38 2e 31 2e 39 32 ff 4d 41 53 4b 3a 32 35 35 2e 32 35 35 2e 32 35 35 2e 30 ff 47 41 54 45 57 41 59 3a 31 39 32 2e 31 36 38 2e 31 2e 31 ff 4e 41 4d 45 3a 43 41 4d 30 30 30 ff 03";
		hexString.replace(QRegularExpression("\\s"), ""); // 删除所有的空格,直接改变本体
		
		hexString="02 4d 41 43 3a 32 38 2d 42 37 2d 37 43 2d 38 46 2d 33 33 2d 33 39 ff 49 50 41 44 52 3a 31 39 32 2e 31 36 38 2e 31 2e 39 32";
		hexString.replace(QRegularExpression("\\s"), "");

		qDebug() << "普通字符串转为16进制字符串: "  << hexString;
		
		// 16进制字符串转为普通字符
		QString result;
		for (int i = 0; i < hexString.length(); i += 2){
			bool ok;
			quint8 byteValue = hexString.mid(i, 2).toInt(&ok, 16); // 将每两个16进制字符转换为一个字节
			if (ok) result.append(QChar(byteValue)); // 将字节转换为字符并添加到结果字符串中
			else { qDebug() << "无效的十六进制字符串"; break;}
        }
		qDebug() << "16进制字符串转为普通字符: " << result;

		// 16进制字符串换为16进制格式发送
		QByteArray byteData2;
		for (int i = 0; i < hexString.length(); i += 2) {
			bool ok;
    		quint8 byteValue2 = hexString.mid(i, 2).toInt(&ok, 16); // 将每两个16进制字符转换为一个字节
			if (ok) byteData2.append(byteValue2);
			else { qDebug() << "无效的十六进制字符串"; break;}
		}
		qDebug() << "16进制字符串换为16进制格式发送: " << byteData2;
		udp->writeDatagram(byteData2,QHostAddress("192.168.1.91"),52380);

	}); // 临时修改ip

 

lambda表达式
 []这个表示Lambda的开始,如果要加参数可以这样:[]()后面括号里面放参数

Qt中connect中的信号,参数:
    []:里面为空,表示不使用任何参数对象的参数;
    =:表示按值的方式进行传递;
    &:表示以引用的方式进行传递;
    this:表示函数体内可以使用Lambda所在类中的成员变量;
    a:按值的方式进行传递,默认是不能修改的,如果要修改,需要添加mutable修饰符。



类型转换

qreal = double

简单类型转换:

QString->QByteArray:.toUtf8()、.toLatin1()

QString->char*:QString str1 = "hello"; QByteArray ba = str1.toLatin1(); char *mm = ba.data();   // 先转成QByteArray再转成char*

QString->char:QString str1 = "hello"; str1.at(i).unicode(); 

QString->char[]: char mac[17]; for (int i = 0; i < macPath.length(); ++i) mac[i] = macPath.at(i).unicode(); // macPath.length > 17

QString->int: QString a="1111"; int b; b=a.toInt()

QString->qreal(double):.toDouble()

QByteArray->QString: QByteArray ba("abc"); QString str1 = ba;  //直接赋值

QByteArray->QPixmap:QPixmap imageresult; imageresult.loadFromData(bytearray);  imageresult.save("d:/result.png");

int->QString: int m=1; QString b; b=QString::number(m);

char * ->QString: str = QString(QLatin1String(ch));

char *->QByteArray: char *ch; QByteArray byte; byte = QByteArray(ch);

char *<-> const char *:char *ch1="tt"; const char *ch2="qq"; ch2 = ch1;//不报错,但有警告 \n ch1 = (char *)ch2;

char->QString: char a='b'; QString str; str=QString(a); 

QPixmap->QByteArray:  QPixmap image("d:/aaa.png"); QByteArray bytearray; QBuffer buffer(&bytearray); buffer.open(QIODevice::WriteOnly); bool bOk = image.save(&buffer,"PNG",20);

 

常用数据结构
包括
1、QString 2、QVariant 3、QStringList 4、QVector
5、QStack 6、QQueue 7、QList 8、QMap

一、QString(QString 是qt中关于String的封装类,用于处理字符串。)

void testQString(){
    QString str1="hello";
    str1.append("word"); 
    qDebug()<<str1;//"hello word"
    qDebug()<<str1.indexOf("word");//5
    qDebug() << str1.at(0); // 'h'
    if (QChar('a') < QChar('f')) qDebug() << "ok";  // ok
    
    QString str2="Hello";
    str2.fill('x'); qDebug()<<str2;   //"xxxxx"
    str2.fill('x',2); qDebug()<<str2; //"xx"
    
    QString str = "This is a\nmultiline\nstring\n";
    QString trimmedStr = str.trimmed();         // "This is a\nmultiline\nstring"
    QString simplifiedStr = str.simplified();   // "This is a multiline string"   // 这里用 ' ' 代替了 \n
    QString removedStr = str.remove('\n');      // "This is amultilinestring"
    QString str3="hello word";
    str3.remove(5,6); qDebug()<<str3;//"hello"

    qDebug()<<QString().isEmpty();//true
    qDebug()<<QString("").isEmpty();//true
    qDebug()<<QString(" ").isEmpty();//false
    qDebug()<<QString("abc").isEmpty();//false
    qDebug()<<QString().isNull();//true
    qDebug()<<QString("").isNull();//false
    qDebug()<<QString(" adc").isNull();//false
    
    QString str4="Hello";
    qDebug()<<str3.left(3);//"hel"
    qDebug()<<str3.mid(2,2);//"ll"
    qDebug()<<str3.mid(2);//"llo"
    qDebug()<<str3.right(4);//"ello"
    

    QString str5="hello word";
    str5.insert(5,QString("word")); qDebug()<<str5;//"hello wordword"

    QString str6 = "hello word word word";
    str6.replace("word", "love");
    qDebug() << str6; // "hello love love love"

    QString path="/user/local/bin/mapp";
    QStringList list=path.split('/',QString::SkipEmptyParts);
    qDebug()<<list;//("user,"local","bin","mapp")
    
    QString str7="hello word";
    qDebug() << str7.contains("hello", Qt::CaseSensitive); // true  // Qt::CaseInsensitive: 不区分大小写
    qDebug()<<str7.startsWith("hello");//true
    qDebug()<<str7.endsWith("word");//true
    qDebug()<<QString("hello %1,helo you %2 ").arg("word").arg("hmf");//hello word,hello you hmf
    qDebug()<<QString::localeAwareCompare("xxx","XXX");//-1    //比较两个字符串如果前者小于后者返回负整值,等于返回0,大于返回正整数
    qDebug() << QString::compare("s11", "S11", Qt::CaseSensitive); // 比较成功返回0 // Qt::CaseInsensitive:不区分大小写
}

二、QVariant (QVariant 是万能变量,可以存取各种变量)
void testQVariant(){
    QVariant var;
    var.setValue(QString("hello word"));
    qDebug()<<var;//QVariant(QString, "hello word")
    QString data=var.toString();
    qDebug()<<data;//"hello word"
    // var.clear();
    var.setValue(100);
    qDebug()<<var;//QVariant(int, 100)
    int d=var.toInt();
    qDebug()<<d;//100

    myStruct a;
    a.set_a(10);
    var=QVariant::fromValue(a);
    qDebug()<<var;//QVariant(myStruct, )
    qDebug()<<var.value<myStruct>().geta();//10

}

三、QStringList(QStringList 是存储QString类型的列表)
void testQStringList(){
    QStringList stL;
    stL<<"str1"<<"str2"<<"str3"<<"str4";
    qDebug()<<stL;//("str1", "str2", "str3", "str4")

    QString str1=stL.join("/");
    qDebug()<<str1;//"str1/str2/str3/str4"
    qDebug()<<stL.contains("str1");//true
    qDebug()<<stL.indexOf("str2");//1

    stL.append("str3");
    stL.append("str4");
    qDebug()<<stL;//("str1", "str2", "str3", "str4", "str3", "str4")

    stL.removeDuplicates();
    qDebug()<<stL;//("str1", "str2", "str3", "str4")

    //遍历方法1
    for (int i=0;i<stL.size();i++){
        qDebug()<<stL.at(i);
    }
    
    //遍历方法2
    QStringList::Iterator itr;
    for(itr=stL.begin();itr!=stL.end();++itr){
        qDebug()<<*itr;
    }
}

四、QVector(QVector 数组的模板类,本质是动态数组,存储方式是一片连续的内存空间)

void testQVector(){
    QVector<QString> tV;
    tV.append("Str1");
    tV.append("str2");
    tV.append("str3");
    tV.append("str4");
    qDebug()<<tV;//QVector("Str1", "str2", "str3", "str4")

    tV.prepend("str0");
    qDebug()<<tV;//QVector("str0", "Str1", "str2", "str3", "str4")

    tV.push_back("str5");
    qDebug()<<tV;//QVector("str0", "Str1", "str2", "str3", "str4", "str5")

    tV.push_front("str00");
    qDebug()<<tV;//QVector("str00", "str0", "Str1", "str2", "str3", "str4", "str5")

    for(int i=0;i<tV.size();i++){
        qDebug()<<tV.at(i);
    }

    QVector<QString>::Iterator itr;
    for(itr=tV.begin();itr!=tV.end();itr++){
        qDebug()<<*itr;
    }

    qDebug()<<tV.isEmpty();//false
    qDebug()<<tV.at(0);//"str00"
    qDebug()<<tV.value(3);//"str2"
    qDebug()<<tV.size();//7
    
    tV.pop_back();
    qDebug()<<tV;//QVector("str00", "str0", "Str1", "str2", "str3", "str4")

    tV.pop_front();
    qDebug()<<tV;//QVector("str0", "Str1", "str2", "str3", "str4")
}


五、QStack(QStack为qt中的栈模板类,继承于QVector,具有后进先出的特性)

void testQStack(){
    QStack<QString> stack;
    stack.push("str1");
    stack.push("str2");
    stack.push("str3");
    stack.push("str4");
    qDebug()<<stack;//QVector("str1", "str2", "str3", "str4")

    qDebug()<<stack.pop();//"str4"
    qDebug()<<stack;//QVector("str1", "str2", "str3")

    qDebug()<<stack.top();//"str3"
    qDebug()<<stack;//QVector("str1", "str2", "str3")

    qDebug()<<stack.isEmpty();//false

    qDebug()<<stack.size();//3

    while(!stack.isEmpty())
    {
    qDebug()<<stack.pop();
    }
}

六、QQueue(QQueue 是qt中的队列的模板类,同样继承自QVector,具有先进先出的特性)

void testQueue()
{
    QQueue<QString> qq;
    qq.enqueue("str1");
    qq.enqueue("str2");
    qq.enqueue("str3");
    qq.enqueue("str4");

    qDebug()<<qq;//("str1", "str2", "str3", "str4")

    qDebug()<<qq.head();//"str1"

    qDebug()<<qq.dequeue();//"str1"
    qDebug()<<qq;//("str2", "str3", "str4")

    qDebug()<<qq.isEmpty();//false

    qDebug()<<qq.size();//3

}

七、QList(QList是qt中的链表的实现,同时可以按位置索引和快速插入删除数据)

void testList(){
    QList<QString> ql;
    ql.append("str");
    ql.append("str1");
    ql.append("str2");
    ql.append("str3");
    ql.append("str4");
    ql.append("str5");
    qDebug()<<ql;//("str", "str1", "str2", "str3", "str4", "str5")

    for(int i=0;i<ql.size();i++){
        qDebug()<<ql.at(i);
    }
    
    QList<QString>::Iterator itr;
    for(itr=ql.begin();itr!=ql.end();itr++){
        qDebug()<<*itr;
    }
    
    ql.pop_back();
    qDebug()<<ql;//("str", "str1", "str2", "str3", "str4")

    ql.pop_front();
    qDebug()<<ql;//("str1", "str2", "str3", "str4")

    qDebug()<<ql.size();//4

    qDebug()<<ql.isEmpty();//false
}


八、QMap(QMap 是qt中映射的模板类。就是字典)

void testMap()
{
    QMap<QString,int> map;
    map["one"]=1;
    map.insert("two",2);
    map["three"]=3;
    map["four"]=4;
    map["five"]=5;
    qDebug()<<map;//QMap(("five", 5)("four", 4)("one", 1)("three", 3)("two", 2))

    qDebug()<<map.value("one");//1
    qDebug()<<map["two"];//2

    qDebug()<<map.contains("two");//true

    qDebug()<<map.keys();//("five", "four", "one", "three", "two")

    qDebug()<<map.values();//(5, 4, 1, 3, 2)
    //数据遍历
    QMapIterator<QString ,int> itr(map);
    while(itr.hasNext()){
        itr.next();
        qDebug()<<itr.key()<<itr.value();
    }
}

 

支持中文:#pragma execution_character_set("utf-8")

 

控件的层级位置:

//labelt3->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//raise();lower();stackUnder(); // stackUnder() 在谁的下面 ; lower() 当前父件的最下面

posted @ 2023-09-08 18:11  封兴旺  阅读(25)  评论(0编辑  收藏  举报

联系方式: 18274305123(微信同号)