STM32MP135F-DK测评 | 网络测评之用QT显示网络天气与基于QT的智能家居
▲ 点击上方 关注 STM32
来 源 :EEWO RLD论坛网友 qiao--- 版权归原作者 所有
//Json数据类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots: //槽函数
void slot_http(QNetworkReply* reply);//网络连接
void slot_pushButton_consult();//查询按钮
private:
Ui::Widget *ui;
QNetworkAccessManager* http;
};
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
http = new QNetworkAccessManager;
http->get(QNetworkRequest(QUrl(QString("http://query.asilu.com/weather/baidu/?city=%1").arg(ui->comboBox_city->currentText()))));//连接天气网站
connect(http,SIGNAL(finished(QNetworkReply*)),this,SLOT(slot_http(QNetworkReply *)));//调用槽函数
}
Widget::~Widget()
{
delete ui;
}
void Widget::slot_http(QNetworkReply *reply)
{
QByteArray Data = reply->readAll();
QJsonDocument JsonDocument = QJsonDocument::fromJson(Data);
QJsonObject JsonObject = JsonDocument.object();
QString City = JsonObject["city"].toString();
ui->textEdit->setText("");
for(int i = 0;i < 7;++i)
{
QString date = JsonObject["weather"].toArray()[i].toObject()["date"].toString();
QString weather = JsonObject["weather"].toArray()[i].toObject()["weather"].toString();
QString temp = JsonObject["weather"].toArray()[i].toObject()["temp"].toString();
QString wind = JsonObject["weather"].toArray()[i].toObject()["wind"].toString();
ui->textEdit->append(date+" "+weather+" "+temp+" "+wind);
}
}
void Widget::slot_pushButton_consult()
{
http->get(QNetworkRequest(QUrl(QString("http://query.asilu.com/weather/baidu/?city=%1").arg(ui->comboBox_city->currentText()))));
}
我们可以在电脑上先预览一下效果在移植到板子上。
#include "home.h"
#include "ui_home.h"
home::home(QWidget *parent)
: QWidget(parent)
, ui(new Ui::home)
{
ui->setupUi(this);
my_timer = new QTimer(this);
connect(my_timer,SIGNAL(timeout()),this,SLOT(slot_timeout()));
my_timer->start(1000);
/*led灯窗口*/
led = new device_led(this);
led->move(160,100);
/*舵机窗窗口*/
motor = new device_motor(this);
motor->move(310,100);
/* 设置样式*/
QPalette pal = this->palette();
pal.setBrush(QPalette::Background,QBrush(QPixmap(":/png/背景.png")));
setPalette(pal);
ui->label_pic->setStyleSheet("border-image:url(:/png/头像.png)");
ui->label_pic->setText("");
ui->frame_backgrand->setStyleSheet("#frame_backgrand{background-color:rgba(0,0,0,15%);}");
ui->label_time1->setStyleSheet("color:#FFFFFF");
ui->label_time2->setStyleSheet("color:#FFFFFF");
ui->label_line->setFixedWidth(3);
ui->label_line->setText("");
ui->label_line->setStyleSheet("background-color:#FFFFFF");
ui->label_eeword->setStyleSheet("color:#FFFFFF");
ui->label_title->setStyleSheet("color:rgba(255,255,255,200)");
ui->label_time1->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd ddd"));
ui->label_time2->setText(QTime::currentTime().toString("hh:mm"));
}
led灯的窗口和舵机的窗口我是从新设计的一个界面框架,如下图。
//led界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-关.png)");
ui->label_pic->setText("");
//舵机界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/百叶窗-叶子-开.png)");
ui->label_pic->setText("");
#include "device/device_led.h"
#include "ui_device_led.h"
device_led::device_led(QWidget *parent) :
QWidget(parent),
ui(new Ui::device_led)
{
ui->setupUi(this);
//滑动按钮
button = new SliderButton(this);
button->set_button_color(QColor(36,110,202),QColor(188,188,188),QColor(255,255,255));
button->set_button_size(12,20);
button->move(85,30);
connect(button,SIGNAL(signal_button_off()),this,SLOT(slot_slider_OFF()));
connect(button,SIGNAL(signal_button_on()),this,SLOT(slot_slider_ON()));
//界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-关.png)");
ui->label_pic->setText("");
//硬件初始化
fd =::open("/dev/led_red",O_RDWR);
if(fd<0){
printf("file open failed!\r\n");
return ;
}
::printf("打开成功");
}
device_led::~device_led()
{
delete ui;
}
void device_led::open_led(void){
databuf[0] = 1;
retvalue = ::write(fd,databuf,1);
if(retvalue < 0){
::close(fd);
return ;
}
}
void device_led::close_led(void){
databuf[0] = 0;
retvalue = ::write(fd,databuf,1);
if(retvalue < 0){
::close(fd);
return ;
}
}
void device_led::slot_slider_OFF(){
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-关.png)"); //替换灯图片
ui->label_onoff->setText("OFF");
close_led();
}
void device_led::slot_slider_ON(){
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-开.png)"); //替换灯图片
ui->label_onoff->setText("ON");
open_led();
}
void device_motor::motor_change(unsigned int angle){
char s[50] ;
::sprintf(s,"/home/root/streering %d",angle);
system(s);
}
device_motor::device_motor(QWidget *parent) :
QWidget(parent),
ui(new Ui::device_motor)
{
ui->setupUi(this);
//界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/百叶窗-叶子-开.png)");
ui->label_pic->setText("");
}
device_motor::~device_motor()
{
delete ui;
}
void device_motor::motor_change(unsigned int angle){
char s[50] ;
::sprintf(s,"/home/root/streering %d",angle);
system(s);
}
void device_motor::on_Slider_sliderReleased()
{
motor_change(ui->Slider->value());
}
© THE END