如果从记事本/ Chrome复制,QClipboard无法获取剪贴板数据

时间:2015-03-30 12:40:05

标签: c++ qt windows-8.1 clipboard

在我的程序中,我的用户可以从任何地方复制一串文本并将其粘贴到我的程序中。我使用简单的QApplication::clipboard()->text();函数,一切都按预期工作。但是,我的一些用户在尝试复制和粘贴Windows 8.1时遇到问题

以下是我从粘贴功能访问剪贴板文本的方法:

QString clipboard = QApplication::clipboard()->text();

//check if the clipboard is empty
if(QApplication::clipboard()->text().isEmpty())
    return;

//do something with clipboard

但如果文本是从记事本或Chrome中复制的,则文本始终为空。 Windows 7用户没有遇到任何问题。并非所有Windows 8用户都有这个问题,它只是少数但问题是一致的。从其他一些随机位置或我的程序本身复制时,剪贴板工作正常。

我已尝试使用mimeData。使用函数formats()时,只有纯文本是可用格式,但文本始终为空。

从记事本/ Chrome中复制的文本在剪贴板查看器和内容中显示正常,可以粘贴到其他程序的其他位置。

复制和粘贴是我的计划中非常重要的功能,我的用户无法通过记事本或Chrome进行复制和粘贴。

有什么想法吗?谢谢你的时间。 :)

编辑:我尝试使用" windows"风格技巧。没有变化。这是我目前仍然没有工作的代码:

QString clipboard = QApplication::clipboard()->text();

//check if the clipboard is empty
if(clipboard.isEmpty())
{
    //might not actually be empty. Check using the other technique
    if (IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(NULL))
        {
            HGLOBAL hGlobal = GetClipboardData(CF_TEXT) ;//hGlobal is NULL
            if (hGlobal != NULL)//This never gets called because it is NULL
            {
                LPTSTR lpszData = (LPTSTR) GlobalLock(hGlobal) ;
                if (lpszData != NULL)
                {
                    clipboard.fromLocal8Bit((const char *)lpszData);
                    GlobalUnlock(hGlobal) ;
                }
            }
            CloseClipboard() ;
        }

    if(clipboard.isEmpty())
        return;
}

复制的文本在剪贴板查看器中显示正常,但无论如何,我的程序都无法访问它: enter image description here

为什么GetClipboardData()没有选择任何东西?同样,复制的文本可以粘贴在我尝试的任何其他程序中......而不是我的。但如果从其他地方复制,它就没有问题。

2 个答案:

答案 0 :(得分:1)

编辑:使用此版本,当用户将文本和文本复制到剪贴板时,函数会使用QFile将文本复制到内部文本文件而不是直接复制到您的程序。另一个函数将文本从内部文本文件复制到您的程序。通过这种方式,我认为你的程序不必直接处理剪贴板中的文本

clipboard.h

#ifndef CLIPBOARD_H
#define CLIPBOARD_H

#include <QDialog>
#include <QClipboard>
#include <QLabel>
#include <QHBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QFile>
#include <QTextStream>

class ClipBoard : public QDialog {
    Q_OBJECT

public:
    explicit ClipBoard(QWidget *parent = 0);
    ~ClipBoard();

private slots:
    //the functions that will copy and paste text from the text file
    void copyToTextFile();
    void paste();

private:

    QPushButton *button;
    QTextEdit *edit;
    QString textFromClipBoard;
    QClipboard *clipBoardText;

    QString clipboard;

};

#endif // CLIPBOARD_H

clipboard.cpp

#include "clipboard.h"
#include "ui_clipboard.h"

ClipBoard::ClipBoard(QWidget *parent) : QDialog(parent) {

    button = new QPushButton("&Paste Copied Text");

    edit = new QTextEdit;

    /*when user copies text and text gets to clipboard, the text is copied 
      from clipboard to a text file then copied from the text file to the   
      program*/
    connect(button, SIGNAL(clicked()), this, SLOT(copyToNotepad()));
    connect(button, SIGNAL(clicked()), this, SLOT(paste()));


    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(button);
    layout->addWidget(edit);


    setLayout(layout);

}

/*This function copies text from the clipboard to an internal text file 
  created by the program*/
void ClipBoard::copyToTextFile() {
    clipboard = QApplication::clipboard()->text();

    QFile output("out.txt");
    if (output.open(QIODevice::ReadWrite | QFile::Truncate |   
    QIODevice::Text)) {
        QTextStream out(&output);
        out << clipboard;
    }
}

/*This function then copies the text from the internal text file and pastes 
  it to the text edit. So the program doesn't have to deal directly with the 
  clipboard*/
void ClipBoard::paste() {

    QFile input("out.txt");
    if (input.exists()) {
        if (input.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&input);
            clipboard = in.readAll();
        }
    }

    edit->setText(clipboard);
}

ClipBoard::~ClipBoard() {

}

的main.cpp

#include "clipboard.h"
#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    ClipBoard w;
    w.show();

    return a.exec();
}

查看并与您的部分代码进行比较,看看是否有错误。但至于你如何声称它适用于某些Windows 8.1系统,而不是其他人困扰我。

答案 1 :(得分:0)

我有类似的问题, 我的解决方案就是睡觉。

void MainWindow::on_clipboard_change(){

    QThread::msleep(1); //without this line I get allways empty clipboard

    QString text = QGuiApplication::clipboard()->text();

    qDebug() << "clipboard change event triggered ( " << text << " )";

}
相关问题