帮朋友写的一个远程关机的小程序,参考一些网上大神的代码,加上自己的代码。
控制端:main.c
#include "shutdown.h"
#include <QApplication>
#include <QSound>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSound::play("music.wav");
shutdown w;
w.setFixedSize(280,260);
w.setWindowTitle(QObject::tr("关机程序"));
w.setWindowFlags(Qt::WindowCloseButtonHint);
w.setWindowOpacity(1);
w.setAutoFillBackground(true);
QPalette palette;
palette.setBrush(QPalette::WindowText, Qt::red);
palette.setBrush(QPalette::ButtonText, Qt::red);
palette.setBrush(QPalette::Button, Qt::red);
w.setPalette(palette);
w.show();
return a.exec();
}
shutdown.cpp
#include "shutdown.h"
#include "ui_shutdown.h"
#include <QDebug>
#include <QTimer>
#include <QTime>
#include <QDateTime>
#include <QLCDNumber>
#include <QMessageBox>
#include <QPalette>
#include <windows.h>
#include <QInputDialog>
#include <QByteArray>
#include <QDebug>
int shutdownTime = 0;
int second = -1;
shutdown::shutdown(QWidget *parent) :
QDialog(parent),
ui(new Ui::shutdown)
{
ui->setupUi(this);
setAutoFillBackground(true);
QPalette p = QPalette();
p.setColor(QPalette::WindowText , Qt::red); //设置某一个控件颜色的方法
ui->lcdNumber->setPalette(p);
QTimer *timer = new QTimer(this); //新建一个定时器
connect(timer,SIGNAL(timeout()),this,SLOT(showTime())); //信号与槽,时间到便触发
connect(timer,SIGNAL(timeout()),this,SLOT(showCountDownTime())); //倒计时的信号与槽
connect(ui->setShutDownbt,SIGNAL(clicked(bool)),this,SLOT(showCountDownTime())); //倒计时信号与槽
connect(timer,SIGNAL(timeout()),this,SIGNAL(showCountDownTime));
timer->start(1000); //一秒刷新一次
showTime(); //显示时间
showCountDownTime();
showColon=true; //显示分号
ui->shutDownTime->setText(tr(" 尚未设置")) ; //初始化关机时间lebel
sender = new QUdpSocket(this);
receiver = new QUdpSocket(this);
receiver->bind(QHostAddress::LocalHost, 6665);
connect(receiver, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
}
shutdown::~shutdown()
{
delete ui;
}
void shutdown::readPendingDatagrams()
{
qDebug()<<"111122";
while (receiver->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(receiver->pendingDatagramSize());
receiver->readDatagram(datagram.data(), datagram.size());
qDebug()<<datagram.data();
//数据接收在datagram里
/* readDatagram 函数原型
qint64 readDatagram(char *data,qint64 maxSize,QHostAddress *address=0,quint16 *port=0)
*/
}
}
void shutdown::showTime()//显示系统时间
{
QTime time