Skip to content

Commit

Permalink
DebugWidget: Add support for context menu on tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Jul 17, 2024
1 parent b851833 commit 3109b3a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/qtwidgets/DebugWidgetViewer_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <QtWidgets/QTreeView>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QMainWindow>
#include <qmenu.h>

enum MyRole {
WidgetRole = Qt::UserRole + 1,
Expand All @@ -48,6 +49,9 @@ class DebugWidgetViewer : public QWidget

m_tree.setMinimumWidth(700);
m_tree.setModel(&m_model);
m_tree.setContextMenuPolicy(Qt::CustomContextMenu);
connect(&m_tree, &QTreeView::customContextMenuRequested, this, &DebugWidgetViewer::onCustomContextMenuRequested);

hlay->addWidget(&m_tree);
hlay->addWidget(&m_preview);
vlay->addLayout(hlay);
Expand Down Expand Up @@ -201,6 +205,24 @@ class DebugWidgetViewer : public QWidget
qDebug() << "END PRINT-------------------";
}

void onCustomContextMenuRequested(QPoint pos)
{
const QModelIndex index = m_tree.indexAt(pos);
if (!index.isValid())
return;

QMenu menu;
QAction *a = menu.addAction(QStringLiteral("render to png"));
connect(a, &QAction::triggered, this, [&index] {
auto widget = index.data(WidgetRole).value<QWidget *>();
Q_ASSERT(widget);
QPixmap px(widget->size());
widget->render(&px, {}, {}, DrawWindowBackground);
px.save(QStringLiteral("widget.png"));
});
menu.exec(m_tree.mapToGlobal(pos));
}

private:
QLabel m_preview;
QPixmap m_previewPx;
Expand Down

0 comments on commit 3109b3a

Please sign in to comment.