PDA

View Full Version : Problems communicating with external editor



titaniumdecoy
18th October 2009, 09:56
I am writing a command-line Qt4 script (using QCoreApplication) on Mac OS X.

I am using this code adapted from C++ Programming with Qt 4, 2nd ed. p. 313:


QTemporaryFile outFile;
if (!outFile.open())
return;

QString fileName = outFile.fileName();
QTextStream out(&outFile);
out << initial_text;
outFile.close();

QProcess::execute(editor, QStringList() << fileName);

QFile inFile(fileName);
if (!inFile.open(QIODevice::ReadOnly))
return;

QTextStream in(&inFile);
QString text = in.readAll();

std::cout << text.toStdString() << std::endl;

When the above is run with editor set to "/usr/bin/vim", "Vim: Warning: Input is not from terminal" is printed, then vim launches with the initial text (the string initial_text); however, I am unable to edit or quit because pressing escape prints a blue ^[ at the position of the cursor, b prints a blue b, and so on.

When editor is instead set to "/Users/jason/bin/mate" (the TextMate command-line utility), TextMate launches, without the initial text. I can edit and save the document, and when I quit, the application reads in the initial text (which should have been overwritten).

I am puzzled since this code is in a printed book so it should work. Am I using the wrong strings for the editor variable?

titaniumdecoy
18th October 2009, 21:12
Since QProcess does not emulate a shell I figured that I might need to provide one so I tried the following:


QProcess::execute("/bin/sh", QStringList() << "-c" << (QString("'") + "vim" + " " + fileName + "'"));

However, when I run this, it prints:


/bin/sh: vim /var/folders/1R/1RaTXd1DHFu0ASTg0KYfqE+++TM/-Tmp-/qt_temp.JL2217: No such file or directory

However, if I execute the command "/bin/sh -c 'vim var/folders/1R/1RaTXd1DHFu0ASTg0KYfqE+++TM/-Tmp-/qt_temp.JL2217'" the vim editor opens and allows me to edit a new file.

Help!