开发者

Qt How to disable mouse scrolling of QComboBox?

I have some embedded QComboBox in a QTableView. To make them show by default I made those indexes "persistent editor". But now every 开发者_JS百科time I do a mouse scroll on top them they break my current table selection.

So basically how can I disable mouse scrolling of QComboBox?


As I found this question, when I tried to figure out the solution to (basically) the same issue: In my case I wanted to have a QComboBox in a QScrollArea in pyside (python QT lib).

Here my redefined QComboBox class:

#this combo box scrolls only if opend before.
#if the mouse is over the combobox and the mousewheel is turned,
# the mousewheel event of the scrollWidget is triggered
class MyQComboBox(QtGui.QComboBox):
    def __init__(self, scrollWidget=None, *args, **kwargs):
        super(MyQComboBox, self).__init__(*args, **kwargs)  
        self.scrollWidget=scrollWidget
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def wheelEvent(self, *args, **kwargs):
        if self.hasFocus():
            return QtGui.QComboBox.wheelEvent(self, *args, **kwargs)
        else:
            return self.scrollWidget.wheelEvent(*args, **kwargs)

which is callable in this way:

self.scrollArea = QtGui.QScrollArea(self)
self.frmScroll = QtGui.QFrame(self.scrollArea)
cmbOption = MyQComboBox(self.frmScroll)

It is basically emkey08's answer in the link Ralph Tandetzky pointed out, but this time in python.


The same can happen to you in a QSpinBox or QDoubleSpinBox. On QSpinBox inside a QScrollArea: How to prevent Spin Box from stealing focus when scrolling? you can find a really good and well explained solution to the problem with code snippets.


You should be able to disable mouse wheel scroll by installing eventFilter on your QComboBox and ignore the events generated by mouse wheel, or subclass QComboBox and redefine wheelEvent to do nothing.


c++ version of Markus ansver

class FocusWhellComboBox : public QComboBox
{
public:
  explicit FocusWhellComboBox(QWidget* parent = nullptr)
    : QComboBox(parent)
  {
    this->setFocusPolicy(Qt::StrongFocus);
  }

  void wheelEvent(QWheelEvent* e) override
  {
    if (this->hasFocus()) {
      QComboBox::wheelEvent(e);
    }
  }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜