PDA

View Full Version : Copying text with an action



fs_tigre
3rd May 2012, 13:38
Hi,

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()
{
ui->lineEdit_PartWidth->copy();
// lineEdit 2...
// lineEdit 3...
}

Thanks a lot

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

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

e.g.


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


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

fs_tigre
4th May 2012, 17: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()
{
ui->label_TotalParts->copy();

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

As always thanks a lot for your help!

amleto
5th May 2012, 12:32
ok.

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
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private slots:

void slotCopyToClipboard(QString text) const;
};

#endif // WIDGET_H


cpp


#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)
{
//ui->setupUi(this);

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(lbl);

layout->addWidget(new QTextEdit());

setLayout(layout);

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)));
}

Widget::~Widget()
{
}


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

cb->setText(text);
}


clickable label from here:
http://qt-project.org/wiki/Make-a-QLabel-Clickable
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.

fs_tigre
5th May 2012, 15: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

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

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