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



  • 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.