0

このコードを 1 つのファイルにまとめたいのですが、方法がわかりません。そうするのは良い習慣ではないかもしれませんが、私はqtを学ぼうとしています.1つのファイルにあれば、情報を理解しやすくなります.

これは 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

4 に答える 4

2

通常、メインと同じファイルに新しいクラスを定義することはお勧めできません。通常、新しいクラスをそれぞれ独自のファイルに入れるか、いくつかの関連するクラスを別々のファイルにまとめます。このためのベストプラクティスに関連してグーグルで検索できるリソースがたくさんあります。少し時間をかけて読んでみることをお勧めします。

しかし、あなたが尋ねたので...以下はあなたがあなたの例のためにそれをする方法です。メインの上にクラスを定義しないと、「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

すべてを 1 つのファイルにコピーするだけです。

#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 に答える