【问题标题】:Qt example notepad program not displaying any controlsQt 示例记事本程序不显示任何控件
【发布时间】:2012-09-16 06:45:29
【问题描述】:

我正在关注 QtSDK 中列出的 Qt 示例,标题为:“Qt 入门编程”。我的问题是程序没有显示任何对象。

我的 main.cpp 看起来像这样:

#include <QtGui>

#include <notepad.h>

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

    QVBoxLayout layout;

    Notepad notepad();

    QWidget window;
    window.setLayout(&layout);

    window.show();

    return app.exec();
}

'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 *text_edit;

        QAction *open_action;
        QAction *save_action;
        QAction *exit_action;

        QMenu *file_menu;
};

#endif // NOTEPAD_H

“notepad.cpp”文件:

#include "notepad.h"

Notepad::Notepad()  {
    open_action = new QAction(tr("&Open"), this);
    save_action = new QAction(tr("&Save"), this);
    exit_action = new QAction(tr("&Exit"), this);

    connect(open_action, SIGNAL(triggered()), this, SLOT(open()));
    connect(save_action, SIGNAL(triggered()), this, SLOT(save()));
    connect(exit_action, SIGNAL(triggered()), qApp, SLOT(quit()));

    file_menu = menuBar()->addMenu(tr("&File"));
    file_menu->addAction(open_action);
    file_menu->addAction(save_action);
    file_menu->addSeparator();
    file_menu->addAction(exit_action);

    text_edit = new QTextEdit;
    setCentralWidget(text_edit);

    setWindowTitle(tr("Notepad"));
}

void Notepad::open()  {
    ...
}

void Notepad::save()  {
    ...
}

void Notepad::quit()  {
}

我们将不胜感激任何解决问题的帮助。

编辑

我最初的问题问我为什么会因为没有实现“退出”功能而出现编译时错误,以防你想知道:)。

【问题讨论】:

  • 为什么notepad.cpp中没有Notepad::quit()方法??!!
  • 你忘记写quit()代码了吗?
  • Opps,已修复,现在我的问题似乎是窗口中没有任何内容。
  • 请更新您帖子中的问题。
  • @KamranAmini 好的,完成。

标签: c++ qt compilation


【解决方案1】:

您必须实现在 notepad.h 中定义为私有插槽的 quit() 方法。未解决的错误是链接错误,它告诉您链接器没有找到函数声明的任何实现。将此添加到 notepad.cpp :

void Notepad::quit()  {
    ...
}

对于新问题:

试试 notepad.show()。您的 MainWindow 可以是记事本对象,因为它是 QMainWindow。

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

    Notepad notepad();
    notepad.show();

    return app.exec();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    相关资源
    最近更新 更多