Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. String trouble in clipboard
Qt 6.11 is out! See what's new in the release blog

String trouble in clipboard

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 832 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Roy44R Offline
    Roy44R Offline
    Roy44
    wrote on last edited by
    #1

    Hi,

    I would like to implement a copy/paste, so I useQClipboard.

    For the paste I get a file path like this:

    const QClipboard *clipboard = QApplication::clipboard();
                const QMimeData *mimeData = clipboard->mimeData();
    
                foreach(QString sMimeFormat, mimeData->formats())
                {
                    if(sMimeFormat.contains("AutoCAD.r",Qt::CaseInsensitive))
                    {
                        QString sFilename((QChar*)mimeData->data(sMimeFormat).data());
                        if(QFile::exists(sFilename))
                        {
                            pasteFile(sFilename, pDb, pApp, pUserIO);
                        }
                        return;
                    }
                }
    

    And It works, so I try to create a copy but it doesnt works, the file name is unreadable.
    I create the copy like this:

    QString sOutputFile = QDir::tempPath() + "/" + "copyDWG.dwg";
        OdStreamBufPtr streamBuf = pCoreApp->createFile(qtStringToOdaString(sOutputFile), Oda::kFileWrite, Oda::kShareDenyWrite, Oda::kCreateAlways);
        pDbDest->writeFile(streamBuf, OdDb::kDwg, OdDb::vAC21);
    
        QMimeData *pMimeData = new QMimeData;
        QByteArray baData;baData.append(sOutputFile);
        pMimeData->setData("AutoCAD.r15", baData);
        QClipboard *clipboard = (QClipboard *)QApplication::clipboard();
        clipboard->setMimeData(pMimeData);
    

    I think it's wide char problem but I don't know how to correct my copy.
    What I did wrong ?

    Thanks,
    Sorry for my english

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! The following works for me on Linux:

      auto const * const mimeData = QApplication::clipboard()->mimeData();
      auto const formats = mimeData->formats();
      
      for (auto const &i: formats) {
          if (i=="text/plain") {
              auto const sourceUrl = QString::fromUtf8(mimeData->data(i));          // "file:///home/pw/test.dxf"
              if (sourceUrl.endsWith(".dxf")) {
                  auto const sourceFilePath = QUrl(sourceUrl).toLocalFile();        // "/home/pw/test.dxf"
                  auto const sourceFileName = QFileInfo(sourceFilePath).fileName(); // "test.dxf"
                  auto const targetPath("/home/pw/tmp/");                           // "/home/pw/tmp/"
                  auto const targetFilePath = targetPath + sourceFileName;          // "/home/pw/tmp/test.dxf"
                  if (!QFile::copy(sourceFilePath, targetFilePath))
                      throw 666;
              } else {
                  qDebug() << "Nothing to copy";
              }
          }
      }
      
      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved