Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QClipboard cannot get clipboard data if copied from Notepad/Chrome

In my program, my users can copy a string of text from anywhere and paste it into my program. I use the simple QApplication::clipboard()->text(); function and everything works as expected. However, several of my users are having problems when trying to copy and paste on Windows 8.1

Here is how I access the clipboard text from my paste function:

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

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

//do something with clipboard

But if the text was copied from Notepad or Chrome, the text is ALWAYS empty. Windows 7 users have not had any problems. Not ALL Windows 8 users have this issue, it's only a handful but the issue it consistent. When copied from some other random places or within my program itself, the clipboard works fine.

I've tried using mimeData. When using the function formats(), only plain text is an available format, but the text is always empty.

The text being copied from Notepad/Chrome shows up fine in clipboard viewers and stuff and can be pasted elsewhere in other programs.

Copying and pasting is a very important feature in my program and its frustrating that my users can't copy and paste from Notepad or Chrome.

Any ideas? Thanks for your time. :)

EDIT: I tried using the "windows" style technique. There was no change. Here is my current, still unworking code:

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;
}

The copied text shows up fine in a clipboard viewer, but my program can't get to it no matter what: enter image description here

How come GetClipboardData() isn't picking anything up? Again, the copied text CAN be pasted in any other program I've tried... just not mine. But if copied from somewhere else, it works no problem.

like image 528
mrg95 Avatar asked Mar 30 '15 12:03

mrg95


1 Answers

EDIT: With this version, When the user copies text and text gets to clipboard, a function copies the text this time to an internal text file not directly to your program, using QFile. Another function copies the text from the internal text file to your program. By this way, i think your program wouldn't have to directly deal with text in the clipboard

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();
}

Look through and compare with your part of the code to see if there's something you did wrong. But as to how you claim it works on some Windows 8.1 systems and don't on others baffles me.

like image 199
Richardson Ansong Avatar answered Nov 05 '22 05:11

Richardson Ansong