String trouble in clipboard

  • 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())
                        QString sFilename((QChar*)mimeData->data(sMimeFormat).data());
                            pasteFile(sFilename, pDb, pApp, pUserIO);

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

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

    Sorry for my english

  • 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";

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.