繁体   English   中英

如何将这个qt程序放入一个源代码文件中

[英]How can I put this qt program into one source code file

我想将此代码保存在一个文件中,但不知道如何处理。 我知道这样做可能不是一个好习惯,但是我正在尝试学习qt,如果将信息放在一个文件中,它将发现更容易理解该信息。

这是main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow mainWindow;
    mainWindow.showMaximized();
    return app.exec();
}

这是mainwindow.cpp

#include "mainwindow.h"

#include <QCoreApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // Create the button, make "this" the parent
    m_button = new QPushButton("My Button", this);
    // set size and location of the button
    m_button->setGeometry(QRect(QPoint(100, 100),
                             QSize(200, 50)));

    // Connect button signal to appropriate slot
    connect(m_button, SIGNAL(released()), this, SLOT(handleButton()));
}

void MainWindow::handleButton()
{
    // change the text
    m_button->setText("Example");
    // resize button
    m_button->resize(100,100);
}

这是mainwindow.h

#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

private slots:
    void handleButton();

private:
    QPushButton *m_button;
};

通过将所有内容复制到一个文件中。

#include <QApplication>
#include <QMainWindow>
#include <QPushButton>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

private slots:
    void handleButton();

private:
    QPushButton *m_button;
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // Create the button, make "this" the parent
    m_button = new QPushButton("My Button", this);
    // set size and location of the button
    m_button->setGeometry(QRect(QPoint(100, 100),
                             QSize(200, 50)));

    // Connect button signal to appropriate slot
    connect(m_button, SIGNAL(released()), this, SLOT(handleButton()));
}

void MainWindow::handleButton()
{
    // change the text
    m_button->setText("Example");
    // resize button
    m_button->resize(100,100);
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow mainWindow;
    mainWindow.showMaximized();
    return app.exec();
}

在与main相同的文件中定义新类通常不是一个好主意。 通常,您希望每个新类都在各自的文件中,或者您希望将几个相关的类放到单独的文件中。 您可以在Google上找到大量与最佳做法相关的资源。 我建议您花一些时间阅读。

但是,既然您问了...,下面是您将如何进行示例的操作。 如果您没有在main之上定义类,则编译器会抱怨,因为它不知道“ MainWindow”是什么。

#include <QApplication>
#include <QMainWindow>
#include <QPushButton>


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

private slots:
    void handleButton();

private:
    QPushButton *m_button;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow mainWindow;
    mainWindow.showMaximized();
    return app.exec();
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // Create the button, make "this" the parent
    m_button = new QPushButton("My Button", this);
    // set size and location of the button
    m_button->setGeometry(QRect(QPoint(100, 100),
                             QSize(200, 50)));

    // Connect button signal to appropriate slot
    connect(m_button, SIGNAL(released()), this, SLOT(handleButton()));
}

void MainWindow::handleButton()
{
    // change the text
    m_button->setText("Example");
    // resize button
    m_button->resize(100,100);
}

#include本质#include获取您选择的任何文件的内容,并将其复制/粘贴到该位置。 然后,编译器从文件的顶部开始,一直向下到底部。

知道这一点,您应该能够按照文件包含的顺序复制粘贴它们的内容。

mainwindow.h

mainwindow.cpp

main.cpp

简短的答案是不要这样做,您应该在自己的头文件中定义一个类,并在要在主目录中运行它时将其#include包含到主目录中。 这使您可以按自己的意愿在整个程序中重用该类,它是面向对象编程的宗旨之一,是可重用的代码。 例如

class A
{
public:
    A();
    ~A();
    void somePublicMethod();
private:
    void somePrivateMethod();
};

如果您在将该类编译为目标代码时将其包含在主类中(假设您知道在.cpp文件中实现该类),则当所有目标文件都链接在一起以创建程序时,链接器基本上会用文件中包含的所有目标代码都将被完全编译。 我建议您阅读更多有关编译和链接的内容,从本质上讲,它可以分为预处理,编译和链接三个阶段。 了解有关面向对象编程的更多信息,并阅读为什么将它们全部都放入一个文件中不是一个好主意。 每个类都应位于自己的自包含.h文件中(除非它是紧密耦合的类),因此您可以根据需要包含它们。 希望这会有所帮助,并与Qt玩得开心

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM