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>
This commit is contained in:
Ivan Solovev 2022-06-01 16:24:52 +02:00
parent 0b90663e08
commit 5c8bf34dc1
2 changed files with 27 additions and 6 deletions

View File

@ -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;

View File

@ -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;