Results 1 to 2 of 2

Thread: How could I copy a tooltip message to the clipboard?

  1. #1
    Join Date
    Mar 2019
    Qt products
    Qt4 Qt5

    Default How could I copy a tooltip message to the clipboard?

    Preface: I'm relatively new to C++ and coding in general, so my jargon and understanding of specific terminology is on the weaker side.

    I'm having difficulty with understanding the proper syntax and functionality of filling a copy slot with the desired information. Basically my knowledge of Signals and Slots is not the best.

    So, I'm working on an established app interface with tooltips in place that appear when hovering over an icon. I would like to create a right click event that would copy the tooltip message to the clipboard.

    So far I have a contextMenuEvent() that has only a copy option and it properly appears when I right click on the icon. But where I get lost is, what do I have to input in the copy action to copy the tool tip message message itself?

    Qt Code:
    1. void
    2. SomeClass::SomeLabel::mouseMoveEvent( QMouseEvent *event )
    3. {
    4. Q_UNUSED( event );
    6. // Show tooltip at a particular place of the icon, no matter where
    7. // the mouse comes into the icon
    9. QToolTip::showText( this->mapToGlobal( QPoint( this->minimumSize().width()/2, this->minimumSize().height()/2 ) ), this->toolTip(), this, this->rect() );
    11. QLabel::mouseMoveEvent( event );
    13. return;
    14. }
    17. // virtual
    18. void
    19. SomeClass::SomeLabel::contextMenuEvent( QContextMenuEvent *event )
    20. {
    22. QMenu menu;
    23. menu.addAction( m_copyInvalidityMessage );
    25. menu.exec( event->globalPos() );
    27. return;
    28. }
    30. void
    31. SomeClass::SomeLabel::copyInvalidityMessageAction( void )
    32. {
    34. m_copyInvalidityMessage = new QAction( QIcon( ":/application/Copy.ico" ), tr( "Copy Message" ), this );
    35. connect( m_copyInvalidityMessage, SIGNAL( triggered( void ) ), this, SLOT( copyInvalidityTriggered( void ) ) );
    37. return;
    38. }
    40. void
    41. SomeClass::SomeLabel::copyInvalidityTriggered(void)
    42. {
    43. QApplication::clipboard()->setText( text() );
    45. return;
    46. }
    To copy to clipboard, switch view to plain text mode 

    My guess, which isn't a good guess at all, is that in copyInvalidityTriggered(void) the setText(text()) needs something? Any help or direction to documentation that may explain or show examples would be greatly appreciated. Thank you.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 619 Times in 609 Posts
    Qt products
    Unix/X11 Windows Android

    Default Re: How could I copy a tooltip message to the clipboard?

    Your code is a bit confusing. It looks like you have derived from QLabel ("SomeLabel") as an embedded class within another class ("SomeClass"), hence the SomeClass:: SomeLabel:: name scoping. That is not something normally done.

    First, child widgets are usually created as member variables within a larger widget class, not as embedded subclasses. So your SomeClass should have a QLabel pointer variable pointing to a QLabel you create using "new QLabel()" (or automatically if you use Qt Designer to lay out the GUI and instantiate it at runtime with a call to setupUi()).

    Second, to use tooltips and context menus, you don't need to derive from QLabel at all. All QWidget-based classes support these out of the box.

    See the Qt Menus example.

    I think you have probably got the context menu working, but you should refactor the code to get rid of the embedded derived class.

    Third, once your context menu action is triggered, you need to : 1) get the tooltip text from the label and 2) put it on the clipboard.

    Step 1) is easy: it is just a call to QLabel::toolTip()
    Step 2) is just as easy: it is a call to the QClipboard::setText() method

    Qt Code:
    1. void SomeClass::copyToolTipAction()
    2. {
    3. QClipboard * pCB = QApplication::clipboard();
    4. if ( pCB )
    5. pCB->setText( ui->someLabel->toolTip() );
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 24th March 2019 at 16:45.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QtCreator unable to copy to the clipboard...
    By squimmy in forum Qt Tools
    Replies: 4
    Last Post: 25th December 2012, 12:53
  2. copy - paste clipboard contents
    By viswanathan in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 12:10
  3. Copy full widget as a picture to clipboard
    By lasher in forum Newbie
    Replies: 1
    Last Post: 26th September 2009, 08:25
  4. Problem with copy text to clipboard
    By weiching in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2008, 12:44
  5. How to copy QTableWidget to Clipboard
    By ranna in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 15:47

Tags for this Thread


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.