summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2022-06-01 16:24:52 +0200
committerIvan Solovev <ivan.solovev@qt.io>2022-06-02 12:27:07 +0200
commit5c8bf34dc1e748dbabe3187b4a77647ccd41be3d (patch)
treef58d4f2ae4c5695b02aed018a15ab6722925430c /examples
parent0b90663e08cdb3f61849dcfe94dd14a32546b648 (diff)
ndefeditor: fix dropdown menu problems
Because of QTBUG-98651 and QTBUG-97482 a QPushButton with a dropdown menu was not working properly neither on iOS nor on Android. This patch fixes the issue, but it's not optimal, because it re-creates the menu every time. Ideally we should provide QML-based GUI for the example. Task-number: QTBUG-103949 Pick-to: 6.3 6.2 Change-Id: Icd1e00f27f5c4864a33fa7f1f7755c0e919183cb Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi>
Diffstat (limited to 'examples')
-rw-r--r--examples/nfc/ndefeditor/mainwindow.cpp29
-rw-r--r--examples/nfc/ndefeditor/mainwindow.h4
2 files changed, 27 insertions, 6 deletions
diff --git a/examples/nfc/ndefeditor/mainwindow.cpp b/examples/nfc/ndefeditor/mainwindow.cpp
index 87042d4c..f39f8d7a 100644
--- a/examples/nfc/ndefeditor/mainwindow.cpp
+++ b/examples/nfc/ndefeditor/mainwindow.cpp
@@ -134,12 +134,7 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
- QMenu *addRecordMenu = new QMenu(this);
- addRecordMenu->addAction(tr("NFC Text Record"), this, SLOT(addNfcTextRecord()));
- addRecordMenu->addAction(tr("NFC URI Record"), this, SLOT(addNfcUriRecord()));
- addRecordMenu->addAction(tr("MIME Image Record"), this, SLOT(addMimeImageRecord()));
- addRecordMenu->addAction(tr("Empty Record"), this, SLOT(addEmptyRecord()));
- ui->addRecord->setMenu(addRecordMenu);
+ connect(ui->addRecord, &QPushButton::clicked, this, &MainWindow::showMenu);
QVBoxLayout *vbox = new QVBoxLayout;
ui->scrollAreaWidgetContents->setLayout(vbox);
@@ -368,6 +363,28 @@ void MainWindow::targetError(QNearFieldTarget::Error error, const QNearFieldTarg
}
}
+void MainWindow::showMenu()
+{
+ // We have to manually call QMenu::popup() because of QTBUG-98651.
+ // And we need to re-create menu each time because of QTBUG-97482.
+ if (m_menu) {
+ m_menu->setParent(nullptr);
+ delete m_menu;
+ }
+ m_menu = new QMenu(this);
+ m_menu->addAction(tr("NFC Text Record"), this, &MainWindow::addNfcTextRecord);
+ m_menu->addAction(tr("NFC URI Record"), this, &MainWindow::addNfcUriRecord);
+ m_menu->addAction(tr("MIME Image Record"), this, &MainWindow::addMimeImageRecord);
+ m_menu->addAction(tr("Empty Record"), this, &MainWindow::addEmptyRecord);
+
+ // Use menu's sizeHint() to position it so that its right side is aligned
+ // with button's right side.
+ QPushButton *button = ui->addRecord;
+ const int x = button->x() + button->width() - m_menu->sizeHint().width();
+ const int y = button->y() + button->height();
+ m_menu->popup(mapToGlobal(QPoint(x, y)));
+}
+
void MainWindow::clearMessage()
{
QWidget *scrollArea = ui->scrollAreaWidgetContents;
diff --git a/examples/nfc/ndefeditor/mainwindow.h b/examples/nfc/ndefeditor/mainwindow.h
index df63b459..e63fd36f 100644
--- a/examples/nfc/ndefeditor/mainwindow.h
+++ b/examples/nfc/ndefeditor/mainwindow.h
@@ -58,6 +58,7 @@
QT_FORWARD_DECLARE_CLASS(QNearFieldManager)
QT_FORWARD_DECLARE_CLASS(QNdefMessage)
QT_FORWARD_DECLARE_CLASS(QScreen)
+QT_FORWARD_DECLARE_CLASS(QMenu)
QT_BEGIN_NAMESPACE
namespace Ui {
@@ -98,6 +99,8 @@ private slots:
void ndefMessageWritten(const QNearFieldTarget::RequestId &id);
void targetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id);
+ void showMenu();
+
private:
enum TouchAction {
NoAction,
@@ -111,6 +114,7 @@ private:
private:
Ui::MainWindow *ui;
+ QMenu *m_menu = nullptr;
QNearFieldManager *m_manager;
TouchAction m_touchAction;