Having problem with Notepad app?



  • Notepad app, sounds fantastic but it's so simple, I only have 3 files, I compile and getting some errors, somebody help me.

    notepad.h

    @
    #ifndef NOTEPAD_H
    #define NOTEPAD_H

    #include <QtGui>

    class Notepad : public QMainWindow
    {
    Q_OBJECT

    public:
    Notepad();

    private slots:
    void open();
    void save();
    void quit();

    private:
    QTextEdit *textEdit;
    QAction *openAction;
    QAction *saveAction;
    QAction *exitAction;
    QMenu *fileMenu;
    };

    #endif
    @

    notepad.cpp

    @
    #include "notepad.h"

    Notepad::Notepad()
    {
    openAction = new QAction(tr("&Open"), this);
    saveAction = new QAction(tr("&Save"), this);
    exitAction = new QAction(tr("E&xit"), this);

    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
    connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);
    
    textEdit = new QTextEdit;
    setCentralWidget(textEdit);
    
    setWindowTitle(tr("Notepad"));
    

    }
    @

    main.cpp

    @
    #include <QtGui/QApplication>
    #include "notepad.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Notepad *note = new Notepad;
    note->show();
    return a.exec();
    }
    @

    Its error:

    !http://nn0.upanh.com/b5.s1.d4/eea0527fdf7b5b61c575404df5e51e17_46009170.h1.png(error)!



  • You have declared the three slots in notepad.h. You need to write the definition of the same in your implementation file i.e notepad.cpp
    @
    void Notepad::open()
    {
    //do something
    }

    void Notepad::save()
    {
    //do something
    }

    void Notepad::quit()
    {
    //do something
    }@



  • oh, I will review it, thanks :)

    ps:

    @
    connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
    connect(exitAction, SIGNAL(triggered()), qApp , SLOT(quit()));
    @

    I don't know how difference between this and qApp ...



  • this is a pointer to the object you are in - in this case it's the Notepad object. aApp is a convenience macro that returns a pointer to the QApplication object.

    The recent docs with the "Getting started with Qt tutorial":/doc/qt-4.8/gettingstartedqt.html have some additions regarding the missing methods.


Log in to reply
 

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