ItemChanged & mousePressEnvent at same time



  • Hello,

    I have a big problem with Qt signals and slots.

    I wish the following features for my subclassed QTreeWidget:
    1.) On right click emit customContextMenuRequested ( for a right click context menu )
    2.) handle the itemChanged signal as well

    Now it's seems to me that I have to choose, because if I handle mousePress and check whether it emited by right mouse click or not, than I'm not abel to use mouse left button to select (and modify) ittems in the tree.

    So my question is: How can I handle the mousePress signal and than throw this full mouse thing away (give it back to treeWidget).

    Main part of my src:

    @
    /*
    treeWidget.cpp inherited from QTreeWidget
    /
    connect(this,SIGNAL(itemChanged(QTreeWidgetItem
    ,int)),
    this,SLOT(on_Item_Changed(QTreeWidgetItem*)));
    connect(this,SIGNAL(mousePressEvent(QMouseEvent*)),
    this,SLOT(on_Mouse_Click(QMouseEvent*)));
    /*
    (...)
    /
    void TreeWidget::on_Item_Changed(QTreeWidgetItem item)
    {
    /
    Some code
    /
    }
    void TreeWidget::on_Mouse_Click( QMouseEvent *event )
    {
    if( event->button() == Qt::RightButton )
    {
    emit customContextMenuRequested(event->pos());
    }

    }@

    Thx for any help (search keywords as well),

    David



  • Hi cinegemadar,

    Does this code really works? mousePressEvent is not a signal, it's a viurtual function. You have to overwrite it and call the base method:

    @
    /*
    treeWidget.cpp inherited from QTreeWidget
    /
    connect(this,SIGNAL(itemChanged(QTreeWidgetItem
    ,int)),
    this,SLOT(on_Item_Changed(QTreeWidgetItem*)));
    /*
    (...)
    /
    void TreeWidget::on_Item_Changed(QTreeWidgetItem item)
    {
    /
    Some code
    /
    }
    void TreeWidget::mousePressEvent( QMouseEvent *event )
    {
    if( event->button() == Qt::RightButton )
    {
    emit customContextMenuRequested(event->pos());
    }
    else
    QTreeWidget::mousePressEvent(event );
    }
    @

    This should do the job.



  • Dear Gerolf,

    It realy works thx!

    For your question:
    In the header file of my class I wrote the next lines:

    @
    signals:
    void customContextMenuRequested(const QPoint &pos);
    void mousePressEvent(QMouseEvent *event);
    @

    And than my code worked (with the problem I sad).

    But now, with your suggestion there is no more problem.

    So thank you very much!

    (ps.: I'm realy new here, so I don't konw if I can "like" your answer or mark it as useful )



  • Hi,

    like will be a feature in future :-)

    I suggest you rename your signal to onMousePressEvent. a signal is made to a method by moc and the method mousePressEvent already exists, which means, you overwrite it. That is not a good idea.

    Additionally, creating a signal with pointers may lead to undefined behavior. Slots may be executed asynchronously. This implies the values (in this case the value of the pointer, not the object behind) is put on the heap somewhere and delivered later on, where the real object might already be destroyed.

    Where did you emit the signam mousePressEvent?



  • For all item views inheriting from [[Doc:QAbstractScrollArea]], they all can reimplement the virtual method "contextMenuEvent() ":/doc/qt-4.8/qabstractscrollarea.html#contextMenuEvent - you can emit your signal from this one. It should be a better place than the mouse event handler.

    Also there's the builtin signal "customContextMenuRequested() ":/doc/qt-4.8/qwidget.html#customContextMenuRequested of [[Doc:QWidget]], this is emitted in case the "contextMenuPolicy":/doc/qt-4.8/qwidget.html#contextMenuPolicy-prop property is set to Qt::CustomContextMenu. I would guess, this fits your needs even better.


Log in to reply
 

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