一、 绘图设备:
1. QPixmap: 针对屏幕进行优化,和平台相关(显卡之类),不能对图片进行修改。
2. QImage: 和平台无关,可以对图片进行修改,可以在线程中绘图。
3. QPicture: 保存绘图的状态(二进制文件)
1.1 QPixmap :
QPixmap pixmap(400,300);
QPainter p(&pixmap);
pixmap.fill(Qt::white);
p.drawPixmap(0,0,80,80,QPixmap("E:\\C++QT\\test\\Image\\AR.png"));
pixmap.save("E:\\C++QT\\test\\Image\\pp.png");
1.2 QImage: 可以对图片进行修改,可以在线程中绘图
QImage image(400,300,QImage::Format_ARGB32);
QPainter p;
p.begin(&image);
p.drawImage(0,0,QImage("E:\\C++QT\\test\\Image\\AR.png"));
p.end();
image.save("E:\\C++QT\\test\\Image\\rr.png");
3. QPicture: 保存绘图的状态(二进制文件)
QPicture picture;
QPainter p;
p.begin(&picture);
p.drawPixmap(0,0,80,80,QPixmap("E:\\C++QT\\test\\Image\\AR.png"));
p.drawLine(50,50,150,50);
p.end();
picture.save("E:\\C++QT\\test\\Image\\CC.png");
void Widget::paintEvent(QPaintEvent *event)
{
QPicture pic;
pic.load("E:\\C++QT\\test\\Image\\CC.png");
QPainter p(this);
p.drawPicture(0,0,pic);
}
二、QImage与Mat类型互相转化
QImage Widget::MatToQImage(const Mat cvImg)
{
QImage qImg;
if(cvImg.channels()==3)
{
cvtColor(cvImg,cvImg,CV_BGR2RGB);
qImg=QImage((const unsigned char*)(cvImg.data),
cvImg.cols,
cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
else if(cvImg.channels()==1)
{
qImg=QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_Indexed8);
QVector<QRgb> colorTable;
for(int k=0;k<256;k++)
{
colorTable.push_back(qRgb(k,k,k));
}
qImg.setColorTable(colorTable);
}
else
{
qImg = QImage((const unsigned char*)(cvImg.data),
cvImg.cols,
cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
return qImg;
}
Mat Widget::QImage2cvMat(QImage image)
{
cv::Mat mat;
qDebug() << image.format();
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}
2.1 使用方法示例:
void Widget::on_pushButton_clicked()
{
QString path=QFileDialog::getOpenFileName(this,tr("打开文件"),"../");
QImage image;
if(path.isEmpty()==false)
{
bool isOK=image.load(path);
if(isOK==false)
{
QMessageBox::information(this,
tr("warning"),
tr("打开图像失败!")
);
return;
}
image=image.convertToFormat(QImage::Format_ARGB32);
Mat img=QImage2cvMat(image);
Mat img_gray;
cvtColor(img,img_gray,CV_BGR2GRAY);
Mat img_BW;
threshold(img_gray,img_BW,0,255,CV_THRESH_OTSU);
QImage image_Q=MatToQImage(img_BW);
image_Q.scaled(ui->label->size(),Qt::KeepAspectRatio);
ui->label->setScaledContents(true);
ui->label->setPixmap(QPixmap::fromImage((image_Q)));
waitKey(0);
}
}
三、Tcp通信:
通讯结构图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7b50833b7cd91805b72d89913439c9e0.png)
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
tcpServer=NULL;
tcpSocket=NULL;
setWindowTitle("服务器端,port8888");
tcpServer=new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,8888);
connect(tcpServer,&QTcpServer::newConnection,
[=]()
{
tcpSocket =tcpServer->nextPendingConnection();
QString ip=tcpSocket->peerAddress().toString();
qint16 port=tcpSocket->peerPort();
QString temp=QString("[%1,%2]:成功连接").arg(ip).arg(port);
ui->read->setText(temp);
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
QByteArray array= tcpSocket->readAll();
ui->read->append(array);
}
);
}
);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_buttonSend_clicked()
{
if(NULL==tcpSocket)
{
return;
}
QString str=ui->write->toPlainText();
tcpSocket->write(str.toUtf8().data());
}
void Widget::on_buttonClose_clicked()
{
if(tcpSocket!=NULL)
{
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
tcpSocket=NULL;
}