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);

 

QString转换为const wchar_t

 

const wchar_t * new_name=reinterpret_cast<const wchar_t *>(name.utf16());

 

 

 

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

     

 

posted @ 2021-07-26 14:21  kongbursi  阅读(271)  评论(0编辑  收藏  举报