在我的程序中,我的用户可以从任何地方复制一串文本并将其粘贴到我的程序中。我使用简单的 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;
}
复制的文本在剪贴板查看器中显示正常,但无论如何我的程序都无法访问它:
为什么 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/