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

标签 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() 没有拾取任何东西?同样,复制的文本可以粘贴到我尝试过的任何其他程序中……只是不是我的。但如果从其他地方复制,它就没有问题。

最佳答案

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

剪贴板.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

剪贴板.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() {

}

主要.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 系统上工作而在其他系统上不工作让我感到困惑。

关于c++ - 如果从记事本/Chrome 复制 QClipboard 无法获取剪贴板数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29346507/

相关文章:

c++ - typeid 结果不匹配

c++ - 如何从 C++ 中的文本文件内容文件扩展名中读取?

android - TextField 文本字段在 QML 上不返回值 | QT 5.12

c++ - 创建一个实现选项卡并可以在 QMainWindow 中用作 "central widget"的类

visual-studio - VS 2012.3 引用 MS Access COM DLL,无法解析 COM 引用错误

c++ - 在派生类中实现虚方法的问题

c++ - 双重标准?为什么只对 char* const& a = "bla"发出警告?

c++ - Qt:Centos 6.7 session 管理错误和符号查找错误

xaml - 绑定(bind)到元素高度加上一个值

c# - 在 Windows 8 中使用 C# 设置菜单 Flyout 背景颜色