【问题标题】:QCalendarWidget as "Pop-up", not as new Window?QCalendarWidget 作为“弹出窗口”,而不是作为新窗口?
【发布时间】:2009-08-29 20:45:35
【问题描述】:

我想创建一个设置小部件,我可以在其中选择一个日期。
因为创建 3 个 QLineEdits 来使用 QDate(int year, int month, int day) 调用 QDate-Constructor 并不好,所以我认为如果你可以按下“显示日历”按钮会更好,例如,您可以在其中选择日期。
但我不想在新窗口中显示此日历,我想将其显示为“弹出窗口”(我不知道如何解释),例如您可能从 OpenOffice-Settings 中知道.
你知道如何实现吗?

【问题讨论】:

    标签: c++ qt qt4 qwidget


    【解决方案1】:

    这是一个弹出式日历的示例,当您按下表单上的按钮时,您必须显示日历。这个类可以在代码中的任何地方重用。在本例中,这是在 main 函数中启动的。

     /*
         * 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();
    }
    

    【讨论】:

    • 这个解决方案是最适合这个问题的解决方案,因为它确实是一个被提出的 QCalendarWidget
    【解决方案2】:

    对于替代选项,您是否考虑过使用QDateEdit?它将允许您的用户以与操作系统其余部分一致的格式编辑日期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多