PDA

View Full Version : Simulating a left clic on a calendar widget



saimel
19th June 2011, 01:07
Hello,

I have a problem here: i want to make an agenda, for that i want to make a context menu that is called on a day, but the right clic do not select the day under the cursor, for that reason i wanna simulate a left clic, for selecting the day under the cursor and then, call the context menu with the given selected day.

This is the code that I have:

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "agenda.h"


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
Agenda *agenda;
};

#endif // MAINWINDOW_H

mainwindow.cc


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {

ui->setupUi(this);
agenda = new Agenda;
this->setCentralWidget(agenda);


}

MainWindow::~MainWindow() {
delete ui;
delete agenda;
}

agenda.h


#ifndef AGENDA_H
#define AGENDA_H

#include <QCalendarWidget>
#include <QMouseEvent>

class Agenda : public QCalendarWidget {
Q_OBJECT
public:
explicit Agenda(QWidget *parent = 0);

signals:

public slots:

protected:
void mousePressEvent(QMouseEvent *event);

};

#endif // AGENDA_H

agenda.cc


#include "agenda.h"

Agenda::Agenda(QWidget *parent) : QCalendarWidget(parent) { }

void Agenda::mousePressEvent(QMouseEvent *event) {
if(event->button() == Qt::RightButton) {
QMouseEvent e(QMouseEvent::MouseButtonPress, event->pos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);

Agenda::mousePressEvent(&e);
}
Agenda::mousePressEvent(event);
}


it doesn't work, when I press the mouse's right button the day under the cursor keeps unselected

Please somebody help me

thanks very much

wysota
19th June 2011, 01:45
Isn't it better to just set the value of the QCalendarWidget::selectedDate instead of simulating some clicks?

By the way, triggering a context menu on a right mouse button is a bad idea. There is a dedicated mechanism for context menus (or even three mechanisms).

saimel
19th June 2011, 03:04
yes, is a better idea just to see the value of the QCalendarWidget::selectedDay if the day on I want programming a task is the selected day, but the selected day can be jun 18 and I maybe want to set a task for jun 25, so I have to clic on jun 25

I know that is not necessary make it using right clic, I can do it just using double clic on a date, but the doubt just came up, furthermore, I want to execuete the default action using a double clic on a date and a context menu when the right button is clicked on a date too

wysota
19th June 2011, 10:20
yes, is a better idea just to see the value of the QCalendarWidget::selectedDay if the day on I want programming a task is the selected day, but the selected day can be jun 18 and I maybe want to set a task for jun 25, so I have to clic on jun 25
No, you have to select jun 25. Actually you don't have to but that's a secondary issue.


I know that is not necessary make it using right clic, I can do it just using double clic on a date
That's not what I meant. Context menu is good, just handle it in a proper event --- look what events QWidget has available, I'm sure you'll quickly find the right one.


but the doubt just came up, furthermore, I want to execuete the default action using a double clic on a date and a context menu when the right button is clicked on a date too
What if someone wants to trigger a context menu by means different than right-clicking the mouse?

saimel
20th June 2011, 03:12
Maybe I should give more details about I wanna do, so here I will explain better

suppose you have a window that only contains a QCalendarWidget item, and no more Qt components, by default the selected day is the current date, so my idea is giving to the users the possibility of create new tasks for a given day (action triggered by double click on a day), modify or delete a task (triggering on a day a context menu with this actions), and paint the cells of the days that have a programmed task, just for the user knowledge,

for example, if the selected day is Jun 19, and I have a task programed in Jun 25 and I want to edit this task, I have to make a left click on Jun 25 for selecting this day, and then make right click for trigger the context menu on Jun 25, for that reason I wanna simulate a left click, to select and later trigger a context menu making just a right click on a day that was not selected

nish
20th June 2011, 07:32
so basically your problem boils down to "selecting the date with right click".
I your first code, you only issued MousePress, but the "click" requires MouePress+MouseRelease, so try to override the mouseRelease the same way as u did your mousePress.

wysota
20th June 2011, 09:23
You don't have to simulate anything, you can just select the date using QCalendarWidget::selectedData property while handling the context menu. Not that it's a right thing to do as on most platforms invoking a context menu over an item shouldn't cause the item to become selected.

saimel
21st June 2011, 00:22
well, at least until I know, the QCalendarWidget::selectedDate property return the selected date, it is the day that is marked as blue, but if I make a right click on another day (not the day that is selected), and I handling the context menu passing the QCalendarWidget::selectedDate value, the given value to context menu is not the day under the cursor, that is what I want, if the selected day is Jun 20 and I put the cursor on Jun 26 and make a right click, the selected day just keep as Jun 20, and if I pass that value to the context menu it will be wrong because the day that I wanna pass is Jun 26, for that reason I wnat to simulate a left click

maybe there is another way to do it, but I don know it, and that is what you have been trying to explain me, but it is not clear enough for me

thanks very much

wysota
21st June 2011, 01:23
well, at least until I know, the QCalendarWidget::selectedDate property return the selected date, it is the day that is marked as blue, but if I make a right click on another day (not the day that is selected), and I handling the context menu passing the QCalendarWidget::selectedDate value, the given value to context menu is not the day under the cursor, that is what I want, if the selected day is Jun 20 and I put the cursor on Jun 26 and make a right click, the selected day just keep as Jun 20, and if I pass that value to the context menu it will be wrong because the day that I wanna pass is Jun 26, for that reason I wnat to simulate a left click
You still don't get it, do you? It's not a read-only property, you can assign a value to it and the field will become selected.

nish
21st June 2011, 07:11
You still don't get it, do you? It's not a read-only property, you can assign a value to it and the field will become selected.
how will he know which date to select? He needs the api like that of itemViews ( listView->itemAt(QPoint)->setSelected(true) ) to select the particular date under the mouse cursor

wysota
21st June 2011, 09:44
The view is actually a table view and its model returns the day of the month as its display role. The view itself can be retrieved using QObject::findChild().

saimel
22nd June 2011, 16:42
I don't understand what you are telling me, so please can you be more especific ??

wysota
22nd June 2011, 17:07
Which part you don't understand?

saimel
24th June 2011, 20:48
The view is actually a table view and its model returns the day of the month as its display role. The view itself can be retrieved using QObject::findChild().

I don't understand this

wysota
24th June 2011, 20:52
QCalendarWidget is really composed of a couple of other widgets. The main widget is the table you see that displays days of month. This table is a QTableView and can be extracted from the calendar widget using QObject::findChild(). Once you have that widget, you can access its API including QAbstractItemView::itemAt() which will return you the day of month at a specified point in the view's viewport coordinates. Then you can use QCalendarWidget::setSelectedDate() to select that date.

saimel
30th June 2011, 23:38
I tried it and work out, thanks very much