QString to char* conversion

前端 未结 10 2076
予麋鹿
予麋鹿 2020-11-27 02:57

I was trying to convert a QString to char* type by the following methods, but they don\'t seem to work.

//QLineEdit *line=new QLineEdit();{just to describe w         


        
10条回答
  •  离开以前
    2020-11-27 03:14

    David's answer works fine if you're only using it for outputting to a file or displaying on the screen, but if a function or library requires a char* for parsing, then this method works best:

    // copy QString to char*
    QString filename = "C:\dev\file.xml";
    char* cstr;
    string fname = filename.toStdString();
    cstr = new char [fname.size()+1];
    strcpy( cstr, fname.c_str() );
    
    // function that requires a char* parameter
    parseXML(cstr);
    

提交回复
热议问题