以下代码摘 self 的一个 Qt 程序,该程序将多个文件名作为多个 QLineEdits 的输入,并在按下按钮后将文件合并为一个。我最初用 C++ 编写代码作为命令行工具,现在问题出现了,因为我试图将来自 QLineEdits 的输入作为 const char* argv[] 提供给命令行工具。
问题是,出于某种原因,命令行代码将 argv[1] 和 argv[2] 作为第二个文件,而 Qt 代码向我保证 argv[1] 是第一个,argv[2]是第二个。
void VLay::condenseReflectanceFiles()
{
/**makes argv for wo_condense**/
const char* argv[size+1];
/**initialize first entry which wo_condense, when ran in terminal, thinks is the executable**/
argv[0] = NULL;
/**converts the text in the LineEdits into character strings and adds to argv**/
for(int k=0; k<size; k++)
{
argv[k+1] = fileList[k]->text().toUtf8();
//cout<< argv[k+1];
}
/**add one for the initial executable entry**/
wo_condense_R(size+1, argv);
}
int wo_condense_R(int argc, const char * argv[])
{
cout<< argv[1];
cout<< argv[2];
return 0;
}
这里有什么理论上的错误吗?
谢谢。
最佳答案
在下面的声明中:
argv[k+1] = fileList[k]->text().toUtf8();
此表达式返回的 QByteArray()
:
fileList[k]->text().toUtf8()
是一个临时变量,在语句结束时被销毁。因此,您存储在 argv[]
数组中的指针立即无效。
也许可以尝试这样的事情:
argv[k+1] = qstrdup(fileList[k]->text().toUtf8().constData());
如果您使用 qstrdup()
,请务必在这些指针上调用 delete []
。
关于c++ - Qt 函数在跳过第一个参数时重复第二个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729095/