日付を選択できる設定ウィジェットを作成したいと考えています。
QDate(int year, int month, int day) で QDate-Constructor を呼び出すために 3 つの QLineEdits を作成するのは良くないので、たとえば「カレンダーを表示」ボタンを押すことができれば、より良いと思いました。日付を選べるところ。
しかし、私はこのカレンダーを新しいウィンドウに表示したくありません。「ポップアップ」として表示したいのですが (これを説明する方法がわかりません)、たとえば OpenOffice の設定.
それを実装する方法はありますか?
4291 次
2 に答える
5
これはポップアップ型カレンダーの例です。フォームのボタンを押したときにカレンダーを表示する必要があります。このクラスは、コードのどこでも再利用できます。この例では、これは 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();
}
于 2009-08-29T22:30:40.780 に答える
2
別のオプションとして、使用を検討しましたQDateEdit
か?これにより、ユーザーは他のオペレーティングシステムと一貫性のある形式で日付を編集できます。
于 2009-08-30T00:04:13.027 に答える