我想创建一个设置窗口小部件,在那里我可以选择一个日期。点击 因为它是不是很好创建3个QLineEdits调用QDATE-构造与QDATE(INT年,月整型,诠释天),我认为这将是更好的,如果你可以把一个“显示日历” - 键例如,在这里你可以选择的日期。结果 但我不希望显示在新窗口中这个日历,我想它显示为一个“弹出”(我不知道该怎么解释这一点),你可能知道,例如从OpenOffice的,设置。结果 你有什么想法如何实现?

有帮助吗?

解决方案

有关的替代选项,你认为使用 QDateEdit ?它将允许用户编辑的格式与操作系统的其他部分相一致的日期。

其他提示

这是一种类型的弹出日历的一个例子,则必须在按窗体上的按钮显示日历。这个类可以在你的代码的任何地方被重新利用。在该示例中这是在主功能启动。

 /*
     * DatePopup.h
     *
     *  Created on: Aug 29, 2009
     *      Author: jordenysp
     */

#ifndef DATEPOPUP_H_
#define DATEPOPUP_H_

#include <QDialog>
#include <QDate>
class QCalendarWidget;
class QDialogButtonBox;
class QVBoxLayout;

class DatePopup : public QDialog{
    Q_OBJECT
public:
    DatePopup(QWidget *parent=0);
    QDate selectedDate() const;

private:
    QWidget *widget;
    QCalendarWidget *calendarWidget;
    QDialogButtonBox* buttonBox;
    QVBoxLayout *verticalLayout;

};

#endif /* DATEPOPUP_H_ */


/*
 * DatePopup.cpp
 *
 *  Created on: Aug 29, 2009
 *      Author: jordenysp
 */

#include <QtGui>
#include "DatePopup.h"

DatePopup::DatePopup(QWidget *parent)
:QDialog(parent, Qt::Popup)
{
    setSizeGripEnabled(false);
    resize(260, 230);
    widget = new QWidget(this);
    widget->setObjectName(QString::fromUtf8("widget"));
    widget->setGeometry(QRect(0, 10, 258, 215));

    verticalLayout = new QVBoxLayout(widget);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalLayout->setContentsMargins(0, 0, 0, 0);

    calendarWidget = new QCalendarWidget(widget);
    calendarWidget->setObjectName(QString::fromUtf8("calendarWidget"));

    verticalLayout->addWidget(calendarWidget);

    buttonBox = new QDialogButtonBox(widget);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

    verticalLayout->addWidget(buttonBox);

    QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

QDate DatePopup::selectedDate() const{
    return calendarWidget->selectedDate();
}




#include <QtGui>
#include <QDate>
#include <QApplication>
#include "DatePopup.h"
#include <iostream>

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

    DatePopup popup;

    int result = popup.exec();
    if(result == QDialog::Accepted){
        QDate date = popup.selectedDate();
        std::cout<< date.year() <<std::endl;
        std::cout<< date.month() <<std::endl;
        std::cout<< date.day() <<std::endl;
    }

    return a.exec();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top