Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在QT-MVC应用程序中创建模型_C++_Qt_Thrift - Fatal编程技术网

C++ 如何在QT-MVC应用程序中创建模型

C++ 如何在QT-MVC应用程序中创建模型,c++,qt,thrift,C++,Qt,Thrift,我有三个小程序。一个是MessageGenerator(我在其中创建了新的文本消息),下一个是MessageStorage(在后台运行的服务器,它有std::vector用于保存新对象)和MessageViewer(这应该是一个视图模型,用于显示来自服务器的std::vector的对象)。问题是我不知道怎么做。我知道我应该创建一些模型,并从服务器将我的std::vector传递给他,但如何做到呢 Apache旧文件 struct Log { 1:string message } ty

我有三个小程序。一个是MessageGenerator(我在其中创建了新的文本消息),下一个是MessageStorage(在后台运行的服务器,它有std::vector用于保存新对象)和MessageViewer(这应该是一个视图模型,用于显示来自服务器的std::vector的对象)。问题是我不知道怎么做。我知道我应该创建一些模型,并从服务器将我的std::vector传递给他,但如何做到呢

Apache旧文件

struct Log
{
    1:string message
}


typedef list<Log> LogsModel

service MessageStorage
{
    void messageAppended(1:string message),
    void messageRemoved(1:i32 index),
    i32 storageSize(),
    void addMessage(1:string message),
    string getMessage()
}
结构日志 { 1:字符串消息 } 类型定义列表日志模型 服务消息存储 { 附加void message(1:字符串消息), 已删除无效消息(1:i32索引), i32 storageSize(), void addMessage(1:字符串消息), 字符串getMessage() } 服务器文件(尚未完成)

//此自动生成的框架文件演示了如何构建服务器。
//您应该将其复制到另一个文件名,以避免覆盖它。
#包括
#包括“MessageStorage.h”
#包括“LogViewerServices\u types.h”
#包括
#包括
#包括
#包括
使用名称空间::apache::thrift;
使用namespace::apache::thrift::protocol;
使用namespace::apache::thrift::transport;
使用namespace::apache::thrift::server;
使用boost::shared\u ptr;
MessageStorageHandler类:虚拟公共MessageStorageIf{
公众:
LogsModel*LogsModel=新LogsModel;
MessageStorageHandler(){
//您的初始化在这里进行
}
附加void message(const std::string和message){
getMessage(消息);
}
已删除无效消息(常量int32_t索引){
对于(int i=0;isize();i++){
如果(i=索引){
logsModel[i].擦除(logsModel->begin()+索引);
}
}
标准::不能推回(*log);
std::cout text()).toStdString();
ui->leu message->clear();
boost::共享的ptr套接字(新的TSocket(“localhost”,9090));
boost::共享ptr传输(新的TBufferedTransport(套接字));
boost::共享ptr协议(新的TBinaryProtocol(传输));
尝试
{
MessageStorageClient(协议);
传输->打开();
client.addMessage(message);
}  
捕获(const std::exception&e)
{

qCritical()看起来你需要学习一些基础知识,虽然节俭在你的项目中有一定的作用,但这似乎根本不是节俭的问题。请正确使用标记,不要将它们放在标题中-我们有专门的标记用于此目的。
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it. 

#include <iostream>
#include "MessageStorage.h"
#include "LogViewerServices_types.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

class MessageStorageHandler : virtual public MessageStorageIf {
public:
  LogsModel *logsModel = new LogsModel;

  MessageStorageHandler() {
   // Your initialization goes here
  }

  void messageAppended(const std::string& message) {
    getMessage(message);
  }

  void messageRemoved(const int32_t index) {
      for(int i = 0; i < logsModel->size(); i++) {
          if(i = index) {
              logsModel[i].erase(logsModel->begin() + index);
          }
      }

  std::cout << "Item deleted" << std::endl;
  }

  int32_t storageSize() {
      std::cout << "Rozmiar vectora:\t" << logsModel->size();
      return logsModel->size();
  }

  void addMessage(const std::string& message) {
      Log *log = new Log;
      log->message = message;

      logsModel->push_back(*log);
      std::cout << "Tresc wiadomosc:\t" << message << std::endl;

      messageAppended(message);
  }

  void getMessage(std::string& _return) {

  }

};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<MessageStorageHandler> handler(new MessageStorageHandler());
  shared_ptr<TProcessor> processor(new MessageStorageProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  std::cout << "Server started..." << std::endl;
  server.serve();
  return 0;
}
#include "messagegenerator.h"
#include "ui_messagegenerator.h"

#include "LogViewerServices_types.h"
#include "MessageStorage.h"

#include <QDebug>

#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

MessageGenerator::MessageGenerator(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MessageGenerator)
{
    ui->setupUi(this);

    connect(ui->le_message, &QLineEdit::returnPressed, this, [this]
    {
        if(ui->le_message->text().isEmpty())
        {
            qDebug() << "Nie ma tresci";
            return;
        }

        std::string message = (ui->le_message->text()).toStdString();
        ui->le_message->clear();

        boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
        boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
        boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

        try
        {
            MessageStorageClient client(protocol);
            transport->open();

            client.addMessage(message);
        }  

        catch(const std::exception &e)
        {
            qCritical() << "Error" << e.what();
        }
    });
}

MessageGenerator::~MessageGenerator()
{
    delete ui; 
}