I'm working with QClipboard
object under Windows 10.
Just trying to see what's inside the clipboard when I press Ctrl-C
on some sample text.
As you can see the results are very inconsistent and I cannot grasp why exactly.
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(showClipboard()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showClipboard()
{
qDebug() << "Clipboard is empty:" << QApplication::clipboard()->text().isEmpty()
<< ":" << QApplication::clipboard()->text();
}
Sample output:
Clipboard is empty: false : ""
Clipboard is empty: true : ""
Clipboard is empty: false : "sample text"
Clipboard is empty: false : "sample text"
Clipboard is empty: true : ""
Clipboard is empty: false : ""
Clipboard is empty: false : ""
Clipboard is empty: false : ""
Clipboard is empty: false : "sample text"
Clipboard is empty: true : ""
Clipboard is empty: false : "sample text"
Clipboard is empty: true : ""
It seems that introduction of a pause before calling
QApplication::clipboard()->text()
fixes this issue.QTimer
or plainSleep()
/nanoSleep()
(Windows/Linux) can be used to achieve the effect.QTimer Example:
Windows
Sleep()
Example: