Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QDateEdit日历弹出窗口_Qt_Qcalendarwidget - Fatal编程技术网

Qt QDateEdit日历弹出窗口

Qt QDateEdit日历弹出窗口,qt,qcalendarwidget,Qt,Qcalendarwidget,我正在尝试获取一个QDateEdit,以允许QCalendarWidget在请求时显示(而不仅仅是单击向下箭头)。例如,在我班的某个地方,我应该能够说: ui.datepicker.showCalendar() 它应该加载显示在日期选择器正下方的日历 看起来我需要将子类QDateEdit,因为这不起作用: QDateEdit *de = new QDateEdit(); de->calendarWidget()->show(); 我也尝试过在您浏览QDateTimeEdit.cp

我正在尝试获取一个
QDateEdit
,以允许
QCalendarWidget
在请求时显示(而不仅仅是单击向下箭头)。例如,在我班的某个地方,我应该能够说:

ui.datepicker.showCalendar()
它应该加载显示在日期选择器正下方的日历

看起来我需要将子类
QDateEdit
,因为这不起作用:

QDateEdit *de = new QDateEdit();
de->calendarWidget()->show();
我也尝试过在您浏览QDateTimeEdit.cpp Qt源代码时发送指定的键盘命令,但似乎我的键盘快捷键被禁用或其他什么

有什么想法我必须做的子类,让这个工作?我在想这样的事情:

class MyDateEdit : QDateEdit
{
  Q_OBJECT

protected:
  void mouseEvent(QEvent *event) {
    this.calendarWidget().show();
  }
};

但遗憾的是,这似乎也不能在中编译或正常工作。

我能够自己解决它-仍然不知道如何让QDateEdit正常工作,但我使用了QLineEdit,它适合我的需要。只需将QCalendarWidget的“onClick(QDate)”连接到您创建的插槽,该插槽执行以下操作:

setText(date.toString("M/d/yyyy"));
ui->calendar->hide();
然后使用“OnFocusIn”事件向QLineEdit添加事件筛选器,该事件执行“ui->calendar->show();”请参阅:QDateTimeEdit中的启用“设置日历弹出(bool启用)”允许弹出日历

您使用的事件过滤器方法是正确的,我们也会使用QDateEdit

我正在编写扩展QDateEdit方法的代码:

在mainwindow.h中,我创建了一个QCalendar指针(使用QtCreator)

以下是mainwindow.cpp的代码(我正在发布完整的代码,以便像我这样的新手可以从中受益)

确保将buttonSymbol和calendarpopup属性设置为false以使其正常工作

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCalendarWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->dateEdit->setDate(QDate::currentDate());

    widget=new QCalendarWidget(); //widget is QCalendar pointer

    ui->verticalLayout->addWidget(widget);
    widget->setWindowFlags(Qt::Popup); // we need widget to popup 

    ui->dateEdit->installEventFilter(this);
    connect(widget,SIGNAL(clicked(QDate)),ui->dateEdit,SLOT(setDate(QDate)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::InputMethodQuery)
    {
        if (object == ui->dateEdit)
        {

          if(widget->isVisible()==false && ui->dateEdit->calendarWidget()->isVisible()==false) // this done to avoid conflict
          {
                qWarning(QString().number(event->type()).toStdString().c_str());
                qWarning(object->objectName().toLatin1().data());
                widget->move(ui->dateEdit->mapToGlobal(QPoint(0,ui->dateEdit->height())));
                widget->show();
          }

        }

    }
    return false;
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
ui->dateEdit->setDate(QDate::currentDate());
widget=new QCalendarWidget();//widget是QCalendar指针
ui->verticalLayout->addWidget(小部件);
widget->setWindowFlags(Qt::Popup);//我们需要widget来弹出
ui->dateEdit->installEventFilter(此);
连接(小部件,信号(点击(QDate)),用户界面->日期编辑,插槽(设置日期(QDate));
}
MainWindow::~MainWindow()
{
删除用户界面;
}
bool主窗口::事件过滤器(QObject*对象,QEvent*事件)
{
if(事件->类型()==QEvent::InputMethodQuery)
{
如果(对象==ui->dateEdit)
{
如果(widget->isVisible()==false&&ui->dateEdit->calendarWidget()->isVisible()==false)//这样做是为了避免冲突
{
qWarning(QString().number(event->type()).tostString().c_str());
qWarning(对象->对象名().toLatin1().data());
widget->move(ui->dateEdit->mapToGlobal(QPoint(0,ui->dateEdit->height());
widget->show();
}
}
}
返回false;
}
或者::我们也可以使用dateEdit提供的QCalendarWidget,尽管它的效率不高,因为将其图灵化为弹出窗口会干扰其内部功能。如果你想试一试

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCompleter>
#include <QCalendarWidget>
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->dateEdit->setDate(QDate::currentDate());

    widget = ui->dateEdit->calendarWidget();
    widget->setWindowFlags(Qt::Popup);

    ui->dateEdit->installEventFilter(this);

    //connecting widget with dateedit
    ui->dateEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
    ui->dateEdit->setCalendarPopup(true);

    connect(widget,SIGNAL(clicked(QDate)),ui->dateEdit,SLOT(setDate(QDate)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if (object == ui->dateEdit)
    {
        if (event->type() == QEvent::FocusIn || event->type() == QEvent::MouseButtonPress)
        {    
           // WE NEED MOUSE EVENT TO AVOID INTERFERNCE WITH CALENDAR POPUP BUTTON SITUATED AT CORNER OF dateEdit WIDGET
            if(widget->isVisible()==false && ( ((QMouseEvent* )event)->x()< (ui->dateEdit->width()-10)))
            {
                widget->move(ui->dateEdit->mapToGlobal(QPoint(0,ui->dateEdit->height())));
                widget->show();
            }
        }    
    }
    return false;
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
#包括
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
ui->dateEdit->setDate(QDate::currentDate());
widget=ui->dateEdit->calendarWidget();
小部件->设置窗口标志(Qt::Popup);
ui->dateEdit->installEventFilter(此);
//使用dateedit连接小部件
ui->dateEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
ui->dateEdit->setCalendarPopup(真);
连接(小部件,信号(点击(QDate)),用户界面->日期编辑,插槽(设置日期(QDate));
}
MainWindow::~MainWindow()
{
删除用户界面;
}
bool主窗口::事件过滤器(QObject*对象,QEvent*事件)
{
如果(对象==ui->dateEdit)
{
如果(事件->类型()==QEvent::FocusIn | |事件->类型()==QEvent::MouseButtonPress)
{    
//我们需要鼠标事件来避免和dateEdit小部件角落处的日历弹出按钮发生冲突
如果(widget->isVisible()==false&((QMouseEvent*)事件)->x()<(ui->dateEdit->width()-10)))
{
widget->move(ui->dateEdit->mapToGlobal(QPoint(0,ui->dateEdit->height());
widget->show();
}
}    
}
返回false;
}

我想提供类似于@Dr.Xperience的选项,将日历小部件封装在QDateEdit子类中:

#包括
#包括
类DateEdit:公共QDateEdit{
Q_对象
公众:
显式日期编辑(QWidget*parent=nullptr);
受保护的:
虚拟无效焦点事件(QFocusEvent*事件)覆盖;
私人:
QCalendarWidget*日历=新的QCalendarWidget(此);
};
DateEdit::DateEdit(QWidget*parent):QDateEdit(parent){
setButtonSymbols(QAbstractSpinBox::NoButtons);
设置日历弹出窗口(假);
setDate(QDate::currentDate());
日历->设置窗口标志(Qt::弹出窗口);
连接(日历,&QCalendarWidget::单击,此,[&](常量QDate和日期){
设定日期(日期);
日历->隐藏();
});
}
作废日期编辑::聚焦事件(QFocusEvent*事件){
如果(!calendar->isVisible()){
日历->设置选定日期(日期());
日历->移动(映射全局(QPoint(0,height()));
日历->显示();
}
返回QDateEdit::focusInEvent(事件);
}

警告:如果使用QtDesigner放置此小部件,它将覆盖按钮符号和calendarPopup属性,因此您必须手动设置它以隐藏QDateEdit的按钮。

以下是我对该问题的拙劣处理方法。在为干净的东西奋斗了很长一段时间之后,我阅读了
QDateEditor
(实际上它只是一个简化的
QDateTimeEditor
)的源代码,但它似乎不是干净的解决方案。下面是
toggle()
的代码,而不是
show()
,但仍然是:

// Enable the calendar popup
date_editor->setCalendarPopup(true);

// Show the calendar popup by default
// There seems to be no proper interface to achieve that
// Fake a mouse click on the right-hand-side button
QPointF point = date_editor->rect().bottomRight() - QPointF{5, 5};
QCoreApplication::postEvent(
    date_editor,
    new QMouseEvent(QEvent::MouseButtonPress, point, Qt::LeftButton,
                    Qt::LeftButton, Qt::NoModifier));
使用这样的东西,你可以继续依靠
// Select a section so that the cursor is be visible
date_editor->setSelectedSection(QDateTimeEdit::DaySection);