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

时间:2017-04-23 14:11:05

标签: c++ qt thrift

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

Apache thrift文件

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()
}

服务器文件(尚未完成)

// 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;
}

和MessageGenerator文件

#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; 
}

0 个答案:

没有答案