Results 1 to 3 of 3

Thread: Right Click a QCalendarWidget date

  1. #1
    Join Date
    Oct 2016
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Right Click a QCalendarWidget date

    Hi, my application currently implements a QCalendarWidget and when a user clicks any date a QMenu popup to choose an option:

    Qt Code:
    1. connect(this, SIGNAL(clicked(QDate)), this, SLOT(dateSelected(QDate)));
    2.  
    3. ...
    4.  
    5. void CalendarManagerRep::dateSelected(const QDate &date)
    6. {
    7. m_selectedDate = date;
    8. QMenu menu;
    9. menu.addAction("0",this, SLOT(noSelection()));
    10. menu.addAction("½",this, SLOT(halfSelected()));
    11. menu.addAction("1",this, SLOT(oneSelected()));
    12. menu.exec(QCursor::pos());
    13.  
    14. auto view = this->findChild<QAbstractItemView*>();
    15. if(view){
    16. view->viewport()->update();
    17. } else update(); // fallback
    18.  
    19. emit datesChanged();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Now I would like to move the QMenu on the right-click and leave left-click for a default value.

    I tried to reimplement void QWidget::mouseReleaseEvent(QMouseEvent *event) but it seems to trigger only when right-clicking the calendar header.

    Any clue? Very thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Right Click a QCalendarWidget date

    You might have to derive a new class from QCalendarWidget and override some of the mouse handling. My guess is that the widget eats the mouse events and converts them into the other signals.

    For starters, you could implement an event filter then use the debugger to see what happens in response to the mousePresseEvent and mouseReleaseEvent events. I imagine a complex widget like this could be pretty easy to break if you start adding custom handling for events.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2012
    Location
    Austria
    Posts
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Right Click a QCalendarWidget date

    You could get the right-click coordinate by setting the contextMenuPolicy to Qt::CustomContextMenu and listen to customContextMenuRequested(). But I see no way to translate the coordinate to the proper QDate displayed at this location. Similar problem if you derive from QCalendarWidget and reimplement mousePressEvent. You would need something like dateAt, similar to indexAt of QTableView.

Similar Threads

  1. Replies: 2
    Last Post: 30th August 2019, 21:16
  2. Replies: 1
    Last Post: 10th March 2014, 13:25
  3. Replies: 1
    Last Post: 5th March 2014, 01:41
  4. Replies: 1
    Last Post: 24th April 2010, 16:31
  5. How to default Date-Edit Widget to system date
    By JohnToddSr in forum Qt Tools
    Replies: 4
    Last Post: 17th January 2007, 20:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.