Important: Please read the Qt Code of Conduct -

How to disable mouseClick event!

  • I have created buttons using QItemDelegate for displaying them in a QListView , in which those buttons should be clickable, which is working too.
    But the problem is the rest of the part of the widget , I want it to do nothing if I click the rest of the area, is there a way !?

  • Moderators

    show how you created your delegate excatly

  • I am subclassing the QItemDelegate.

    void myDeligate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    // I'm creating a QRect and printing it as a button
     QRect ButtonRect(option.rect;);
     QStyleOptionButton button;
    QSize myDeligate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{
    bool myDeligate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){
                            emit buttonClicked(index);

    I have connected

    emit buttonClicked(index);

    to an appropriate slot , Which does


    And I'm receiving that message by clicking all over the widget.
    Thank you.

  • Moderators

    of course. You accept any mouse press event in the widget, no matter where it appeared.

    bool myDeligate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){
                if(event->type()==QEvent::MouseButtonPress ) {
                            QMouseEvent* me = static_cast<QMouseEvent*>(event);
                            if( option.rect.contains(me->pos()) )
                                  emit buttonClicked(index);

  • @raven-worx Thanks for responding , The problem is solved.
    I had to handle the QMouseEvent properly.

Log in to reply