Skip to content

Commit

Permalink
feat(qt): Add TreeView with keyEvent listener
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiusD committed Feb 19, 2024
1 parent 62e17bc commit c13cf65
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ set(SOURCES
src/modelaccounttree.cpp
src/modelnewemails.cpp
src/morkparser.cpp
src/qtcomponents.cpp
src/setting_newemail.cpp
src/settings.cpp
src/trayicon.cpp
Expand Down Expand Up @@ -99,6 +100,7 @@ set(HEADERS
src/modelaccounttree.h
src/modelnewemails.h
src/morkparser.h
src/qtcomponents.h
src/setting_newemail.h
src/settings.h
src/trayicon.h
Expand Down
22 changes: 22 additions & 0 deletions src/qtcomponents.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "qtcomponents.h"

QTreeViewWithKeyEvents::QTreeViewWithKeyEvents( QWidget *parent )
: QTreeView(parent)
{

}

void QTreeViewWithKeyEvents::onKeyPressed( void * handle, TreeViewKeyPressedEvent callback)
{
mHandle = handle;
mCallback = callback;
}

void QTreeViewWithKeyEvents::keyPressEvent( QKeyEvent *event )
{
if (mCallback != NULL && mHandle != NULL)
{
mCallback(mHandle, event);
}
QTreeView::keyPressEvent(event);
}
29 changes: 29 additions & 0 deletions src/qtcomponents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef QTCOMPONENTS_H
#define QTCOMPONENTS_H

#include <QKeyEvent>
#include <QTreeView>

typedef void ( * TreeViewKeyPressedEvent)(void * handle, QKeyEvent * event);

/**
* A QTreeView that allow to define listener of keyEvent received
*/
class QTreeViewWithKeyEvents : public QTreeView {
Q_OBJECT

public:
explicit QTreeViewWithKeyEvents(QWidget *parent = NULL);

public slots:
void onKeyPressed(void * handleUsed, TreeViewKeyPressedEvent callback);

protected:
void keyPressEvent(QKeyEvent * event) override;

private:
TreeViewKeyPressedEvent mCallback;
void * mHandle;
};

#endif // QTCOMPONENTS_H

0 comments on commit c13cf65

Please sign in to comment.