7
votes

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.

2
first eliminate the possibility that windows 8.1 is restricting access to the clipboard. Copy from the problematic source to another program.UmNyobe
Sure. I just wanted to include that the only users that have had this issue had Windows 8.1 and I've never heard about it happen on any other OS. I'm not sure of it's significance, but I thought I would include it anyway.mrg95
"The text being copied from Notepad/Chrome shows up fine in clipboard viewers and stuff and can be pasted elsewhere in other programs."mrg95
didn't read that line sorry :|UmNyobe
Have you tried allocating your hglobal variable first? Like this: HGLOBAL hGlob = GlobalAlloc(GMEM_FIXED, 64); msdn.microsoft.com/en-us/library/80db3kax.aspxgibertoni

2 Answers

1
votes

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.

0
votes

I had similar issue, the solution for me is sleep.

void MainWindow::on_clipboard_change(){

    QThread::msleep(1); //without this line I get allways empty clipboard

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

    qDebug() << "clipboard change event triggered ( " << text << " )";

}