欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Qt制作簡(jiǎn)單的日期選擇界面

 更新時(shí)間:2023年03月08日 10:37:44   作者:TGTSTTG  
Qt自帶的日期選擇控件過(guò)于丑陋與難用,所以但凡有點(diǎn)小想法的人都會(huì)做一個(gè)全新的日歷。這篇文章就來(lái)利用Qt制作一個(gè)簡(jiǎn)單的日期選擇界面,感興趣的可以了解一下

Qt自帶的日期選擇控件過(guò)于丑陋與難用,所以但凡有點(diǎn)小想法的人都會(huì)做一個(gè)全新的日歷。

這就是本人制作的日歷的截圖:

制作日歷的核心難點(diǎn)是填充日歷。以下為我的填充日歷函數(shù):

void STCalandarWidget::FillCalandar()
{
    QDate firstDay;
    firstDay.setDate(currentDate.year(), currentDate.month(), 1);
    int firstnum = firstDay.dayOfWeek();//這個(gè)月的第一天是星期幾
    qDebug() << firstnum;
    QDate firstDayOfMonth = firstDay.addDays(-(firstnum - 1));//日歷的第一天實(shí)際的日期
    for (int i = 0; i < 42; i++) {
        if (i < firstnum-1 || (firstDayOfMonth.month() != currentDate.month())) {//理論上只需要后半邊的條件就行
            datewidgets[i]->SetDate(firstDayOfMonth.year(), firstDayOfMonth.month(), firstDayOfMonth.day(), false);
        }
        else {
            datewidgets[i]->SetDate(firstDayOfMonth.year(), firstDayOfMonth.month(), firstDayOfMonth.day(), true);
        }
        firstDayOfMonth = firstDayOfMonth.addDays(1);
    }
}

鄙人制作的日歷使用起來(lái)非常簡(jiǎn)單,僅需將兩個(gè)頭文件和兩個(gè)CPP加入到工程中,使用時(shí)的代碼如下:

  calandar_ = new STCalandarWidget(this);
   connect(calandar_, SIGNAL(DateSelectSignal(QDate)), this, SLOT(HaveDateChose(QDate)));
   calandar_->exec();

當(dāng)日歷的日期被選擇時(shí),會(huì)發(fā)送DateSelectSignal信號(hào),調(diào)用者寫一個(gè)槽函數(shù)接受一下就可以:

void MyClass::HaveDateChose(QDate c_dates)
{
    ui.dateedit->setDate(c_dates);
    calandar_->accepted();
    calandar_->deleteLater();
}

具體日歷的實(shí)現(xiàn)如下,當(dāng)然不想登錄的同志們可以通過(guò)百度網(wǎng)盤進(jìn)行下載:

百度網(wǎng)盤鏈接:

鏈接: https://pan.baidu.com/s/1x8EMfKYP3t04ssG-UpoGMQ 提取碼: vwqw

STDateWidget.h:

#pragma once
#include <qwidget.h>
#include <QDate>
#include <QPainter>
class STDateWidget :
    public QWidget
{
    Q_OBJECT
public:
    enum Direction {
        DIR_TOP = 0,
        DIR_BOTTOM,
        DIR_LEFT,
        DIR_RIGHT
    };
 
    STDateWidget(QWidget* parrent = nullptr);
    ~STDateWidget();
    void SetDate(int year, int month, int day, bool isThisMonth);
    QDate GetCurrentDate();
    void AddNeighbor(STDateWidget* wid,Direction dir);
    void HaveGoodNeighbor(Direction dir);
    void DeleteGoodNgithbor(Direction dir);
 
 
private:
    bool isHasGoodNeighbor;
    bool canSelect;
    bool isMoveIn;
    Direction direction;
    QDate currentDate;
    QList<STDateWidget*> neighbors;
    QList<Direction>directions;
protected:
    void mouseReleaseEvent(QMouseEvent* event) ;
    void enterEvent(QEvent* event);
    void leaveEvent(QEvent* event);
    void paintEvent(QPaintEvent* event);
 
signals:
    void updateCurrentDate(QDate date);
};

STDateWidget.cpp

#include "STDateWidget.h"
#include <QMouseEvent>
#include <QLinearGradient>
STDateWidget::STDateWidget(QWidget* parrent /* = nullptr */) :QWidget(parrent)
{
    isMoveIn = false;
    //qDebug() << "make";
    //this->setGeometry(0, 0, 180, 160);
    isHasGoodNeighbor = false;
    //this->setStyleSheet("background-color:red");
}
STDateWidget::~STDateWidget()
{
}
 
void STDateWidget::SetDate(int year, int month, int day, bool isThisMonth)
{
    currentDate.setDate(year, month, day);
    canSelect = isThisMonth;
    
    update();
}
 
QDate STDateWidget::GetCurrentDate()
{
    return currentDate;
}
 
void STDateWidget::AddNeighbor(STDateWidget* wid, Direction dir)
{
    neighbors.append(wid);
    directions.append(dir);
}
 
void STDateWidget::HaveGoodNeighbor(Direction dir)
{
    isHasGoodNeighbor = true;
    direction = dir;
    update();
}
 
void STDateWidget::DeleteGoodNgithbor(Direction dir)
{
    isHasGoodNeighbor = false;
    direction = dir;
    update();
}
 
void STDateWidget::mouseReleaseEvent(QMouseEvent* event)
{
    if (canSelect) {
        emit updateCurrentDate(currentDate);
    }
    
}
 
void STDateWidget::enterEvent(QEvent* event)
{
    isMoveIn = true;
    for (int i = 0; i < neighbors.count(); i++) {
        neighbors[i]->HaveGoodNeighbor(directions[i]);
    }
    update();
}
 
void STDateWidget::leaveEvent(QEvent* event)
{
    isMoveIn = false;
    for (int i = 0; i < neighbors.count(); i++) {
        neighbors[i]->DeleteGoodNgithbor(directions[i]);
    }
    update();
}
 
void STDateWidget::paintEvent(QPaintEvent* event)
{
    //return;
    QPainter painter(this);
    painter.save();
    int xx = 1;
    int yy = 1;
    int ww = this->geometry().width();
    int hh = this->geometry().height();
    if (isMoveIn) {
        QPen pen;
        pen.setBrush(QColor(0, 200, 250, 200));
        pen.setWidth(2);
        painter.setPen(pen);
        painter.drawRect(xx +2 ,yy +2,ww-4,hh-4);
    }
    else {
        if (isHasGoodNeighbor)
        {
            QPen pen;
            pen.setBrush(QColor(255, 255, 255, 0));
            pen.setWidth(0);
            painter.setPen(pen);
            QLinearGradient line_left_top2bottom(xx + 2, yy + 2, xx + 2, yy + hh - 6);//左。
            line_left_top2bottom.setColorAt(0.0, Qt::white);
            line_left_top2bottom.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_left_bottom2top( xx + 2, yy + hh - 6, xx + 2, yy + 2);//左。
            line_left_bottom2top.setColorAt(0.0, Qt::white);
            line_left_bottom2top.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_right_top2bottpm(xx + ww - 6, yy + 2, xx + ww - 6, yy + hh - 6);
            line_right_top2bottpm.setColorAt(0.0, Qt::white);
            line_right_top2bottpm.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_right_bottom2top(xx + ww - 6, yy + hh - 6, xx + ww - 6, yy + 2);
            line_right_bottom2top.setColorAt(0.0, Qt::white);
            line_right_bottom2top.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_top_left2right(xx + 2, yy + 2, xx + ww - 6, yy + 2);
            line_top_left2right.setColorAt(0.0, Qt::white);
            line_top_left2right.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_top_right2left(xx + ww - 6, yy + 2, xx + 2, yy + 2);
            line_top_right2left.setColorAt(0.0, Qt::white);
            line_top_right2left.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_bottom_left2right(xx + 2, yy + hh - 6, xx + ww - 6, yy + hh - 6);
            line_bottom_left2right.setColorAt(0.0, Qt::white);
            line_bottom_left2right.setColorAt(1.0, QColor(0, 200, 250, 100));
            QLinearGradient line_bottom_right2left(xx + ww - 6, yy + hh - 6, xx + 2, yy + hh - 6);
            line_bottom_right2left.setColorAt(0.0, Qt::white);
            line_bottom_right2left.setColorAt(1.0, QColor(0, 200, 250, 100));
            QRect rectTop(xx + 2, yy + 2, ww - 6, 2);
            QRect rectBottom(xx + 2, yy + hh - 4, ww - 4, 2);
            QRect rectLeft(xx + 2, yy + 2, 2, hh - 6);
            QRect rectRight(xx + ww - 4, yy + 2, 2, hh - 6);
            switch (direction)
            {
            case STDateWidget::DIR_TOP:
                painter.setBrush(QColor(0, 200, 250, 100));
                painter.drawRect(rectBottom);
                painter.setBrush(line_left_top2bottom);
                painter.drawRect(rectLeft);
                painter.setBrush(line_right_top2bottpm);
                painter.drawRect(rectRight);               
                break;
            case STDateWidget::DIR_BOTTOM:
                painter.setBrush(QColor(0, 200, 250, 100));
                painter.drawRect(rectTop);
                painter.setBrush(line_left_bottom2top);
                painter.drawRect(rectLeft);
                painter.setBrush(line_right_bottom2top);
                painter.drawRect(rectRight);
                break;
            case STDateWidget::DIR_LEFT:
                painter.setBrush(QColor(0, 200, 250, 100));
                painter.drawRect(rectRight);
                painter.setBrush(line_top_left2right);
                painter.drawRect(rectTop);
                painter.setBrush(line_bottom_left2right);
                painter.drawRect(rectBottom);
                break;
            case STDateWidget::DIR_RIGHT:
                painter.setBrush(QColor(0, 200, 250, 100));
                painter.drawRect(rectLeft);
                painter.setBrush(line_top_right2left);
                painter.drawRect(rectTop);
                painter.setBrush(line_bottom_right2left);
                painter.drawRect(rectBottom);
                break;
            default:
                break;
            }
        }
        /*   QPen pen;
           pen.setBrush(QColor(255, 255, 255, 255));
           pen.setWidth(2);
           painter.setPen(pen);
           painter.drawRect(this->geometry());*/
    }
    painter.restore();
    if (canSelect) {
        QPen pen2;
        pen2.setBrush(QColor(0, 0, 0));
        painter.setPen(pen2);
        painter.drawText(ww/2 - 10, hh/2, QString::number(currentDate.day()));
    }
    else {
        QPen pen2;
        pen2.setBrush(QColor(200, 200, 200));
        painter.setPen(pen2);
        painter.drawText(ww/2- 10, hh/2, QString::number(currentDate.day()));
    }
    //painter
}

STCalandarWidget.h

#pragma once
#include <qdialog.h>
#include "STDateWidget.h"
#include <QLabel>
#include <QPushButton>
class STCalandarWidget :
    public QDialog
{
    Q_OBJECT
public:
    STCalandarWidget(QWidget* parrent = nullptr);
    ~STCalandarWidget();
    void SetCurrentDate(int year, int month, int day);
    QDate GetCurrentDate();
private:
    void FillCalandar();
    void initLabels();
    void initCalandar();
    void init();
    QString getFormatMonth();
private:
    QLabel *weeklabels[7];
    STDateWidget *datewidgets[42];
    QPushButton *lastYearButton;
    QPushButton *lastMonthButton;
    QPushButton *nextMonthButton;
    QPushButton *nextYearButton;
    QDate currentDate;
    QLabel *cdlabel;
public slots:
    void HaveDateSelect(QDate date);
    void JumpLastYear();
    void JumpLastMonth();
    void JumpNextMonth();
    void JumpNextYear();
 
signals:
    void DateSelectSignal(QDate date);
};

STCalandarWidget.cpp:

#include "STCalandarWidget.h"
#include <QDebug>
STCalandarWidget::STCalandarWidget(QWidget* parrent /* = nullptr */) :QDialog(parrent) {
    this->setStyleSheet(QString::fromLocal8Bit("font:15px 等線; background-color:rgb(250,250,250)"));
    this->setMinimumSize(580, 450);
    this->setMaximumSize(580, 450);
    Qt::WindowFlags flags = Qt::Dialog;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);
    init();
}
 
STCalandarWidget::~STCalandarWidget()
{
}
 
void STCalandarWidget::SetCurrentDate(int year, int month, int day)
{
    currentDate.setDate(year, month, day);
}
 
QDate STCalandarWidget::GetCurrentDate()
{
    return currentDate;
}
 
void STCalandarWidget::FillCalandar()
{
    QDate firstDay;
    firstDay.setDate(currentDate.year(), currentDate.month(), 1);
    int firstnum = firstDay.dayOfWeek();
    qDebug() << firstnum;
    QDate firstDayOfMonth = firstDay.addDays(-(firstnum - 1));
    for (int i = 0; i < 42; i++) {
        if (i < firstnum-1 || (firstDayOfMonth.month() != currentDate.month())) {
            datewidgets[i]->SetDate(firstDayOfMonth.year(), firstDayOfMonth.month(), firstDayOfMonth.day(), false);
        }
        else {
            datewidgets[i]->SetDate(firstDayOfMonth.year(), firstDayOfMonth.month(), firstDayOfMonth.day(), true);
        }
        firstDayOfMonth = firstDayOfMonth.addDays(1);
    }
}
 
void STCalandarWidget::initLabels()
{
    for (int i = 0; i < 7; i++) {
        weeklabels[i] = new QLabel(this);
        weeklabels[i]->setGeometry(35 + i*80, 50, 80, 40);
    }
    weeklabels[0]->setText("Mon");
    weeklabels[1]->setText("Tue");
    weeklabels[2]->setText("Wed");
    weeklabels[3]->setText("Thu");
    weeklabels[4]->setText("Fri");
    weeklabels[5]->setText("Sat");
    weeklabels[6]->setText("Sun");
}
 
void STCalandarWidget::initCalandar()
{
    for (int i = 0; i < 42; i++) {
        datewidgets[i] = new STDateWidget(this);
        datewidgets[i]->setGeometry(10 + i % 7 * 80, 80 + i / 7 * 60, 80, 60);
        connect(datewidgets[i], SIGNAL(updateCurrentDate(QDate)), this, SLOT(HaveDateSelect(QDate)));
    }
    for (int i = 0; i < 42; i++) {
        if (i / 7 == 0 ) {//第一排
            if (i % 7 == 0) {//第一個(gè)
                datewidgets[i]->AddNeighbor(datewidgets[i + 1], STDateWidget::DIR_RIGHT);
            }
            else if (i % 7 == 6) {//最后一個(gè)
                datewidgets[i]->AddNeighbor(datewidgets[i - 1], STDateWidget::DIR_LEFT);
            }
            else {
                datewidgets[i]->AddNeighbor(datewidgets[i + 1], STDateWidget::DIR_RIGHT);
                datewidgets[i]->AddNeighbor(datewidgets[i - 1], STDateWidget::DIR_LEFT);
            }
            datewidgets[i]->AddNeighbor(datewidgets[i + 7], STDateWidget::DIR_BOTTOM);
        }
        else if (i / 7 == 5) {//最后一排
            if (i % 7 == 0) {//第一個(gè)
                datewidgets[i]->AddNeighbor(datewidgets[i + 1], STDateWidget::DIR_RIGHT);
            }
            else if (i % 7 == 6) {//最后一個(gè)
                datewidgets[i]->AddNeighbor(datewidgets[i - 1], STDateWidget::DIR_LEFT);
            }
            else {
                datewidgets[i]->AddNeighbor(datewidgets[i + 1], STDateWidget::DIR_RIGHT);
                datewidgets[i]->AddNeighbor(datewidgets[i - 1], STDateWidget::DIR_LEFT);
            }
            datewidgets[i]->AddNeighbor(datewidgets[i - 7], STDateWidget::DIR_TOP);
        }
        else
        {
            if (i % 7 == 0) {//第一個(gè)
                datewidgets[i]->AddNeighbor(datewidgets[i + 1], STDateWidget::DIR_RIGHT);
            }
            else if (i % 7 == 6) {//最后一個(gè)
                datewidgets[i]->AddNeighbor(datewidgets[i - 1], STDateWidget::DIR_LEFT);
            }
            else {
                datewidgets[i]->AddNeighbor(datewidgets[i + 1], STDateWidget::DIR_RIGHT);
                datewidgets[i]->AddNeighbor(datewidgets[i - 1], STDateWidget::DIR_LEFT);
            }
            datewidgets[i]->AddNeighbor(datewidgets[i - 7], STDateWidget::DIR_TOP);
            datewidgets[i]->AddNeighbor(datewidgets[i + 7], STDateWidget::DIR_BOTTOM);
        }
    }
    FillCalandar();
}
 
void STCalandarWidget::init()
{
    currentDate = QDate::currentDate();
    lastYearButton = new QPushButton(this);
    lastYearButton->setGeometry(10, 10, 100, 30);
    lastYearButton->setText("<<");
 
    lastMonthButton = new QPushButton(this);
    lastMonthButton->setGeometry(120, 10, 100, 30);
    lastMonthButton->setText("<");
 
    cdlabel = new QLabel(this);
    cdlabel->setGeometry(255, 10, 100, 40);
    cdlabel->setText(getFormatMonth());
 
    nextMonthButton = new QPushButton(this);
    nextMonthButton->setGeometry(360, 10, 100, 30);
    nextMonthButton->setText(">");
 
    nextYearButton = new QPushButton(this);
    nextYearButton->setGeometry(470, 10, 100, 30);
    nextYearButton->setText(">>");
 
    connect(lastYearButton, SIGNAL(clicked()), this, SLOT(JumpLastYear()));
    connect(lastMonthButton, SIGNAL(clicked()), this, SLOT(JumpLastMonth()));
    connect(nextMonthButton, SIGNAL(clicked()), this, SLOT(JumpNextMonth()));
    connect(nextYearButton, SIGNAL(clicked()), this, SLOT(JumpNextYear()));
    initLabels();
    initCalandar();
}
QString STCalandarWidget::getFormatMonth()
{
    QString ans = "";
    ans += QString::number(currentDate.year());
    ans += QString::fromLocal8Bit("年");
    ans += QString::number(currentDate.month());
    ans += QString::fromLocal8Bit("月");
    return ans;
}
void STCalandarWidget::HaveDateSelect(QDate date)
{
    qDebug() << date;
    emit DateSelectSignal(date);
}
void STCalandarWidget::JumpLastYear()
{
    currentDate = currentDate.addYears(-1);
    FillCalandar();
    cdlabel->setText(getFormatMonth());
}
 
void STCalandarWidget::JumpLastMonth()
{
    currentDate = currentDate.addMonths(-1);
    FillCalandar();
    cdlabel->setText(getFormatMonth());
}
 
void STCalandarWidget::JumpNextMonth()
{
    currentDate = currentDate.addMonths(1);
    FillCalandar();
    cdlabel->setText(getFormatMonth());
}
 
void STCalandarWidget::JumpNextYear()
{
    currentDate = currentDate.addYears(1);
    FillCalandar();
    cdlabel->setText(getFormatMonth());
}

到此這篇關(guān)于利用Qt制作簡(jiǎn)單的日期選擇界面的文章就介紹到這了,更多相關(guān)Qt日期選擇界面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c++容器list、vector、map、set區(qū)別與用法詳解

    c++容器list、vector、map、set區(qū)別與用法詳解

    這篇文章主要介紹了c++容器list、vector、map、set區(qū)別與用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C++中輸出十六進(jìn)制形式的字符串

    C++中輸出十六進(jìn)制形式的字符串

    這篇文章主要給大家介紹了C++中輸出十六進(jìn)制形式的字符串,文中給出了詳細(xì)的介紹,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2016-12-12
  • 詳解C語(yǔ)言之文件操作下)

    詳解C語(yǔ)言之文件操作下)

    這篇文章主要介紹了關(guān)于C語(yǔ)言文件操作方法的相關(guān)資料,小編覺(jué)得這篇文章寫的還不錯(cuò),需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • C語(yǔ)言中free函數(shù)的使用詳解

    C語(yǔ)言中free函數(shù)的使用詳解

    free函數(shù)是釋放之前某一次malloc函數(shù)申請(qǐng)的空間,而且只是釋放空間,并不改變指針的值。下面我們就來(lái)詳細(xì)探討下
    2017-05-05
  • 詳解C語(yǔ)言之預(yù)處理(下)

    詳解C語(yǔ)言之預(yù)處理(下)

    這篇文章主要介紹了C語(yǔ)言程序的預(yù)處理,小編覺(jué)得這篇文章寫的還不錯(cuò),需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • C語(yǔ)言實(shí)現(xiàn)查看進(jìn)程是否存在的方法示例

    C語(yǔ)言實(shí)現(xiàn)查看進(jìn)程是否存在的方法示例

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)查看進(jìn)程是否存在的方法,涉及C語(yǔ)言針對(duì)進(jìn)程操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • 掌握C++:揭秘寫時(shí)拷貝與淺深拷貝之間的關(guān)系

    掌握C++:揭秘寫時(shí)拷貝與淺深拷貝之間的關(guān)系

    探索C++的奧秘,本指南將揭秘寫時(shí)拷貝與淺深拷貝之間的微妙關(guān)系,摸索這些復(fù)雜概念背后的邏輯,讓你的編程技能瞬間提升,來(lái)吧,讓我們一起進(jìn)入這個(gè)引人入勝的C++世界!
    2024-01-01
  • C語(yǔ)言實(shí)現(xiàn)圖的鄰接矩陣存儲(chǔ)操作

    C語(yǔ)言實(shí)現(xiàn)圖的鄰接矩陣存儲(chǔ)操作

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)圖的鄰接矩陣存儲(chǔ)操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 一文弄懂C語(yǔ)言EOF

    一文弄懂C語(yǔ)言EOF

    在 C語(yǔ)言中,EOF 是一個(gè)宏定義,EOF 常常用于文件的輸入輸出中,當(dāng)讀取到文件結(jié)束時(shí),會(huì)返回 EOF,本文就詳細(xì)的介紹一下具體使用方法,感興趣的可以一起來(lái)了解一下
    2023-05-05
  • 一篇文章帶你了解c++運(yùn)算符重載

    一篇文章帶你了解c++運(yùn)算符重載

    下面小編就為大家?guī)?lái)一篇深入理解C++運(yùn)算符重載。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-08-08

最新評(píng)論