简体   繁体   English

如何在Qt(Windows XP)中禁用禁用QScrollbar上下文菜单

[英]How to disable disable QScrollbar context menu in Qt (windows xp)

I have a Qt application running on a windows XP machine and i am trying to finally disable the context menu which pops up when right clicking on a scroll bar (with the "scroll down" and "page up" etc. .. ) inside this application. 我有一个在Windows XP机器上运行的Qt应用程序,并且我试图最终禁用右键单击其中的滚动条(带有“向下滚动”和“向上翻页”等的弹出菜单)。应用。

I tried the following things, which didn't work: 我尝试了以下操作,但无效:

ui->scrollArea->setContextMenuPolicy(Qt::NoContextMenu);
ui->scrollAreaWidgetContents->setContextMenuPolicy(Qt::NoContextMenu);            
ui->scrollArea->horizontalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);

ui->scrollArea->setContextMenuPolicy(Qt::PreventContextMenu);
ui->scrollAreaWidgetContents->setContextMenuPolicy(Qt::PreventContextMenu);       
ui->scrollArea->horizontalScrollBar()->setContextMenuPolicy(Qt::PreventContextMenu);

I never found out why it didn't really worked in my context (it seems it has to do with nested widgets and objects) but I found a solution which is something like a sledge-hammer method but works for me: 我从来没有发现为什么它不能在我的上下文中真正起作用(它似乎与嵌套的小部件和对象有关),但是我找到了一种类似于大锤方法的解决方案,但对我有用:

foreach(QObject *widget, qApp->allWidgets())
{
    QScrollBar *scrollBar = dynamic_cast<QScrollBar*>(widget);
    if(scrollBar)
    {
        scrollBar->setContextMenuPolicy(Qt::NoContextMenu);
    }
}

This disables context menus of scrollbars (or other objects if modified) in the whole application once at startup. 启动后,这将在整个应用程序中禁用滚动条(或其他对象,如果已修改)的上下文菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM