Qt(C ++)的一些问题

时间:2011-08-05 23:50:25

标签: c++ qt

的main.cpp

#include <QtGui>
#include <QApplication>


int main(int argv, char **args)
{
    QApplication app(argv, args);

    QTextEdit textEdit;
    QPushButton quitButton("Quit");

    QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    QVBoxLayout layout;
    layout.addWidget(&textEdit);
    layout.addWidget(&quitButton);

    QWidget window;
    window.setLayout(&layout);
    window.show();

    return app.exec();        
}

notepad.cpp

#include <QtGui>
#include <QApplication>

class Notepad : public QMainWindow
{


    Notepad::Notepad()
    {
        saveAction = 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"));
    }
    Q_OBJECT

public:
    Notepad();

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

private:
    QTextEdit *textEdit;

    QAction *openAction;
    QAction *saveAction;
    QAction *exitAction;

    QMenu *fileMenu;
};

错误:

  会员记事本(第8行)

额外资格'NotePad ::'

     

notepad :: notepad()无法重载(第32行)

     

带记事本::记事本(第8行)

为什么我会收到这些错误?构造函数看起来很好,类设置看起来很好。但是我收到了这些错误。

2 个答案:

答案 0 :(得分:2)

Notepad类中Notepad::构造函数前面的Notepad()不是必需的。也没有后来的声明,因为你已经完成了这个并且在上面定义了它(尽管是私下的)。您可能需要考虑将其分隔为标头和cpp文件。

您发布的代码仍然存在各种其他问题,但您发布的错误很可能是由我上面提到的引起的。

答案 1 :(得分:1)

  • 您已使用Notepad::
  • 限定内联私有构造函数
  • 您在第二个声明
  • 中错误地将该私有构造函数重载为public
  • Q_OBJECT宏需要在方法和成员之前的类声明中排在第一位。
  • Notepad的每个实例至少有4次内存泄漏?

Perhaps pick up a book?