Qt类使用记录

1 QSting


   1)   QString::arg()//用字符串变量参数依次替代字符串中最小数值       参见:   QString:: arg()方法 

    QString i = "iTest";           // current file's number  
    QString total = "totalTest";       // number of files to process  
    QString fileName = "fileNameTest";    // current file's name  
      
    QString status = QString("Processing file %1 of %2: %3")  
                    .arg(i).arg(total).arg(fileName);  
     style="background-color: #ffffff;">     qDebug() << status ;</span>  
 结果就是:"Processing file iTest of totalTest: fileNameTest"


  2) QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

     a表示输入的值,fieldWidth表示字符宽度,base表示进制,fillChar 表示填充字符

     若是字符宽度fieldWidth是正数,fillchar填在左边,若是负数,fillchar填在右边

    QString str;  
    str = QString("Decimal 63 is %1 in hexadecimal")  
            .arg(63, 0, 16);  
    // str == "Decimal 63 is 3f in hexadecimal"  
      
    QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));  
    str = QString("%1 %L2 %L3")  
            .arg(12345)  
            .arg(12345)  
            .arg(12345, 0, 16);  
    // str == "12345 12,345 3039"  
    //16进制显示,就忽略%L3的L  
    //格式化输出
    str = QString("%1").arg(521,5,10,QChar('a'))
    //str =="aa521"

3)  QString - std::string - char* - const char*  转换

QString - std::string:
            fileName.toStdWString() 

<pre name="code" class="html"> QString --> const char*:
            fileName.toStdWString().c_str()) 

<pre name="code" class="html"> QString -->  char*:

     QByteArray ba = fileName.toLatin1(); 
 
    char * fileName2=ba.data();
<span class="con"> char* --> const char *</span>
C++里面没有直接将stirng类型转换为char *类型的函数,用string.c_str()是将string类型的转换为const char *类型
1、你可以先将string类型转化为CString类型:
CString temp=c.c_str();
ps:mfc工程才可以用 包含头文件 :CString位于头文件afx.h中。
2、然后将CString类型转化为char *类型:
char *s=temp.GetBuffer(temp.ReleaseBuffer());
 
 

char*,const char*和string的相互转换

1. string转const char*
   string s = "abc";
   const char* c_s = s.c_str();

2. const char*转string
   直接赋值即可
   const char* c_s = "abc";
   string s(c_s);

3. string转char*
   string s = "abc";
   char* c;
   const int len = s.length();
   c = new char[len+1];
   strcpy(c,s.c_str());

4. char*转string
   char* c = "abc";
   string s(c);

5. const char*转char*
   const char* cpc = "abc";
   char* pc = new char[100];//足够长
   strcpy(pc,cpc);

6. char*转const char*
   直接赋值即可
   char* pc = "abc";
   const char* cpc = pc;

<pre name="code" class="html">void MainWindow::loadConfigure(QString fileName)
{   
    //char * fileName2= const_cast<char*>(fileName.toStdWString().c_str());
    QByteArray ba = fileName.toLatin1();
    char * fileName2=ba.data();
    configure_file = fileName2;
    if( pCam_qr_estimation->initConfigure(configure_file))
    {
        cout<<"configure_file"<<endl;
        configure_is_finished  = true;
        ui->pushButtonOpenFile->setEnabled(false);
    }
    else
        configure_is_finished  = false;
}
 
 


你可能感兴趣的:(Qt类使用记录)