如何将std :: ostream转换为QDataStream

时间:2019-06-17 14:03:02

标签: c++ qt operator-overloading

我试图将自定义对象从QListView复制并粘贴到同一应用程序的另一个窗口中,但是此类仅返回std :: ostream,并且我无权修改此类。 我如何将这个std :: ostream放在QDatastream中,然后将QClipboard放在复制和粘贴过程中?

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

自定义类mo :: model retun 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;}}

0 个答案:

没有答案