- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我的程序中,我的用户可以从任何地方复制一串文本并将其粘贴到我的程序中。我使用简单的 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/
我是编程新手,我开始创建一个简单的记事本,只有 4 个按钮(打开、保存、新建和字体)。 如果我打开或保存我收到一个错误:这是我的代码: //Declare save as a new
我用vba编写了一个宏,它用记事本打开一个文本文件,选择所有txt并将其复制到Excel。我每天必须以这种方式处理大约 100 个文件,并且我想保留我观察到的闪烁图像。代码可以工作,但问题是命令 Ap
所以我一直在寻找将我创建的 JSON 数据库连接到 Java 程序的方法,我似乎找不到适合这种特定情况的任何内容,因此我尝试使用 JDBC 教程(http://www.tutorialspoint.c
我想在记事本中显示一个消息框,所以我找到了一个简单的dll 注入(inject)示例。注入(inject)器本身不是我的并且似乎工作正常(获取进程的 id ,创建一个远程线程,获取 dll 文件的绝对
Dim ProcessProperties As New ProcessStartInfo() ProcessProperties.FileName = "notepad.exe" ProcessPr
我使用 Process.Start() 从我的程序中打开一个记事本,但新打开的记事本覆盖了屏幕。但我确实希望我的应用程序保持其焦点。 我类似地(使用相同的 Process.Start)打开 MS Ex
之前进行Web开发的时候使用的是myeclipse,但只用过的人都知道,由于其插件太多,而且有很多插件的功能根本就接触不到。所以导致一旦工程稍微大一点就会很卡,虽然之前也对其进行优化过,但还是觉得不
在 android Notes 演示中,它接受 URI: sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES); sUriMatc
我想知道,如何将 Redhat 中的 vi 编辑器中的整行文本复制到 Windows 中的记事本中。 如果要复制的文本尺寸很小,可以通过鼠标选择行并复制到记事本来轻松完成。但是我遇到过复制数千行的情况
主要目的是了解是否有办法从 UMDF(用户模式驱动程序框架)事件启动或执行任何用户模式应用程序。事件无关紧要。任何事件都可能起作用,例如DriverEntry、DeviceAttach 我正在研究一种
我有制表符分隔的值,需要使用 Java 将其导出为文本文件,以便在 Microsoft Excel 中打开。当制表符分隔值包含中文字符时就会出现问题。 我尝试使用 UTF-8 导出文本文件,但 Exc
我们如何管理使用 Win32 api 运行的应用程序的 wndproc 函数?我使用的软件语言是 Go。 我尝试了不止一种方法,但无法做到。你能帮我吗? I am learning the HANDL
我在源代码管理中打开历史记录,然后打开一个变更集,然后在文件列表中选择一个文件并双击打开它。 TFS 通过记事本打开文件,我想通过 Visual Studio 或任何其他文本编辑器(以突出显示语法)打
Win32的FindWindow()可以找到标题为“Untitled - Notepad”的窗口,但是如果我只想找到一个记事本窗口但不知道是不是“try. bat - 记事本”或标题栏上的其他文件名?
我是一名优秀的程序员,十分优秀!