C++学习(十四)
2019.02.17
QT入门(四)
事件(二)
隐藏事件:隐藏时调用
void hideEvent(QHideEvent *event);
show事件:显示时调用
void showEvent(QShowEvent *event);
Close事件
void closeEvent(QCloseEvent *event);
弹窗类的使用
1.加头文件
#include <QMessageBox> //弹窗
2.用法
普通弹窗
QMessageBox::about(this,"asd", "hello");
Qt弹窗
QMessageBox::aboutQt(this, "Qt");
错误弹窗(危险信息)
QMessageBox::critical(this, "asd", "cri");
信息弹窗
QMessageBox::information(this, "asd", "ddd");
警告弹窗
QMessageBox::warning(this, "asd", "ddd");
问题弹窗
QMessageBox::question(this, "abc","hello");
问题弹窗(可附加按钮,按钮之间相或)
QMessageBox::StandardButton ret = QMessageBox::question(this, "abc","hello",QMessageBox::Open | QMessageBox::Save |QMessageBox::Cancel | QMessageBox::Apply)
(鼠标)进入事件
void enterEvent(QEvent *event);
(鼠标)离开事件
void leaveEvent(QEvent *event);
自己写按钮类:
1.创建
2.继承QWidget
3.实现相应的功能(进入、离开事件)
mybutton.h
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QObject>
#include <QPushButton>
class MyButton : public QPushButton
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = 0);
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
signals:
public slots:
};
#endif // MYBUTTON_H
mybutton.cpp
#include "mybutton.h"
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
}
void MyButton::enterEvent(QEvent *event)
{
//设置按钮颜色字体
this->setStyleSheet("background-color: rgb(255, 0, 127);font: 24pt '黑体';");
}
void MyButton::leaveEvent(QEvent *event)
{
//设置按钮颜色字体
this->setStyleSheet("");
}
将自己的按钮在ui中也能使用的方法:
进入.ui文件 将一个PushButton 拖入窗口,右击“提升为”进行操作就可以了
二、Qt网络编程
1.客户端搭建(client)
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket> //TCP通信类
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
QTcpSocket *socket;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
socket = new QTcpSocket(this);
//成功连上服务器后会触发这个信号
connect(socket,&QTcpSocket::connected,
[this]()
{
qDebug() << "connect server";
});
//当有数据可读的时候,会触发这个信号
connect(socket, &QTcpSocket::readyRead,
[this]()
{
QByteArray data = socket->readAll();
QString s = QString("server: %1").arg(QString(data));
ui->servertext->append(s);
});
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QString ip = ui->IP->text();
uint16_t port = ui->port->text().toInt();
socket->connectToHost(ip, port);
}
void Widget::on_pushButton_2_clicked()
{
QString data = ui->mytext->toPlainText();
socket->write(data.toUtf8()); //发送
QString s = QString("me: %1").arg(data);
ui->servertext->append(s);
}
2.服务器搭建(server)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer> //服务器类
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected slots:
void handleClient();
private:
Ui::MainWindow *ui;
QTcpServer *server;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
#include <QTcpSocket>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
server = new QTcpServer(this);
server->listen(QHostAddress::Any,9999);
connect(server, &QTcpServer::newConnection,
[this]()
{
//信号不会排队,当相同事件触发多次时,信号只会有一个
while(server->hasPendingConnections())
{
//接收客户端请求,产生一个和客户端通信的套接字
QTcpSocket *s = server->nextPendingConnection();
connect(s, &QTcpSocket::readyRead, this, &MainWindow::handleClient);
}
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handleClient()
{
//信号的发出者
QTcpSocket *s = (QTcpSocket *)sender();
//信号不会排队,当相同事件触发多次时,信号只会有一个
//所以每次必须把缓存里的数据全部读完
while(s->bytesAvailable())
{
QByteArray data = s->readAll();
s->write(data);
}
}