View Full Version : Copying text with an action

3rd May 2012, 14:38

I'm currently using the copy() function to copy text from a lineEdit field using an action but I was wondering if there was a way to have this action to copy any selected text without having to specify each field or it's simply the way it works? If this is the only way, it's totally fine, I just don't want to list each field if there is a better way.

This is what I'm currently using that works, but only for the lineEdit_PartWidth of course.

void MainWindow::on_actionCopy_triggered()
// lineEdit 2...
// lineEdit 3...

Thanks a lot

3rd May 2012, 22:37
do you want to copy all of the line edits?

make a container for holding a pointer to all of the lineedits.


QList<QLineEdit*> list;
list << ui->lineedit1 << ui->lineedit2 << ...;
foreach(QLineEdit* ptr, list)

the container should be a member variable so you would only make it once. Hope you get the gist...

4th May 2012, 18:29
do you want to copy all of the line edits?

Not all of them at once, I just want to have something more universal that could work on all lineEdits without having to list everyone because I'm afraid that as the application grows I may forget to add some of them and so I want the action to work on any selected text regardless of where it is. Sorry if my question doesn't make too much sense, you can ignore it since I can probably list everyone manually as my original example.

One more question.

Is it possible to copy the text in a QLabel to the clipboard when a button is clicked and without having to select the text? This is something I will be doing to one Qlabel only.

I tried....

void MainWindow::on_pushButton_Copy_clicked()

but of course it didn't work because Qlabel doesn't have the copy function.

As always thanks a lot for your help!

5th May 2012, 13:32

The point I made about saving the lineedit pointers in a container is valid - you should do that.

You can iterate over all the lineedits in the container and use http://qt-project.org/doc/qt-4.8/qlineedit.html#selectedText-prop to find the selected text in each.

To copy text from a label into the clipboard is very simple using signal mapper. I will make a small example...

Added after 21 minutes:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class Widget : public QWidget

explicit Widget(QWidget *parent = 0);

private slots:

void slotCopyToClipboard(QString text) const;

#endif // WIDGET_H


#include "widget.h"
#include "ui_widget.h"

#include "clickablelabel.h"

#include <QClipboard>
#include <QLabel>
#include <QVBoxLayout>
#include <QTextEdit>

#include <QList>
#include <QSignalMapper>

Widget::Widget(QWidget *parent) :
QWidget(parent) //ui(new Ui::Widget)

QVBoxLayout* layout = new QVBoxLayout;

ClickableLabel* lbl1 = new ClickableLabel("one");
ClickableLabel* lbl2 = new ClickableLabel("two");
ClickableLabel* lbl3 = new ClickableLabel("three");

QList<ClickableLabel*> labels;
labels << lbl1 << lbl2 << lbl3;

foreach(ClickableLabel* lbl, labels)

layout->addWidget(new QTextEdit());


QSignalMapper* sm = new QSignalMapper(this);

foreach(ClickableLabel* lbl, labels)
connect(lbl, SIGNAL(clicked()), sm, SLOT(map()));
sm->setMapping(lbl, lbl->text());

connect(sm, SIGNAL(mapped(QString)), this, SLOT(slotCopyToClipboard(QString)));


void Widget::slotCopyToClipboard(QString text) const
QClipboard* cb = QApplication::clipboard();


clickable label from here:
Or you could use a pushbutton and make it look flat.

Just click on the label, and then right-click in the text edit and paste to confirm that the label string is in the clipboard.

5th May 2012, 16:30
You are awesome! I will give this a try later.

Hey I noticed you have been helping me here and in the qtforum. Which one do you usually visit more often?

Thanks a lot for your help

5th May 2012, 18:22
I flip-flop between boards. I like the feature on this board where people can give thanks for posts ;)

6th May 2012, 04:00
Hmm, I didn't know about this feature. Thanks