I still remember that I had to save images to the database in the previous project, and Qt had to read and read the images in the database. after a long time, I checked a lot of information to solve the problem, so I hope this article will help you. Now let's talk about the implementation steps. 1. MySQL remembers that an image was saved to the database in the previous project, and Qt had to read and read the image in the database. after a long time, I checked a lot of information to solve the problem, so I hope this article will help you. Now let's talk about the implementation steps.
1. store images in MySQL
First, declare the field type as longblob when creating the table, as shown below:
create table `sfood`(`name` varchar(255) not null,`type` varchar(255) not null,`material` varchar(255) not null,`price` int(200) not null,`feature` varchar(255) not null,`image` longblob,primary key(`name`))ENGINE=innodb default charset=gb2312;
Image is my image field, declared as longblob type, indicating the image of food.
Insert data to the table:
Insert into sfood (name, type, material, price, feature, image) values ('raw cabbage ', 'Sichuan food', 'cabbage, raw water', 8, 'light ', LOAD_FILE ('G: \ images \ chuancai \ baicai.jpg '));
Here, LOAD_FILE ('G: \ images \ chuancai \ baicai.jpg ') is used to write images to the image field. here, an absolute path is used to indicate the position of your image. This is in windows. in Linux, change the directory interval //.
In this way, we have already written images to the database.
2. how to read images from the database in Qt? the following code uses the above table sfood as an example:
QString select = "select * from sfood";
query.exec(select);
if( query.next() )
{
QLabel *PicLabel = new QLabel();
QPixmap photo;
Photo. loadFromData (query. value (5 ). toByteArray (), "JPG"); // read the image as binary data from the database. The image format is JPG and then displayed in the QLabel.
PicLabel->setPixmap(photo);
PicLabel->setScaledContents(true);
}
3. write images to the database through Qt
Query.exe c ("select * from sfood where name = '" + nameEdit-> text () + "'"); // the code here is to add dishes, query whether the dish exists by name
if(query.next())
{
QMessageBox: information (this, tr ("warning"), tr ("this dish is already stored in the database "));
db.Close();
return;
}
query.prepare("insert into sfood(name,type,material,price,feature,image) values(?,?,?,?,?,?)");
query.addBindValue(nameEdit->text());
query.addBindValue(typeEdit->text());
query.addBindValue(materialEdit->toPlainText());
query.addBindValue(priceEdit->text());
query.addBindValue(featureEdit->text());
// The following code is to save the image to the database.
ImagePath. replace ("\", "/"); // Convert the path format. imagePath is the path of the image file. here I use the absolute path.
/* The imagePath method can be written as follows:
imagePath = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.jpg)"));
*/
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
pictureLabel->pixmap()->save(&buffer,"JPG");
QByteArray data;
QFile * file = new QFile (imagePath); // file is the binary data file name.
file->open(QIODevice::ReadOnly);
data = file->readAll();
file->close();
QVariant var(data);
query.addBindValue(var);
query.exec();
OK. The image has been written to the database through Qt.
I don't have any skills. I hope I can help the Cainiao that I need just the same. I also hope that some senior engineers will give me advice or have better methods.