mrg*_*g95 7 c++ clipboard qt windows-8.1
在我的程序中,我的用户可以从任何地方复制一串文本并将其粘贴到我的程序中.我使用简单的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
Run Code Online (Sandbox Code Playgroud)
但如果文本是从记事本或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;
}
Run Code Online (Sandbox Code Playgroud)
复制的文本在剪贴板查看器中显示正常,但无论如何,我的程序都无法访问它:

为什么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
Run Code Online (Sandbox Code Playgroud)
剪贴板.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() {
}
Run Code Online (Sandbox Code Playgroud)
主程序
#include "clipboard.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
ClipBoard w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
仔细检查并与您的代码部分进行比较,看看是否有错误的地方。但至于你如何声称它在某些 Windows 8.1 系统上工作而在其他系统上不起作用,这让我感到困惑。
| 归档时间: |
|
| 查看次数: |
1895 次 |
| 最近记录: |