0

我想把这段代码放在一个文件中,但不知道怎么做。我知道这样做可能不是一个好习惯,但我正在尝试学习 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();
}

这是主窗口.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);
}

这是主窗口.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;
};
4

4 回答 4

2

在与您的主文件相同的文件中定义新类通常不是一个好主意。通常,您希望每个新类都在它们自己的文件中,或者您希望将几个相关的类放在一个单独的文件中。您可以搜索大量与最佳实践相关的资源。我建议你花一些时间阅读。

但是既然你问了......下面是你如何为你的例子做的。如果你没有在 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);
}
于 2013-03-05T21:25:04.803 回答
2

只需将所有内容复制到一个文件中即可。

#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();
}
于 2013-03-05T21:14:01.537 回答
1

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

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

mainwindow.h

mainwindow.cpp

main.cpp

于 2013-03-05T21:10:14.027 回答
0

简短的回答是不要这样做,你应该在它自己的头文件中定义一个类,并在你想在主文件中运行它时将它#include 到主文件中。这允许您在整个程序中重用您认为合适的类,这是面向对象编程的原则之一,可重用代码。例如

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

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

于 2013-03-05T22:36:12.873 回答