如何在Qt中复制和粘贴自定义对象

Ima*_*gab 5 c++ qt

我正在尝试将自定义对象复制并粘贴QListView到同一应用程序的另一个窗口中,但是此类仅返回一个std::ostream,并且我无权修改此类。

我怎样才能把这个的std :: ostream的中QDatastreamQClipboard进行复制和粘贴的过程?

void ProjectModel::copymodel(QModelIndex idx){

  const mo::model model(modelList.at(idx.row()));

  std::ostream *stream;
  stream << &model;

  QByteArray itemData;
  QDataStream datastream(&itemData, QIODevice::WriteOnly);

  QMimeData *mimeData = new QMimeData;
  mimeData->setData("test", itemData);

  QApplication::clipboard()->setMimeData(mimeData);}
Run Code Online (Sandbox Code Playgroud)

自定义类mo::model返回std::stream如下:

namespace mo {

::std::ostream& operator<< (::std::ostream& o, const model& i){
  o << ::std::endl << "name: " << i.name ();
  if (i.box ())
  {
    o << ::std::endl << "box: " << *i.box ();
  }

  for (model::basic_element_const_iterator
       b (i.basic_element ().begin ()), e (i.basic_element ().end ());
       b != e; ++b)
  {
    o << ::std::endl << "basic_element: " << *b;
  }

  for (model::net_const_iterator
       b (i.net ().begin ()), e (i.net ().end ());
       b != e; ++b)
  {
    o << ::std::endl << "net: " << *b;
  }

  for (model::com_position_const_iterator
       b (i.com_position ().begin ()), e (i.com_position ().end ());
       b != e; ++b)
  {
    o << ::std::endl << "com_position: " << *b;
  }

  if (i.com_truthtable ())
  {
    o << ::std::endl << "com_truthtable: " << *i.com_truthtable ();
  }

  if (i.com_pos_titles ())
  {
    o << ::std::endl << "com_pos_titles: " << *i.com_pos_titles ();
  }

  return o;}}
Run Code Online (Sandbox Code Playgroud)