Clearing a QMenu while open toggles visibility



  • I found a weird behavior with context menus. When I clear it, it toggles visibility.

    Create a Qt desktop app and a window with a tree view. Insert the following:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
    }
    
    void MainWindow::on_treeView_customContextMenuRequested(const QPoint &pos)
    {
        QMenu *m = new QMenu();
        m->addAction("foo");
        QTimer t;
        connect(&t, &QTimer::timeout, this, [=](){
            m->clear();
            m->addAction("bar");
        });
        t.start(2000);
        m->exec();
    }
    
    

    When I now right-click into the tree view, my context menu opens (top, left!), and with each execution of the timer, the context menu gets invisible, and visible again, and so on.

    Note: Although it effectively triggers visibility, it doesn't in the data structures. In my real program, I can trigger the actions by clicking on the area nevertheless.

    I'm not sure if I should report it too fast. I suspect it might depend on some part of my environment (Qt version, KDE, Ubuntu, GPU, whatever, ...). It looks so low-level... Can you reproduce it? Or, is it okay to report it directly without further checks (sorry for my stupid question ;) )?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you give more details about your environment ?


  • Qt Champions 2016

    Hi
    Note:
    It was easy to test so i did it on win 10/Qt.57 vs 2015
    I did see some strange flashing so i tried the following

    ....
    connect(&t, &QTimer::timeout, this, = {
    m->setUpdatesEnabled(false) ;
    m->clear();
    m->addAction("bar");
    m->setUpdatesEnabled(true) ;
    });
    ....
    Which seems to reduce it a lot.



  • @SGaist Hi, thank you! It's a kubuntu 17.04 (with its Qt 5.7.1), amdgpu gpu driver.



  • @mrjj Okay, then it's more specific. The setUpdatesEnabled calls haven't changed the behavior here.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.