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

时间:2013-03-05 20:59:40

标签: c++ qt user-interface

我希望将此代码放在一个文件中,但无法弄清楚如何操作。我知道这样做可能不是一个好习惯,但我正在尝试学习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;
};

4 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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);
}

答案 2 :(得分:1)

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

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

mainwindow.h

mainwindow.cpp

main.cpp

答案 3 :(得分:0)

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

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

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

相关问题