I made a basic text editor(called 'Note') in Qt on Arch Linux! so I built the project and made an installer using installjammer. now when I type note in terminal it opens the program. Now here is my question: if we use nano or leafpad or mousepad it take the path of the file opens it. Eg. nano /etc/fstab how can I do this in my program? do I need to edit something in the installer or in my codes? HELP ME! pls! ~Thanks!
问题:
回答1:
You might want to read the docs for QCoreApplication. Especially: QStringList QCoreApplication::arguments()
Get the filename from this ist, open the file.
回答2:
Get the passed command line argument either directly in main() from the argv
argument, or through QCoreApplication::arguments(). This is well documented and should be rather easy. The tricky part is actually opening the file. For that, you need to schedule a slot for running right after you call exec() on your QApplication instance. First, create a slot. For example, if you're subclassing QApplication, you can:
class MyApplication: public QApplication {
Q_OBJECT
// ...
private slots:
void checkCmdLine();
// ...
};
In your MyApplication::checkCmdLine() function you get the arguments from QCoreApplication::arguments() and check whether a filename was passed. If yes, you open it.
Now you need to make sure that MyApplication::checkCmdLine() will run right after you call exec() on MyApplication. You do that in your main() function by using QMetaObject::invokeMethod(). For example:
int main(int argc, char* argv[])
{
MyApplication* app = new MyApplication(argc, argv);
// ...
QMetaObject::invokeMethod(app, "checkCmdLine", Qt::QueuedConnection);
app->exec();
// ...
}
If you're not subclassing QApplication, then you can implement the slot in some other QObject subclass and use QMetaObject::invokeMethod() on that instead.