PDA

View Full Version : How could I copy a tooltip message to the clipboard?



EveryoneCallsMeM
21st March 2019, 14:49
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?



void
SomeClass::SomeLabel::mouseMoveEvent( QMouseEvent *event )
{
Q_UNUSED( event );

// Show tooltip at a particular place of the icon, no matter where
// the mouse comes into the icon

QToolTip::showText( this->mapToGlobal( QPoint( this->minimumSize().width()/2, this->minimumSize().height()/2 ) ), this->toolTip(), this, this->rect() );

QLabel::mouseMoveEvent( event );

return;
}


// virtual
void
SomeClass::SomeLabel::contextMenuEvent( QContextMenuEvent *event )
{

QMenu menu;
menu.addAction( m_copyInvalidityMessage );

menu.exec( event->globalPos() );

return;
}

void
SomeClass::SomeLabel::copyInvalidityMessageAction( void )
{

m_copyInvalidityMessage = new QAction( QIcon( ":/application/Copy.ico" ), tr( "Copy Message" ), this );
connect( m_copyInvalidityMessage, SIGNAL( triggered( void ) ), this, SLOT( copyInvalidityTriggered( void ) ) );

return;
}

void
SomeClass::SomeLabel::copyInvalidityTriggered(void )
{
QApplication::clipboard()->setText( text() );

return;
}



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.

d_stranz
24th March 2019, 16:39
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 (https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html).

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



void SomeClass::copyToolTipAction()
{
QClipboard * pCB = QApplication::clipboard();
if ( pCB )
pCB->setText( ui->someLabel->toolTip() );
}