Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ItemChanged & mousePressEnvent at same time

ItemChanged & mousePressEnvent at same time

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cinegemadar
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cinegemadar
        wrote on last edited by
        #3

        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 )

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          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?

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved