Results 1 to 16 of 16

Thread: Simulating a left clic on a calendar widget

  1. #1
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Simulating a left clic on a calendar widget

    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

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "agenda.h"
    6.  
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. Agenda *agenda;
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cc

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    5.  
    6. ui->setupUi(this);
    7. agenda = new Agenda;
    8. this->setCentralWidget(agenda);
    9.  
    10.  
    11. }
    12.  
    13. MainWindow::~MainWindow() {
    14. delete ui;
    15. delete agenda;
    16. }
    To copy to clipboard, switch view to plain text mode 

    agenda.h

    Qt Code:
    1. #ifndef AGENDA_H
    2. #define AGENDA_H
    3.  
    4. #include <QCalendarWidget>
    5. #include <QMouseEvent>
    6.  
    7. class Agenda : public QCalendarWidget {
    8. Q_OBJECT
    9. public:
    10. explicit Agenda(QWidget *parent = 0);
    11.  
    12. signals:
    13.  
    14. public slots:
    15.  
    16. protected:
    17. void mousePressEvent(QMouseEvent *event);
    18.  
    19. };
    20.  
    21. #endif // AGENDA_H
    To copy to clipboard, switch view to plain text mode 

    agenda.cc

    Qt Code:
    1. #include "agenda.h"
    2.  
    3. Agenda::Agenda(QWidget *parent) : QCalendarWidget(parent) { }
    4.  
    5. void Agenda::mousePressEvent(QMouseEvent *event) {
    6. if(event->button() == Qt::RightButton) {
    7. QMouseEvent e(QMouseEvent::MouseButtonPress, event->pos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    8.  
    9. Agenda::mousePressEvent(&e);
    10. }
    11. Agenda::mousePressEvent(event);
    12. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by wysota; 19th June 2011 at 00:43. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulating a left clic on a calendar widget

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    Quote Originally Posted by saimel View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulating a left clic on a calendar widget

    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

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simulating a left clic on a calendar widget

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulating a left clic on a calendar widget

    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    Quote Originally Posted by saimel View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simulating a left clic on a calendar widget

    Quote Originally Posted by wysota View Post
    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulating a left clic on a calendar widget

    I don't understand what you are telling me, so please can you be more especific ??

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    Which part you don't understand?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulating a left clic on a calendar widget

    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

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simulating a left clic on a calendar widget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    saimel (30th June 2011)

  17. #16
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulating a left clic on a calendar widget

    I tried it and work out, thanks very much

Similar Threads

  1. Opensource Calendar Widget
    By donmorr in forum Qt Tools
    Replies: 5
    Last Post: 15th February 2012, 18:54
  2. Calendar Widget - Change cell colour
    By Gavin Harper in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2011, 08:31
  3. Replies: 10
    Last Post: 29th May 2010, 18:42
  4. How to call a function when i clic a button?
    By trend900 in forum Newbie
    Replies: 2
    Last Post: 22nd January 2009, 16:40
  5. Calendar widget with selectionMode != SingleSelection
    By Tamara in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2007, 10:13

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.