
Variant类类似于C++的联合数据类型,它不仅能够保存很多Qt类型的值,包括QColor, QBrush, QFont, QRect, QString, QSize等,也能够存放Qt 的容器类型的值。Qt的很多功能都是建立在QVariant基础上的,如Qt的对象属性及数据库功能。
举例:
#include "widgeth"
#include <QDebug>
#include <QVariant>
#include <QColor>
Widget::Widget(QWidget parent)
: QWidget(parent)
{
QVariant v(709);
qDebug()<<vtoInt();
QVariant w("How are you!");
qDebug()<<wtoString();
1、QString
2、QVariant
3、QStringList
4、QVector
5、QStack
6、QQueue
7、QList
8、QMap
QString 是qt中关于String的封装类,用于处理字符串。
'''
void testQString(){
QString str1="hello";
qDebug()<<str1;
str1append("word");
qDebug()<<str1;//"hello word"
qDebug()<<str1indexOf("word");//5
QString str2="Hello";
qDebug()<<str2;
str2fill('x');//"xxxxx"
qDebug()<<str2;
str2fill('x',2);//"xx"
qDebug()<<str2;
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 str3="Hello";
qDebug()<<str3;
qDebug()<<str3left(3);//"hel"
qDebug()<<str3mid(2,2);//"ll"
qDebug()<<str3mid(2);//"llo"
qDebug()<<str3right(4);//"ello"
QString str4="hello word";
qDebug()<<str4;//"hello word"
str4remove(5,6);
qDebug()<<str4;//"hello"
QString str5="hello word";
str5insert(5,QString("word"));
qDebug()<<str5;//"hello wordword"
QString str6="hello word";
QString re="you";
str6replace("word",re);
qDebug()<<str6;//"hello you"
QString path="/user/local/bin/mapp";
qDebug()<<path;//"/user/local/bin/mapp"
QStringList list=pathsplit('/',QString::SkipEmptyParts);
qDebug()<<list;//("user,"local","bin","mapp")
QString str7="hello word";
qDebug()<<str7startsWith("hello");//true
qDebug()<<str7endsWith("word");//true
qDebug()<<QString("hello %1,helo you %2 ")arg("word")arg("hmf");//hello word,hello you hmf
qDebug()<<QString::localeAwareCompare("xxx","XXX");//-1
}
'''
QVariant 是万能变量,可以存取各种变量。
'''
void testQVariant(){
QVariant var;
varsetValue(QString("hello word"));
qDebug()<<var;//QVariant(QString, "hello word")
QString data=vartoString();
qDebug()<<data;//"hello word"
// varclear();
varsetValue(100);
qDebug()<<var;//QVariant(int, 100)
int d=vartoInt();
qDebug()<<d;//100
}
'''
QStringList 是存储QString类型的列表。
'''
void testQStringList(){
QStringList stL;
stL<<"str1"<<"str2"<<"str3"<<"str4";
qDebug()<<stL;//("str1", "str2", "str3", "str4")
QString str1=stLjoin("/");
qDebug()<<str1;//"str1/str2/str3/str4"
qDebug()<<stLcontains("str1");//true
qDebug()<<stLindexOf("str2");//1
stLappend("str3");
stLappend("str4");
qDebug()<<stL;//("str1", "str2", "str3", "str4", "str3", "str4")
stLremoveDuplicates();
qDebug()<<stL;//("str1", "str2", "str3", "str4")
//遍历方法1
for (int i=0;i<stLsize();i++){
qDebug()<<stLat(i);
}
//遍历方法2
QStringList::Iterator itr;
for(itr=stLbegin();itr!=stLend();++itr){
qDebug()<<itr;
}
}
'''
QVector 数组的模板类,本质是动态数组,存储方式是一片连续的内存空间。
'''
void testQVector(){
QVector<QString> tV;
tVappend("Str1");
tVappend("str2");
tVappend("str3");
tVappend("str4");
qDebug()<<tV;//QVector("Str1", "str2", "str3", "str4")
tVprepend("str0");
qDebug()<<tV;//QVector("str0", "Str1", "str2", "str3", "str4")
tVpush_back("str5");
qDebug()<<tV;//QVector("str0", "Str1", "str2", "str3", "str4", "str5")
tVpush_front("str00");
qDebug()<<tV;//QVector("str00", "str0", "Str1", "str2", "str3", "str4", "str5")
for(int i=0;i<tVsize();i++){
qDebug()<<tVat(i);
}
QVector<QString>::Iterator itr;
for(itr=tVbegin();itr!=tVend();itr++){
qDebug()<<itr;
}
qDebug()<<tVisEmpty();//false
qDebug()<<tVat(0);//"str00"
qDebug()<<tVvalue(3);//"str2"
qDebug()<<tVsize();//7
tVpop_back();
qDebug()<<tV;//QVector("str00", "str0", "Str1", "str2", "str3", "str4")
tVpop_front();
qDebug()<<tV;//QVector("str0", "Str1", "str2", "str3", "str4")
}
'''
QStack为qt中的栈模板类,继承于QVector,具有后进先出的特性。
'''
void testQStack(){
QStack<QString> stack;
stackpush("str1");
stackpush("str2");
stackpush("str3");
stackpush("str4");
qDebug()<<stack;//QVector("str1", "str2", "str3", "str4")
qDebug()<<stackpop();//"str4"
qDebug()<<stack;//QVector("str1", "str2", "str3")
qDebug()<<stacktop();//"str3"
qDebug()<<stack;//QVector("str1", "str2", "str3")
qDebug()<<stackisEmpty();//false
qDebug()<<stacksize();//3
while(!stackisEmpty())
{
qDebug()<<stackpop();
}
}
'''
QQueue 是qt中的队列的模板类,同样继承自QVector,具有先进先出的特性。
'''
void testQueue()
{
QQueue<QString> qq;
qqenqueue("str1");
qqenqueue("str2");
qqenqueue("str3");
qqenqueue("str4");
}
'''
QList是qt中的链表的实现,同时可以按位置索引和快速插入删除数据。
'''
void testList(){
QList<QString> ql;
qlappend("str");
qlappend("str1");
qlappend("str2");
qlappend("str3");
qlappend("str4");
qlappend("str5");
qDebug()<<ql;//("str", "str1", "str2", "str3", "str4", "str5")
for(int i=0;i<qlsize();i++){
qDebug()<<qlat(i);
}
QList<QString>::Iterator itr;
for(itr=qlbegin();itr!=qlend();itr++){
qDebug()<<itr;
}
qlpop_back();
qDebug()<<ql;//("str", "str1", "str2", "str3", "str4")
qlpop_front();
qDebug()<<ql;//("str1", "str2", "str3", "str4")
qDebug()<<qlsize();//4
qDebug()<<qlisEmpty();//false
}
'''
QMap 是qt中映射的模板类。就是字典。
'''
void testMap()
{
QMap<QString,int> map;
map["one"]=1;
mapinsert("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()<<mapvalue("one");//1
qDebug()<<map["two"];//2
qDebug()<<mapcontains("two");//true
qDebug()<<mapkeys();//("five", "four", "one", "three", "two")
qDebug()<<mapvalues();//(5, 4, 1, 3, 2)
//数据遍历
QMapIterator<QString ,int> itr(map);
while(itrhasNext()){
itrnext();
qDebug()<<itrkey()<<itrvalue();
}
}
'''
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
常用的Json库
JsonCpp
JsonCpp是一个C++用来处理JSON数据的开发包。
网址:>
在使用Qt编写应用程序时,如果您想获取最后一个插入的记录的ID,可以使用以下方法:
1 在插入新记录之前,请确保您的表中有一个主键列,并将其设置为自增长(即自动增加)类型。
2 在执行INSERT语句时,使用数据库驱动程序提供的lastInsertId()函数来获取刚刚插入的记录的ID。例如,在SQLite中,您可以使用“SELECT last_insert_rowid()”查询最后插入行的ID。
3 如果您使用的是模型/视图框架(例如QSqlTableModel或QSqlQueryModel),则可以使用model->query()lastInsertId()或者model->record(row)value("id")toInt()来获取插入记录的ID。其中,“id”是您的表中的主键列名称。
需要注意的是,不同的数据库驱动程序和 *** 作系统可能会有所不同,因此具体的实现可能会因平台而异。同时,在处理敏感信息时,请务必遵循安全的编程实践,例如使用参数化查询等技术来防止SQL注入攻击。
QT SQL 变量值 条件查询 插入数据
(本文只是总结网络上的教程)
在 *** 作数据库时
SQL语句中难免会用到变量
比如
在条件值已知的情况下
INSERT INTO table_name (列1, 列2,) VALUES (值1, 值2,)SELECT FROM Persons WHERE FirstName='Bush'
在条件值是变量的情况下
INSERT INTO table_name (列1, 列2,) VALUES (变量1, 变量2,)SELECT FROM Persons WHERE FirstName='变量'
或者SELECT FROM Persons WHERE 变量='变量'
方法据我所知有两种
1:用query的绑定特性来做
2:利用字符串特性,来组合SQL字符串(+,&,arg())详情可参考qstring,string用法(提示:qstring在执行sql语句时相当有用,当深入研究之>
以上就是关于qvariantmap 相当于pb于什么数据类型全部的内容,包括:qvariantmap 相当于pb于什么数据类型、QT中的常用数据结构及函数、请教Qt如何解析出Json的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)