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

时间:2019-06-17 08:44:06

标签: c++ qt

我想将自定义对象从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返回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 个答案:

没有答案