[C++][Qt] A command to put data into clipboard in linux command pipe terminal

Just for the fun, I put here the code of an application that moves to clipboard what it receives from input.

It may already exist, but I wanted to do it, and thus I am sharing the code which is a basic example to understand Qt clipboard and C++ terminal pipe programming.

# to ask for help: 
clipme -h 
[Help] clipme:
[Help] You ask for help.
[Help] Print what is passed to the input.:
[Help] Print the clipboard at the end.:
[Help] Append to clipboard (instead of replacing by default).:



echo "write me in clipboard" | clipme
# Ctr+V = write me in clipboard
echo ", stuff to append to clipboard" | clipme +=
# Ctr+V = write me in clipboard, stuff to append to clipboard

The code


#include <QApplication>
#include <QClipboard>
#include <QStringList>

#include <iostream>

/** To manage user choices */
class UserChoice{
    bool printInput;
    bool printClipboardAtEnd;
    bool append;
    bool printHelp;

    /** Quick check if a parameter has been passed */
    static bool ParamsExist(const int argc, char* argv[],
                            const QStringList& params){
        for(int idxParam = 1 ; idxParam < argc ; ++idxParam){
            if(params.contains(argv[idxParam], Qt::CaseInsensitive)){
                return true;

        return false;

    /** Print help */
    static void PrintOption(const QString& definition, const QStringList& params){
        std::cout << "[Help] " << (const char*)definition.toLatin1() << ":\n";
        foreach(const QString& param, params){
            std::cout << "\t" << (const char*)param.toLatin1() << "\n";

    /** Init parameters from arguments */
    UserChoice(const int argc, char *argv[])
        : printInput(false), printClipboardAtEnd(false), append(false),
        const QStringList printInputParams(QStringList() << "-printout" << "-pipeout");
        const QStringList printClipboardAtEndParams(QStringList() << "-showclip" << "-printclip");
        const QStringList appendParams(QStringList() << "-append" << "+=");

        // Check options
        printInput = ParamsExist(argc, argv, printInputParams);
        printClipboardAtEnd = ParamsExist(argc, argv, printClipboardAtEndParams);
        append = ParamsExist(argc, argv, appendParams);
        printHelp = ParamsExist(argc, argv, (QStringList() << "-help" << "--help" << "-h"));

        // Print help
            std::cout << "[Help] clipme:\n";
            std::cout << "[Help] You ask for help.\n";
            PrintOption("Print what is passed to the input.", printInputParams);
            PrintOption("Print the clipboard at the end.", printClipboardAtEndParams);
            PrintOption("Append to clipboard (instead of replacing by default).", appendParams);

    // Get option choices

    bool askForHelp() const{
        return printHelp;

    bool optionPrintOut() const{
        return printInput;

    bool optionPrintClipboard() const{
        return printClipboardAtEnd;

    bool optionAppend() const{
        return append;

int main(int argc, char *argv[])
    // Init options
    const UserChoice config(argc, argv);
    // If asked for help exit
        return 0;

    // Create an application to get the clipboard
    QApplication app(argc, argv);
    QClipboard* clipboard = app.clipboard();

    // Process data
    QString allReadData;

    { // Read Data
        std::string line ;
        while( std::getline( std::cin, line ) ){
            allReadData += line.c_str();
                std::cout << line << '\n' ;

    // Append if needed and set to the clipboard
        allReadData = clipboard->text(QClipboard::Clipboard) + allReadData;

    // Print clipboard if aksed to
        std::cout << (const char*)clipboard->text(QClipboard::Clipboard).toLatin1() << "\n";

    return 0;