1

In my application I want generate random numbers or strings with a text in front of it. It is important for me that the text won't appear in my window, but instead gets copied to the clipboard.

int randomnumber = rand() % 46 + 1;

QClipboard *cb = QApplication::clipboard();
cb->setText("Just a test text. And here we have a placeholder! %i", randomnumber);

QClipboard works fine with plain text (in this example "Just a test text. And here we have a placeholder!"). But I also want to copy placeholders for random numbers so that the copied text looks like this:

Just a test text. And here we have a placeholder! 42

Sadly I get the error message: invalid conversion from 'int' to 'QClipboard::Mode'

Is it possible to copy text, placeholders and so on to the clipboard and not just plain text?

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
GoYoshi
  • 253
  • 1
  • 3
  • 17

2 Answers2

4

You're not using the function setText correctly. The canonical prototype is text(QString & subtype, Mode mode = Clipboard) const from the documentation.

What you want to do is assemble your QString ahead of time and then use that to populate the clipboard.

QString message = QString("Just a test text. 
     And here we have a placeholder! %1").arg(randomnumber);
cb->setText(message);

Note that the argument is %1 instead of %f. The argument numbers are sequential in Qt. Please check out this article for more information.

Hope that helps!

Community
  • 1
  • 1
Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
1

You must format your string before passing it as parameter to cb->setText.

just do this:

QString txt = QString("Just a test text. And here we have a placeholder! %1").arg(randomnumber);

And then:

cb->setText(txt);

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
Tolio
  • 1,023
  • 13
  • 30