Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. QToolButton with QMenu stops working after switching tabs (QWebEngineView involved)
Forum Updated to NodeBB v4.3 + New Features

QToolButton with QMenu stops working after switching tabs (QWebEngineView involved)

Scheduled Pinned Locked Moved Unsolved QtWebEngine
13 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    soumyaniljana
    wrote last edited by
    #1

    Hi, everyone,
    I'm working on a Qt6/C++ Qt Widgets application using QMainWindow and QTabWidget.

    • One tab embeds a QWebEngineView (in a tab named "Browser").
    • Another tab (called "Vocabulary") displays custom CardWidgets.
    • Each of which includes a QToolButton with a QMenu.

    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 a QToolButton's QMenu to stop appearing after switching to a tab containing a QWebEngineView and then returning?

    Versions:

    • Qt6.9
    • Qt Creator 13.0.0
    • OS: Fedora
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi and welcome to devnet,

      Does it also happen if you delete the QWebEngineView ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        soumyaniljana
        wrote last edited by
        #3

        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?

        1 Reply Last reply
        0
        • Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote last edited by
          #4

          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.

          Software Engineer
          The Qt Company, Oslo

          S 1 Reply Last reply
          0
          • Axel SpoerlA Axel Spoerl

            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.

            S Offline
            S Offline
            soumyaniljana
            wrote last edited by
            #5

            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!

            1 Reply Last reply
            0
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote last edited by
              #6

              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

              Software Engineer
              The Qt Company, Oslo

              S 1 Reply Last reply
              1
              • Axel SpoerlA Axel Spoerl

                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

                S Offline
                S Offline
                soumyaniljana
                wrote last edited by
                #7

                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,
                soumyanil

                S 1 Reply Last reply
                0
                • S soumyaniljana

                  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,
                  soumyanil

                  S Offline
                  S Offline
                  soumyaniljana
                  wrote last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote last edited by
                    #9

                    @soumyaniljana That looks incomplete.

                    Software Engineer
                    The Qt Company, Oslo

                    S 1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      soumyaniljana
                      wrote last edited by
                      #10
                      #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);
                      }
                      
                      1 Reply Last reply
                      0
                      • Axel SpoerlA Axel Spoerl

                        @soumyaniljana That looks incomplete.

                        S Offline
                        S Offline
                        soumyaniljana
                        wrote last edited by
                        #11
                        #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;
                        };
                        
                        S 1 Reply Last reply
                        0
                        • S soumyaniljana
                          #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;
                          };
                          
                          S Offline
                          S Offline
                          soumyaniljana
                          wrote last edited by
                          #12

                          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

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            soumyaniljana
                            wrote last edited by
                            #13

                            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

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved