当前位置: 代码迷 >> 综合 >> 错误:cannot convert 'QString' to 'const char*' for argument '1' to 'int system(const char*)'
  详细解决方案

错误:cannot convert 'QString' to 'const char*' for argument '1' to 'int system(const char*)'

热度:75   发布时间:2023-12-13 12:34:36.0
若:    QString  str1 = "date -s 04/30/2012";

             system(str1);//

则:/home/qust/qt/time-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK____/../time/mainwindow.cpp:31: 错误:cannot convert 'QString' to 'const char*' for argument '1' to 'int system(const char*)'

修改:

QString  str1 = "date -s 04/30/2012";

             system(str1.toLatin1().data());//



QByteArray QString::toLatin1 () const


Returns a Latin-1 representation of the string as a QByteArray.

The returned byte array is undefined if the string contains non-Latin1 characters. Those characters may be suppressed or replaced with a question mark.


char * QByteArray::data ()


Returns a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is '\0'-terminated, i.e. the number of bytes in the returned character string is size() + 1 for the '\0' terminator.

Example:

 QByteArray ba("Hello world");char *data = ba.data();while (*data) {cout << "[" << *data << "]" << endl;++data;}


  相关解决方案