Results 1 to 7 of 7

Thread: Copying text with an action

  1. #1
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Copying text with an action

    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.

    Qt Code:
    1. void MainWindow::on_actionCopy_triggered()
    2. {
    3. ui->lineEdit_PartWidth->copy();
    4. // lineEdit 2...
    5. // lineEdit 3...
    6. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copying text with an action

    do you want to copy all of the line edits?

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

    e.g.
    Qt Code:
    1. QList<QLineEdit*> list;
    2. list << ui->lineedit1 << ui->lineedit2 << ...;
    3. foreach(QLineEdit* ptr, list)
    4. ptr->copy()
    To copy to clipboard, switch view to plain text mode 

    the container should be a member variable so you would only make it once. Hope you get the gist...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    fs_tigre (5th May 2012)

  4. #3
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Copying text with an action

    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:n_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!

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copying text with an action

    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/qli...ectedText-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:


    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit Widget(QWidget *parent = 0);
    12. ~Widget();
    13.  
    14. private slots:
    15.  
    16. void slotCopyToClipboard(QString text) const;
    17. };
    18.  
    19. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    cpp
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. #include "clickablelabel.h"
    5.  
    6. #include <QClipboard>
    7. #include <QLabel>
    8. #include <QVBoxLayout>
    9. #include <QTextEdit>
    10.  
    11. #include <QList>
    12. #include <QSignalMapper>
    13.  
    14.  
    15. Widget::Widget(QWidget *parent) :
    16. QWidget(parent) //ui(new Ui::Widget)
    17. {
    18. //ui->setupUi(this);
    19.  
    20. QVBoxLayout* layout = new QVBoxLayout;
    21.  
    22. ClickableLabel* lbl1 = new ClickableLabel("one");
    23. ClickableLabel* lbl2 = new ClickableLabel("two");
    24. ClickableLabel* lbl3 = new ClickableLabel("three");
    25.  
    26. QList<ClickableLabel*> labels;
    27. labels << lbl1 << lbl2 << lbl3;
    28.  
    29. foreach(ClickableLabel* lbl, labels)
    30. layout->addWidget(lbl);
    31.  
    32. layout->addWidget(new QTextEdit());
    33.  
    34. setLayout(layout);
    35.  
    36. QSignalMapper* sm = new QSignalMapper(this);
    37.  
    38. foreach(ClickableLabel* lbl, labels)
    39. {
    40. connect(lbl, SIGNAL(clicked()), sm, SLOT(map()));
    41. sm->setMapping(lbl, lbl->text());
    42. }
    43.  
    44. connect(sm, SIGNAL(mapped(QString)), this, SLOT(slotCopyToClipboard(QString)));
    45. }
    46.  
    47. Widget::~Widget()
    48. {
    49. }
    50.  
    51.  
    52. void Widget::slotCopyToClipboard(QString text) const
    53. {
    54. QClipboard* cb = QApplication::clipboard();
    55.  
    56. cb->setText(text);
    57. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by amleto; 5th May 2012 at 13:32.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. The following user says thank you to amleto for this useful post:

    fs_tigre (5th May 2012)

  7. #5
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Copying text with an action

    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

  8. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copying text with an action

    I flip-flop between boards. I like the feature on this board where people can give thanks for posts
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. The following user says thank you to amleto for this useful post:

    fs_tigre (5th May 2012)

  10. #7
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Copying text with an action

    Hmm, I didn't know about this feature. Thanks

Similar Threads

  1. How to get toggle action text?
    By sudhansu in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2012, 12:57
  2. Hide shorcut text of QMenu Action
    By KillGabio in forum Qt Programming
    Replies: 5
    Last Post: 19th January 2012, 10:43
  3. copying text from qtextbrowser
    By user14921 in forum Qt Programming
    Replies: 0
    Last Post: 7th December 2011, 00:02
  4. Replies: 1
    Last Post: 6th December 2011, 23:44
  5. get Text from Popupmenu action
    By raphaelf in forum Newbie
    Replies: 6
    Last Post: 11th October 2006, 16:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.