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

C++ 如何将此qt程序放入一个源代码文件中,c++,qt,user-interface,C++,Qt,User Interface,我想将此代码保存在一个文件中,但不知道如何保存。我知道这样做可能不是一个好的做法,但我正在尝试学习qt,如果它在一个文件中,我会发现更容易理解信息 #include <QApplication> #include <QMainWindow> #include <QPushButton> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_O

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

#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.cpp

#include "mainwindow.h"

#include <QApplication>

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

    MainWindow mainWindow;
    mainWindow.showMaximized();
    return app.exec();
}
#包括“mainwindow.h”
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
主窗口主窗口;
mainWindow.showMaximized();
返回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”
#包括
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
//创建按钮,使“this”成为父级
m_按钮=新的QPushButton(“我的按钮”,此按钮);
//设置按钮的大小和位置
m_按钮->设置几何体(QRect(QPoint)(100100),
QSize(200,50));
//将按钮信号连接到适当的插槽
连接(m_按钮,信号(释放()),此,插槽(把手按钮());
}
void主窗口::把手按钮()
{
//更改文本
m_按钮->设置文本(“示例”);
//调整大小按钮
m_按钮->调整大小(100100);
}
这是主窗口

#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;
};
#定义主窗口
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
专用插槽:
无效把手按钮();
私人:
QPushButton*m_按钮;
};
#include
实质上是获取您选择的任何文件的内容,并将其复制/粘贴到该位置。然后,编译器从文件的顶部开始,一直到底部

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

main window.h

mainwindow.cpp

main.cpp

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

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

main window.h

mainwindow.cpp


main.cpp

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

#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();
}
#包括
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
专用插槽:
无效把手按钮();
私人:
QPushButton*m_按钮;
};
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
//创建按钮,使“this”成为父级
m_按钮=新的QPushButton(“我的按钮”,此按钮);
//设置按钮的大小和位置
m_按钮->设置几何体(QRect(QPoint)(100100),
QSize(200,50));
//将按钮信号连接到适当的插槽
连接(m_按钮,信号(释放()),此,插槽(把手按钮());
}
void主窗口::把手按钮()
{
//更改文本
m_按钮->设置文本(“示例”);
//调整大小按钮
m_按钮->调整大小(100100);
}
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
主窗口主窗口;
mainWindow.showMaximized();
返回app.exec();
}

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

#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();
}
#包括
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
专用插槽:
无效把手按钮();
私人:
QPushButton*m_按钮;
};
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
//创建按钮,使“this”成为父级
m_按钮=新的QPushButton(“我的按钮”,此按钮);
//设置按钮的大小和位置
m_按钮->设置几何体(QRect(QPoint)(100100),
QSize(200,50));
//将按钮信号连接到适当的插槽
连接(m_按钮,信号(释放()),此,插槽(把手按钮());
}
void主窗口::把手按钮()
{
//更改文本
m_按钮->设置文本(“示例”);
//调整大小按钮
m_按钮->调整大小(100100);
}
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
主窗口主窗口;
mainWindow.showMaximized();
返回app.exec();
}

在主文件中定义新类通常不是一个好主意。通常情况下,您需要在各自的文件中添加新类,或者将几个相关类放在一个单独的文件中。您可以通过谷歌搜索大量与此相关的最佳实践资源。我建议你花点时间读书

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

#包括
#包括
#包括
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
专用插槽:
无效把手按钮();
私人:
QPushButton*m_按钮;
};
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
主窗口主窗口;
mainWindow.showMaximized();
返回app.exec();
}
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
//创建按钮,使“this”成为父级
m_按钮=新的QPushButton(“我的按钮”,此按钮);
//设置按钮的大小和位置
m_按钮->设置几何体(QRect(QPoint)(100100),
QSize(200,50));
//将按钮信号连接到适当的插槽
连接(m_按钮,信号(释放()),此,插槽(把手按钮());
}
void主窗口::把手按钮()
{
//更改文本
m_按钮->设置文本(“示例”);
//调整大小按钮
m_按钮->调整大小(100100);
}

在同一个类中定义新类通常不是一个好主意