Qt初学笔记-3

组件运用

1、坐标系统

2、绘图

QPixmap pix(300,300);
    pix.fill(Qt::white);

    QPainter painter(&pix);
    painter.drawEllipse(QPoint(150,150),100,100);
pix.save("E:/Qt/untitled1/a.png");
上述代码绘出来的图片将被保存在所在位置。
    QImage img(300,300,QImage::Format_RGB32);
    img.fill(Qt::white);
    QPainter painter(&img);
    painter.drawEllipse(QPoint(150,150),100,100);
img.save("E:/Qt/untitled1/b.png");
上述代码绘出来的图片将被保存在所在位置。

    QPicture pic;
    QPainter painter(&pic);
    painter.begin(&pic);
    painter.drawEllipse(QPoint(150,150),100,100);
    painter.end();
pic.save("E:/Qt/untitled1/c.png");
如果不开启录像begin,将会导致图片为空(没有图片被保存),(本图片不能被正常打开)。
void Widget::paintEvent(QPaintEvent *event)
{
    QPicture pic;
    pic.load("E:/Qt/untitled1/c.png");
    QPainter painter(this);
    painter.drawPicture(0,0,pic);
}
上述代码是重现Picture所录像的图片。

repainter()函数和update()函数手动触发绘图事件。

三态交互:
void CMouse::mousePressEvent(QMouseEvent *e)
{
    m_state=CMouse::Estate::Press;
    QPushButton::mousePressEvent(e);
    //update();
}

void CMouse::mouseReleaseEvent(QMouseEvent *e)
{
    m_state=CMouse::Estate::Normal;
    QPushButton::mouseReleaseEvent(e);
}

void CMouse::enterEvent(QEvent *event)
{
    m_state=CMouse::Estate::Enter;
    //QPushButton::enterEvent(event);
}

void CMouse::leaveEvent(QEvent *event)
{
    m_state=CMouse::Estate::Leave;
    QPushButton::leaveEvent(event);
}

painter.drawEllipse(0,0,this->width()-1,this->height()-1);//超出界限会被其它图像覆盖

3、文件

3、1文件基础
//开始读文件
//1.创建文件对象
QFile file(filepath);
//2.打开文件
file.open(QIODevice::ReadOnly);
//3.读取文件内容,存储到某个对象
QByteArray content=file.readAll();
//把信息转换成gbk编码
QTextCodec *codec=QTextCodec::codecForName("GBK");
QString str=codec->toUnicode(content);

//4.把读取的信息设置显示框
//ui->plainTextEdit->setPlainText(content);
ui->plainTextEdit->setPlainText(str);
//5.关闭文件
file.close();

4、通信(TCP/IP,UDP)

4、1 TCP
4、2UDP
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    udpsocket=new QUdpSocket(this);
    setWindowTitle("port:6666");

    quint16 port=6666;
    //绑定端口号,如果要接收信息就需要绑定端口号
    udpsocket->bind(port);

    connect(udpsocket,&QUdpSocket::readyRead,[=]()mutable{
        char buf[1024]="";
        QHostAddress host;
        quint64 len=udpsocket->readDatagram(buf,sizeof(buf),&host,&port);
        if(len>0)
        {
            QString str=QString("[%1,%2]%3").arg(host.toString()).arg(port).arg(buf);
            ui->textEdit->append(str);
        }
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    QString ip=ui->lineEdit->text();
    quint16 port=ui->lineEdit_2->text().toInt();
    QString str=ui->textEdit_2->toPlainText();

    udpsocket->writeDatagram(str.toUtf8(),QHostAddress(ip),port);
    ui->textEdit_2->clear();
}

void Widget::on_pushButton_2_clicked()
{
    this->close();
}

UDP在同一端口号和同一地址时,将不能发消息。

udpsocket->bind(port,QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);这个函数第一个参数表示运行其它的客户端连接到相同端口,第二个参数表示可以连接相同的地址和连接不上时重新连接。

显示类

1、1QStringList
它定义的变量str,str<<“Math”<<“Chinese”<<“English”;中间自动添加逗号(,)分隔符。

1、2QDebug

它可使用qDebug()函数打印,包含C风格打印和C++打印。

猜你喜欢

转载自blog.csdn.net/Mangkhut_/article/details/83034018