Accessing a class containing Mouse ClickEvents from another class
-
Hi,
I have several classes that create and process QTableWidgets. They all work fine. I'm adding another capability to allow users to hightlight rows in the table and use CNTRL-C to copy those rows to the clipboard where I can paste to a text file, excel, etc.
If I use the method below within the class where I create and manage the tableWidget, it works fine. However, since I have many classes that contain different tableWidgets, I would like to create a separate class that contains the code below so I can reference the that code without keeping multiple copies of the same code in different classes. This way if I make a change, it is in one place.
Would someone know how to do this? When I create a class and use the logic below, typing CNTRL-C does not enter the keyPressEvent method as if I had it in the same class where the tableWidget is managed. The copyTextFromTableWidgetToClipBoard() method juts scans the table and see what is highlighted and copies to the clipboard. This works fine. I just can't figure out how to access the keyPressEvent method.
keyPressEvent(QKeyEvent* event)
{
// If Ctrl-C typed
// Or use event->matches(QKeySequence::Copy)
if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier))
{
copyTextFromTableWidgetToClipBoard();
}}
Thanks!
-
@leinad said in Accessing a class containing Mouse ClickEvents from another class:
copyTextFromTableWidgetToClipBoard
You could move this method into a common parent class and subclass all your classes with QTableWidgets from that common parent class.
-
@leinad said in Accessing a class containing Mouse ClickEvents from another class:
I made it a simple class with the keyPressEvent. The problem is when I hit the keys, it is not recognized.
Don't know what exactly you have in mind. Just having some class with a method named
keyPressEvent
won't hook up toQTableWidget::keyPressEvent()
virtual method.So far as I know there will only be two possible/useful ways of "intercepting" a key press on a widget:
- Subclass the widget and override virtual
QTableWidget::keyPressEvent()
. - Install an event filter to deal with the key press, which avoids having to subclass.
Actually the full, more complex details can be read in QCoreApplication::notify().
- Subclass the widget and override virtual
-