How I can create a text editor in QT C++ which I can open any text document by right-clicking it and open with my application.
First receive the file path as positional argument main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCommandLineParser parser;
parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open."));
parser.process(a);
MainWindow w;
w.show();
QStringList filename=parser.positionalArguments();
w.OpenFileInText(filename[0]);
return a.exec();
}
Then pass the file to open file method. Below is the method in MainWindow.cpp
void MainWindow::OpenFileInText(QString strFilePath)
{
ui->plainTextEdit->clear();
QFile file(strFilePath);
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}
QTextStream in(&file);
while(!in.atEnd())
{
QString line = in.readLine();
ui->plainTextEdit->appendPlainText(line) ;
}
file.close();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.