QToolButton with QMenu stops working after switching tabs (QWebEngineView involved)
-
Hi, everyone,
I'm working on a Qt6/C++ Qt Widgets application usingQMainWindow
andQTabWidget
.- One tab embeds a
QWebEngineView
(in a tab named "Browser"). - Another tab (called "Vocabulary") displays custom
CardWidgets
. - Each of which includes a
QToolButton
with aQMenu
.
Issue:
When the app starts (the Vocabulary tab is active), everything works fine — the menu shows as expected when the button is clicked.
But after I switch to the Browser tab (with QWebEngineView) and then return to Vocabulary, the QToolButton click is detected but the menu no longer appears.
There are no crashes or warnings, and the QToolButton is still visible and responsive — just no menu.CardWidget constructor snippet:
CardWidget::CardWidget(const QVariantList& data, QWidget *parent) : QWidget(parent), expanded(false), currentRating(0) { this->id = data[0].toInt(); this->word = data[1].toString(); setCursor(Qt::PointingHandCursor); // Cursor on hover setStyleSheet(R"( QWidget { border: 1px solid transparent; border-radius: 8px; padding: 8px; } QWidget:hover { border: 2px solid #3498db; } )"); QVBoxLayout* mainLayout = new QVBoxLayout(this); // Top Layout QHBoxLayout* topLayout = new QHBoxLayout(); titleLabel = new QLabel(data[1].toString()); titleLabel->setStyleSheet("font-weight: bold; font-size: 16px;"); topLayout->addWidget(titleLabel); menuButton = new QToolButton(); menuButton->setText("⋮"); menuButton->setPopupMode(QToolButton::InstantPopup); menuButton->installEventFilter(this); QMenu* menu = new QMenu(); QAction* editAction = menu->addAction("Edit"); QAction* deleteAction = menu->addAction("Delete"); menuButton->setMenu(menu); menuButton->setFocus(); topLayout->addWidget(menuButton); connect(editAction, &QAction::triggered, this, [this](){ emit CardWidget::editRequested(this->id); }); connect(deleteAction, &QAction::triggered, this, [this](){ emit CardWidget::deleteRequested(this->id); }); mainLayout->addLayout(topLayout); // Expandable area expandableArea = new QWidget(); QVBoxLayout* expandLayout = new QVBoxLayout(expandableArea); QStringList labels = { "Part of Speech", "Meaning", "Example", "Synonyms", "Antonyms", "Notes" }; for (int i = 2; i < 8 && i < data.size(); ++i) { QLabel* label = new QLabel(QString("<b>%1:</b> %2").arg(labels[i - 2], data[i].toString())); label->setWordWrap(true); expandLayout->addWidget(label); } expandableArea->setVisible(false); mainLayout->addWidget(expandableArea); // Star Rating int rating = data[8].toInt(); QHBoxLayout* starsLayout = new QHBoxLayout(); for (int i = 0; i < 5; ++i) { QPushButton* star = new QPushButton("☆"); star->setFlat(true); star->setStyleSheet("font-size: 20px; color: gold;"); starButtons.append(star); starsLayout->addWidget(star); setRating(rating); connect(star, &QPushButton::clicked, this, [this, i]() { setRating(i+1); }); } mainLayout->addLayout(starsLayout); }
In MainWindow
DictionaryApp::DictionaryApp(QWidget *parent) : QMainWindow(parent) , ui(new Ui::DictionaryApp) { ui->setupUi(this); ui->statusBar->hide(); browser = new QWebEngineView(); QVBoxLayout *layout = qobject_cast<QVBoxLayout *>(ui->browser_container->layout()); if(layout) { layout->insertWidget(1, browser); } pool = new QThreadPool(); initDB(); full_refresh_vocabTab(); }
full_refresh_vocabTab
function:void DictionaryApp::full_refresh_vocabTab() { QList<QVariantList> records = loadFromDB(); QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(ui->vocabListContainer->layout()); if (!layout) { qDebug() << "Error: No layout set for vocabListContainer"; return; } // Clear previous cards if needed QLayoutItem* item; while ((item = layout->takeAt(0)) != nullptr) { if (item->widget()) delete item->widget(); delete item; } // Add new cards for (const QVariantList& row : records) { CardWidget* card = new CardWidget(row); cards.append(card); layout->addWidget(card); connect(card, &CardWidget::editRequested, this, &DictionaryApp::onEditRequested); connect(card, &CardWidget::deleteRequested, this, &DictionaryApp::onDeleteRequested); connect(card, &CardWidget::ratingChanged, this, &DictionaryApp::full_refresh_vocabTab); } }
What I've Tried
- eventFilter() confirms that the button is receiving mouse clicks even after the tab switch.
- The menu works only until I visit the
QWebEngineView
tab. Returning to the Vocabulary tab causes the menu to stop opening. - There are no errors in the debug output.
- Tried removing/re-adding the widget — no change.
Question:
What might cause aQToolButton
'sQMenu
to stop appearing after switching to a tab containing aQWebEngineView
and then returning?Versions:
- Qt6.9
- Qt Creator 13.0.0
- OS: Fedora
- One tab embeds a
-
Hi and welcome to devnet,
Does it also happen if you delete the QWebEngineView ?
-
Hi, thanks for the response,
I tested it by explicitly deleting QWebEngineView when switching tabs. The qDebug() output confirms the deletion is happening, but issue still persists. Any ideas on what might be causing this? -
Can you boil this down to a small, compilable reproducer, which can be posted here?
A web view is a complex beast. I've used it myself in a widgets application, in a dialog as a toplevel window of its own. At least there, it doesn't cause any trouble. I'll gladly have a look. Maybe it's even a bug, who knows. -
Can you boil this down to a small, compilable reproducer, which can be posted here?
A web view is a complex beast. I've used it myself in a widgets application, in a dialog as a toplevel window of its own. At least there, it doesn't cause any trouble. I'll gladly have a look. Maybe it's even a bug, who knows.Hi, @Axel-Spoerl
Thanks for your willingness to take a look!Unfortunately, I’m currently unable to post the code or zip file directly here as I don't currently have the privileges to upload a .zip file directly and it’s being flagged as spam due to its length or format. To work around that, I’ve uploaded the minimal, compilable reproducer as a GitHub Gist instead:
https://gist.github.com/sjana0/9a022d41d3b050236779fdfba2a10088
The project is self-contained and demonstrates the issue where the QToolButton menu stops opening after switching tabs with a QWebEngineView.Please let me know if you run into any issues running it or need a different format. I appreciate your help!
-
Hi @soumyaniljana,
please post the code here explicitly, using the </> code tags. No zip files, no links.
The community should see the code and not be referred to an external page, that might change over time.
Cheers
Axel -
Hi @soumyaniljana,
please post the code here explicitly, using the </> code tags. No zip files, no links.
The community should see the code and not be referred to an external page, that might change over time.
Cheers
AxelHi @Axel-Spoerl ,
Thanks again for your patience and help.
I've been trying to post the minimal reproducible example using the </> code formatting as requested, but unfortunately, my post keeps getting flagged by Akismet as spam — even though I'm not including any links or external files. It seems the amount of code or structure might be triggering it.
Would it be alright if I posted the code across a few smaller replies in the thread instead? Or if there’s another approach you’d recommend, I’d be happy to follow that.
Really appreciate your time and support.
Kind regards,
soumyanil -
Hi @Axel-Spoerl ,
Thanks again for your patience and help.
I've been trying to post the minimal reproducible example using the </> code formatting as requested, but unfortunately, my post keeps getting flagged by Akismet as spam — even though I'm not including any links or external files. It seems the amount of code or structure might be triggering it.
Would it be alright if I posted the code across a few smaller replies in the thread instead? Or if there’s another approach you’d recommend, I’d be happy to follow that.
Really appreciate your time and support.
Kind regards,
soumyanilThis post is deleted! -
@soumyaniljana That looks incomplete.
-
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), webView(new QWebEngineView), menuButton(new QToolButton), tabs(new QTabWidget) { // Browser Tab QWidget *browserTab = new QWidget; QVBoxLayout *browserLayout = new QVBoxLayout(browserTab); browserLayout->addWidget(webView); // if you dont initialize the webView, the QMenu dropdown works. // and same if you switch tab without initializing it and come back // vocabulary tab it again stops working. webView->setUrl(QUrl("https://www.google.com")); // Vocabulary Tab QWidget *vocabTab = new QWidget; QVBoxLayout *vocabLayout = new QVBoxLayout(vocabTab); QMenu *menu = new QMenu; menu->addAction("Edit"); menu->addAction("Delete"); menuButton->setText("Menu"); menuButton->setMenu(menu); menuButton->setPopupMode(QToolButton::InstantPopup); vocabLayout->addWidget(menuButton); // Add tabs to QTabWidget tabs->addTab(vocabTab, "Vocabulary"); tabs->addTab(browserTab, "Browser"); // Set central widget setCentralWidget(tabs); }
-
@soumyaniljana That looks incomplete.
#pragma once #include <QMainWindow> class QWebEngineView; class QToolButton; class QTabWidget; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); private: QWebEngineView *webView; QToolButton *menuButton; QTabWidget *tabs; };
-
#pragma once #include <QMainWindow> class QWebEngineView; class QToolButton; class QTabWidget; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); private: QWebEngineView *webView; QToolButton *menuButton; QTabWidget *tabs; };
Please include QTabWidget, QWebEngineView, QVBoxLayout, QToolButton, QMenu, QWidget
And that should now be the complete minimal example! Please let me know if it’s still missing anything or if there’s anything you'd like me to simplify further. I really appreciate your time and guidance.Kind regards,
Soumyanil -
Hi @Axel-Spoerl,
Just checking in to see if you had a chance to look at the code I posted earlier. I'd really appreciate any insights or suggestions when you get a moment.
Thanks again for your time and guidance.
Regards,
Soumyanil