Open file with double click, syntax error
-
Hi everyone!
I want to open a .htm file in my application by double clicking.
So i have written These Code in main.cpp:for ( int i = 1; i < argc; ++i ) mainWin.load(argv[i]);
And These in mainwindow.h
void load(char *fileName);
And now i'm getting These Errors:
C:\Users\Henrik\Documents\TextPad\mainwindow.h:147: Fehler: 'void MainWindow::load(char*)' is private void load(char *fileName); ^
C:\Users\Henrik\Documents\TextPad\main.cpp:28: Fehler: within this context mainWin.load(argv[i]); ^
Does anybody know why the Errors are there and how to fix them?
Thanks,
Henrik -
as the error says the load() method is private.
But you are trying to access it "from the outside" of the class. Declare it as public. -
As the error message already says: you put load() in private section in your class, put it in public.
-
C:\Users\Henrik\Documents\TextPad\main.cpp:28: Fehler: undefined reference to `MainWindow::load(char*)'
C:\Users\Henrik\Documents\build-TextPad-Desktop_Qt_5_5_1_MinGW_32bit2-Debug\debug\moc_mainwindow.cpp:350: Fehler: undefined reference to `MainWindow::load(char*)'
-
Did you define load() method?
I mean (usually) in cpp file:void MainWindow::load(char*) { }
How experienced are you with C++?
-
The method can be empty like in the example I gave.
What you should write there is something you should know as it is your program.
I guess you want to open a file and read its content, right? Then do it using QFile. -
@jsulm
Yes i want to open the file...QString fileName = QFileDialog::getOpenFileName(this, tr("Öffnen..."), QString(), tr("TextPad 1.0 (*.htm);;")); QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); QFile file(fileName, this); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Fehler beim Öffnen der Datei"; } if (document()) { document()->clear(); } document()->setHtml(file.readAll()); file.close();
Something like that?
-
You already pass file path as parameter to the load() method.
In this case there is no need for:QString fileName = QFileDialog::getOpenFileName(this, tr("Öffnen..."), QString(), tr("TextPad 1.0 (*.htm);;")); // This line does not make any sence because you do not use its return value QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);