내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
다음은 모델(Model)에서의 작업이 자식 스레드에서 처리되는 간단한 Qt 기반 MVC 프레임워크 예제입니다. 이 예에는 기본 보기, 컨트롤러 및 모델이 포함됩니다.
프로젝트 구조는 다음과 같습니다.
MyMVCApp/
├── main.cpp
├── model.h
├── model.cpp
├── view.h
├── view.cpp
├── controller.h
├── controller.cpp
├── mainwindow.ui
├── mainwindow.h
├── mainwindow.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
보기 부분은 주로 데이터 표시 및 사용자 상호 작용을 담당합니다.우리는 간단한 것을 사용할 것입니다QMainWindow
뷰로서 버튼과 라벨이 포함되어 있습니다.
#ifndef VIEW_H
#define VIEW_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
class View : public QWidget
{
Q_OBJECT
public:
explicit View(QWidget *parent = nullptr);
QPushButton *getButton() const;
QLabel *getLabel() const;
private:
QPushButton *button;
QLabel *label;
QVBoxLayout *layout;
};
#endif // VIEW_H
#include "view.h"
View::View(QWidget *parent) : QWidget(parent)
{
button = new QPushButton("Click me", this);
label = new QLabel("Initial text", this);
layout = new QVBoxLayout(this);
layout->addWidget(button);
layout->addWidget(label);
setLayout(layout);
}
QPushButton* View::getButton() const
{
return button;
}
QLabel* View::getLabel() const
{
return label;
}
존재하다mainwindow.h
, 우리는 포함할 것입니다view.h
그리고 이를 메인 창의 일부로 만듭니다.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "controller.h"
#include "view.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
Controller *controller;
View *view;
};
#endif // MAINWINDOW_H
존재하다mainwindow.cpp
, 뷰를 초기화하고 컨트롤러와 뷰의 신호 및 슬롯을 연결합니다.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
controller(new Controller(this)),
view(new View(this))
{
ui->setupUi(this);
setCentralWidget(view);
// Connect button click to controller slot
connect(view->getButton(), &QPushButton::clicked, controller, &Controller::handleButtonClicked);
// Connect controller signal to update label
connect(controller, &Controller::updateLabel, view->getLabel(), &QLabel::setText);
}
MainWindow::~MainWindow()
{
delete ui;
}
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QObject>
#include "model.h"
class Controller : public QObject
{
Q_OBJECT
public:
explicit Controller(QObject *parent = nullptr);
signals:
void updateLabel(const QString &text);
public slots:
void handleButtonClicked();
private:
Model *model;
};
#endif // CONTROLLER_H
#include "controller.h"
Controller::Controller(QObject *parent) : QObject(parent), model(new Model(this))
{
// Connect model signal to controller signal
connect(model, &Model::dataProcessed, this, &Controller::updateLabel);
}
void Controller::handleButtonClicked()
{
model->processData();
}
#ifndef MODEL_H
#define MODEL_H
#include <QObject>
#include <QThread>
class Model : public QObject
{
Q_OBJECT
public:
explicit Model(QObject *parent = nullptr);
~Model();
void processData();
signals:
void dataProcessed(const QString &result);
private:
QThread workerThread;
};
#endif // MODEL_H
#include "model.h"
#include <QTimer>
class Worker : public QObject
{
Q_OBJECT
public slots:
void doWork()
{
// Simulate long-running task
QThread::sleep(2);
emit resultReady("Data processed in background thread");
}
signals:
void resultReady(const QString &result);
};
Model::Model(QObject *parent) : QObject(parent)
{
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Model::operate, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Model::dataProcessed);
workerThread.start();
}
Model::~Model()
{
workerThread.quit();
workerThread.wait();
}
void Model::processData()
{
emit operate();
}
존재하다model.cpp
하나를 정의하십시오Worker
하위 스레드에서 작업을 수행하기 위한 클래스입니다.
class Worker : public QObject
{
Q_OBJECT
public slots:
void doWork()
{
// Simulate long-running task
QThread::sleep(2);
emit resultReady("Data processed in background thread");
}
signals:
void resultReady(const QString &result);
};
이 예에는 뷰, 컨트롤러 및 모델이 포함됩니다.
뷰는 데이터 표시와 사용자 상호 작용을 담당하고, 컨트롤러는 사용자 입력을 처리하고 뷰를 업데이트하며, 모델은 하위 스레드의 데이터를 처리하고 컨트롤러에 뷰를 업데이트하도록 알립니다.
필요에 따라 이 프레임워크를 확장하여 더 많은 기능과 복잡성을 추가할 수 있습니다.
이 예제가 도움이 되기를 바랍니다!