Qt中类型之间的转换

QPixmap和QByteArray转换

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

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

int和QString转换

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

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

char和QString转换

//char 转换为 QString
char a='b';
QString str;
str=QString(a);

//QString 转换为 char
QString str="qq";
char *ch;
ch = str.toLatin1.data();

char * 和 const char *的转换

char *ch1="tt";
const char *ch2="qq";
ch2 = ch1;//不报错,但有警告
ch1 = (char *)ch2;

QByteArray 和 char *转换

//QByteArray 转换为 char *
char *ch;//不要定义成ch[n];
QByteArray byte;
ch = byte.data();

//char * 转换为  QByteArray
char *ch;
QByteArray byte;
byte = QByteArray(ch);

QString 和  QByteArray转换

//QString 转换为  QByteArray
QByteArray byte;
QString string;
byte = string.toAscii();

//QByteArray 转换为  QString
QByteArray byte;
QString string;
string = QString(byte);

 

参考链接:https://blog.csdn.net/ymc0329/article/details/7284514

     

 

上一篇:QString.toUtf8().data()的问题 & char *转换到QByteArray注意


下一篇:Qt_屏幕保护程序、进程监听、数据库读取、屏幕保护