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:
Code:
connect(this,
SIGNAL(clicked
(QDate)),
this,
SLOT(dateSelected
(QDate)));
...
void CalendarManagerRep
::dateSelected(const QDate &date
) {
m_selectedDate = date;
menu.addAction("0",this, SLOT(noSelection()));
menu.addAction("½",this, SLOT(halfSelected()));
menu.addAction("1",this, SLOT(oneSelected()));
auto view = this->findChild<QAbstractItemView*>();
if(view){
view->viewport()->update();
} else update(); // fallback
emit datesChanged();
}
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
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.
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.