Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    @MokJ
    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;);
     ButtonRect.setY(textRect.y()+35);
     ButtonRect.setHeight(30);
     QStyleOptionButton button;
        
        button.rect=ButtonRect;
        button.text=item->text();
        button.state=m_state|QStyle::State_Enabled;
    QApplication::style()->drawControl(QStyle::CE_PushButton,&button,painter);
    }
    QSize myDeligate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{
    }
    bool myDeligate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){
                if(event->type()==QEvent::MouseButtonPress){
                            emit buttonClicked(index);
        }
    }
    
    

    I have connected

    emit buttonClicked(index);
    

    to an appropriate slot , Which does

    qDebug()<<"Clicked";
    

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


  • Moderators

    @MokJ:
    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