PDA

View Full Version : Calendar Pop Up



Rakesh_Kumar
3rd December 2008, 06:40
Hi ,

I have a QdateEdit and Qbutton. Whenever I click on the button the calendar of the dateedit should be poped up.

I have tried to make a calendarwidget as given in the attachment. Intially I have made setVisible(false) for the calendar widget. Then I connected the click event of the button to setVisible(true) for the calendar widget. It's working fine. But this component is to be placed on another container. It has limited space. I can't give space for the calendar widget in the container. So it's hidding the view of calendar widget.

In stead of doing this if any way I can make the calendar of Dateedit visible the problem will be solved.

Please give me some idea to resolve it.

Thanks and Regards,
Rakesh

jpn
4th December 2008, 19:55
You don't need a separate button. QDateTimeEdit (notice that QDateEdit is a QDateTimeEdit) has built-in support for a "calendar popup". Look into QDateTimeEdit docs for more details.

Rakesh_Kumar
5th December 2008, 03:02
Hi ,

Thanks for your reply. I know that QDateTimeEdit has a property to setcalendarpopup.
But my project requirement is we need a separate button so that we can have an icon for the button. Whenever I click on the icon the calendar should popped up.

So please give me some idea how to do it.

Regards,
Rakesh

jpn
5th December 2008, 06:19
You mean something like this?


#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QWidget window;
QToolButton* toolButton = new QToolButton(&window);
toolButton->setText("QToolButton");
QPushButton* pushButton = new QPushButton("QPushButton", &window);
QHBoxLayout* layout = new QHBoxLayout(&window);
layout->addWidget(toolButton);
layout->addWidget(pushButton);
window.show();

QMenu* toolMenu = new QMenu(toolButton);
QCalendarWidget* toolCalendar = new QCalendarWidget(toolMenu);
QWidgetAction* toolAction = new QWidgetAction(toolMenu);
toolAction->setDefaultWidget(toolCalendar);
toolMenu->addAction(toolAction);
toolButton->setMenu(toolMenu);

QMenu* pushMenu = new QMenu(pushButton);
QCalendarWidget* pushCalendar = new QCalendarWidget(pushMenu);
QWidgetAction* pushAction = new QWidgetAction(pushMenu);
pushAction->setDefaultWidget(pushCalendar);
pushMenu->addAction(pushAction);
pushButton->setMenu(pushMenu);

QObject::connect(toolCalendar, SIGNAL(currentPageChanged(int, int)), pushCalendar, SLOT(setCurrentPage(int, int)));
QObject::connect(pushCalendar, SIGNAL(currentPageChanged(int, int)), toolCalendar, SLOT(setCurrentPage(int, int)));
QObject::connect(toolCalendar, SIGNAL(clicked(QDate)), pushCalendar, SLOT(setSelectedDate(QDate)));
QObject::connect(pushCalendar, SIGNAL(clicked(QDate)), toolCalendar, SLOT(setSelectedDate(QDate)));

return app.exec();
}

QToolButton is a bit more flexible because it offers different popup modes. The default popup mode is delayed.

Rakesh_Kumar
5th December 2008, 08:52
Hi,

Thanks a lot. It's really matches my requirement.

Now I am using QPushButton as you explained in the code. One problem is I am not able to set the location for the calendar widget. I tried pushCalendar->setGeometry(). but it's not working.

Is there any way I can set the position for Calendar widget?

Regards,
Rakesh

jpn
5th December 2008, 09:46
You can show the calendar as a top level popup yourself in response to a button click, instead of (ab)using the readily available button menu functionality.

Rakesh_Kumar
5th December 2008, 09:59
Hi ,

Thanks a lot for your reply. I am not aware how to implement this. Could you please provide me a sample code for doing this?

Regards,
Rakesh

jpn
5th December 2008, 13:31
I am not aware how to implement this. Could you please provide me a sample code for doing this?
No, I won't code it for you just like that. This is not a coding service forum. Please try at least something on your own. Here are some hints: QAbstractButton::clicked(), QCalendarWidget and Qt::Popup.