HTML格式有效
截取图片的字符串,修改height属性来达到放大缩小的效果(百度过是否有其他更方便的方式,貌似没发现。。
效果图:
两个槽: 1.5倍放大
void MainWindow::zoomInImage()
{
//获取文本编辑游标
QTextCursor cursor = centerWidget->textEdit->textCursor();
//判断游标是否有选中内容
if(cursor.hasSelection()){
//判断游标选中内容是否包含图片
if(cursor.selection().toHtml().contains("img src=")){
//获取选中内容的html语句
QString img_html = cursor.selection().toHtml();
//定义一个用于记录图片语句长度的变量
int str_size = 0;
//从语句开始,到该语句结束(结束条件是 />)
for(int i = img_html.indexOf("<img src=") + 10; i < img_html.length(); ++i){
++str_size;
if(img_html.mid(i, 2) == "/>"){
//减2是因为不想多拿到height=“xxx” /> 此处的右引号与空格
str_size -= 2;
break;
}
}
//截取出字符串
QString str_temp = img_html.mid(img_html.indexOf("<img src=") + 9, str_size);
//获取高度
QString temp_height;
for(int i = str_temp.indexOf("height=") + 8; i < str_temp.length(); ++i){
temp_height += str_temp[i];
}
//修改高度,放大1.5倍
int temp_height_int = temp_height.toInt() * 1.5;
//替换高度
str_temp.replace(temp_height, QString("%1").arg(temp_height_int));
//替换图片html语句
img_html.replace(img_html.mid(img_html.indexOf("<img src=") + 9, str_size), str_temp);
//清除选中的语句,重新插入语句
cursor.deleteChar();
centerWidget->textEdit->insertHtml(img_html);
}
}
}
1.5倍缩小
void MainWindow::zoomOutImage()
{
QTextCursor cursor = centerWidget->textEdit->textCursor();
if(cursor.hasSelection()){
if(cursor.selection().toHtml().contains("img src=")){
QString img_html = cursor.selection().toHtml();
int str_size = 0;
for(int i = img_html.indexOf("<img src=") + 10; i < img_html.length(); ++i){
++str_size;
if(img_html.mid(i, 2) == "/>"){
str_size -= 2;
break;
}
}
QString str_temp = img_html.mid(img_html.indexOf("<img src=") + 9, str_size);
QString temp_height;
for(int i = str_temp.indexOf("height=") + 8; i < str_temp.length(); ++i){
temp_height += str_temp[i];
}
int temp_height_int = temp_height.toInt() / 1.5;
str_temp.replace(temp_height, QString("%1").arg(temp_height_int));
img_html.replace(img_html.mid(img_html.indexOf("<img src=") + 9, str_size), str_temp);
cursor.deleteChar();
centerWidget->textEdit->insertHtml(img_html);
}
}
}