qt实现日历和天气显示(QCalendarWidget)

完成展示效果:

 本项目主要有QCalendarWidget类和获取天气api

一、QCalendarWidget

关键代码:

    ui->mCalendarWidget->setHorizontalHeaderFormat(QCalendarWidget :: ShortDayNames);//星期一、二
    ui->mCalendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);//删除左侧周数
    ui->mCalendarWidget->setGridVisible(false);//删除网格
    ui->mCalendarWidget->setFirstDayOfWeek(Qt::Sunday);//首列是星期几
    //周六周末颜色设置
    QTextCharFormat f;// =ui->mCalendarWidget->weekdayTextFormat(Qt::Monday);//获取周一字体格式
    f.setForeground(QBrush(QColor("#ffffff")));//替换字体颜色
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Sunday,f);//设置周六日为新的字体格式
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Saturday,f);//六
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Monday,f);
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Tuesday,f);
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Wednesday,f);
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Thursday,f);
    ui->mCalendarWidget->setWeekdayTextFormat(Qt::Friday,f);
    ui->mCalendarWidget->setNavigationBarVisible(false);//隐藏导航栏
qss设置:   
QCalendarView#qt_calendar_calendarview {
  alternate-background-color:#000000;   
}
QAbstractItemView {
selection-color: #ffffff;
selection-background-color: rgb(255, 174, 0);
font: 20px;
}
二、获取天气Api

1、获取天气数据

void Weather::queryWeather()
{
    char quest_array[256]= "http://t.weather.sojson.com/api/weather/city/"; 
    QNetworkRequest quest;
    sprintf(quest_array,"%s%s",quest_array,cityName.toLatin1().data());
    quest.setUrl(QUrl(quest_array));
    qDebug()<<"quest_array:"<<quest_array;
    quest.setHeader(QNetworkRequest::UserAgentHeader,"RT-Thread ART");
    /*发送get网络请求*/
    manager->get(quest);
}

2、解析json

void Weather::replyFinished(QNetworkReply *reply)
{
    this->isGetData=false;
    allinfo = reply->readAll();

    QJsonParseError err;
    QJsonDocument json_recv = QJsonDocument::fromJson(allinfo.toUtf8(),&err);//解析json对象
    if(!json_recv.isNull())
    {
        QJsonObject object = json_recv.object();
        if(object.contains("cityInfo"))
        {
            QJsonValue dateValue = object.value("cityInfo");
            QJsonObject object_data = dateValue.toObject();
            cityName = object_data.value("city").toVariant().toString();
        }
        if(object.contains("data"))
        {
            QJsonValue dateValue = object.value("data");
            if(dateValue.isObject())
            {
                mWeatherInfo.shidu = dateValue.toObject().value("shidu").toString();
                mWeatherInfo.wendu = dateValue.toObject().value("wendu").toString();
                mWeatherInfo.quality = dateValue.toObject().value("quality").toString();

                QJsonValue value = dateValue.toObject().value("forecast");

                if(value.isArray())
                {
                    qDebug()<<"size:"<<value.toArray().size();
                    mWeatherInfo.weatherInfoDetailList.clear();
                    for(int i = 0; i < value.toArray().size(); i++)
                    {
                        QJsonObject object = value.toArray().at(i).toObject();
                        struct _WeatherInfoDetail tmpWeatherInfoDetail;
                        tmpWeatherInfoDetail.type = object.value("type").toString();
                        QString high = object.value("high").toString();
                        QString low = object.value("low").toString();
                        tmpWeatherInfoDetail.high = high.mid(low.length()-3,4);
                        tmpWeatherInfoDetail.low = low.mid(low.length()-3,4);
                        tmpWeatherInfoDetail.notice = object.value("notice").toString();
                        tmpWeatherInfoDetail.week = object.value("week").toString();
                        tmpWeatherInfoDetail.fx = object.value("fx").toString();
                        tmpWeatherInfoDetail.fl = object.value("fl").toString();
                        mWeatherInfo.weatherInfoDetailList.append(tmpWeatherInfoDetail);
                    }
                    this->isGetData=true;
                }
            }
        }

    }
}

完整代码:

https://download.csdn.net/download/Baikal_Lake/89235489

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值