zoukankan      html  css  js  c++  java
  • QT 在QCustomCalendarWidget添加今天按钮

    QDateEdit可以弹出日期选择框

     qt默认的日期选择窗口是不带today按钮的,如何添加按钮快速定位到今天日期呢?

    我们可以自定义一个类通过继承QCalendarWidget的形式来实现

    详细代码演示

    .h头文件

    #ifndef QCUSTOMCALENDARWIDGET_H
    #define QCUSTOMCALENDARWIDGET_H
    #include "custom_global.h"
    #include <QCalendarWidget>
    class QHBoxLayout;
    class MYEXPORT QCustomCalendarWidget : public QCalendarWidget
    {
    Q_OBJECT
    public:
    QCustomCalendarWidget(QWidget *parent);
    ~QCustomCalendarWidget();
    private:
    QHBoxLayout* hboxLayoutOuter;
    private:
    void initBottomWidget();
    void initUi();
    };
    #endif // QCUSTOMCALENDARWIDGET_H

    .cpp文件

    #include "QCustomCalendarWidget.h"
    #include "qboxlayout.h"
    #include "qpushbutton.h"
    QCustomCalendarWidget::QCustomCalendarWidget(QWidget *parent):
    QCalendarWidget(parent)
    {
    initUi();
    initBottomWidget();
    }

    QCustomCalendarWidget::~QCustomCalendarWidget()
    {

    }

    void QCustomCalendarWidget::initBottomWidget()
    {
    QWidget* bottomWidget = new QWidget(this);
    bottomWidget->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);

    QHBoxLayout* hboxLayout = new QHBoxLayout;


    QSpacerItem * spcer1 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    hboxLayout->addItem(spcer1);

    QPushButton *totayBtn = new QPushButton(this);
    totayBtn->setText(tr("today"));
    hboxLayout->addWidget(totayBtn);

    spcer1 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    hboxLayout->addItem(spcer1);

    bottomWidget->setLayout(hboxLayout);

    connect(totayBtn,&QPushButton::clicked,this,[this]()
    {
    this->setSelectedDate(QDate::currentDate());
    });
    this->layout()->addWidget(bottomWidget);
    }

    void QCustomCalendarWidget::initUi()
    {
    setNavigationBarVisible(false);
    setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    }

    通过上述源码就是实现了QDateEdit弹出日期窗口的自定义

    然后可以在其他文件里面通过如下代码来使用这个自定义的类

    QCustomCalendarWidget *customCalendarWidget = new QCustomCalendarWidget(this);

    QDateEdit *dateEdit = new QDateEdit(this);
    dateEdit ->setCalendarWidget(customCalendarWidget);

    即可完成自定义窗口

    需要注意的地方是QCustomCalendarWidget *customCalendarWidget = new QCustomCalendarWidget(this); 

    new这个对象的时候注意把当前窗口作为父对象传给它,因为引用官网上的一句话

    void QDateTimeEdit::setCalendarWidget(QCalendarWidget *calendarWidget)

    Sets the given calendarWidget as the widget to be used for the calendar pop-up. The editor does not automatically take ownership of the calendar widget

    calendar widget并不会自动把editor作为自己的父控件,为了避免内存泄漏,在new calendar widget把当前窗口传给它作为父对象即可。

    PS:如果你有什么好方法,或者有什么需要指正的地方,请在下面留言,知识因为分享而变得更加美好

  • 相关阅读:
    LeetCode_1025.除数博弈
    LeetCode_102.二叉树的层序遍历
    Oracle报错:不是GROUP BY 表达式
    Oracle报错:不是单组分组函数
    不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的表进行了更改或启用了“阻止保存要求重新创建表的更改”选项
    分页存储过程
    线程间操作无效: 从不是创建控件“labMessage”的线程访问它。
    C#连接oracle 数据库查询时输入中文查询不出来,用plsql就可以
    select ,update 加锁
    错误代码: 0x800700b7 配置错误:定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节
  • 原文地址:https://www.cnblogs.com/tianmochou/p/14474439.html
Copyright © 2011-2022 走看看