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


  • Moderators

    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.


  • Moderators

    As the error message already says: you put load() in private section in your class, put it in public.



  • @jsulm

    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*)'
    

  • Moderators

    Did you define load() method?
    I mean (usually) in cpp file:

    void MainWindow::load(char*)
    {
    
    }
    

    How experienced are you with C++?



  • @jsulm
    No, i don't...
    What should i write in the method?

    My experiences are not so good... I read a lot of book... Reading the Code is no Problem but writing is very hard for me...


  • Moderators

    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?


  • Moderators

    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);
    


  • @jsulm
    Yes. I tried before your post and it works!

    Thank you all...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.