Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用paintEvent()绘制文本_Html_Qt_Qt4_Qstring_Qpainter - Fatal编程技术网

Html 使用paintEvent()绘制文本

Html 使用paintEvent()绘制文本,html,qt,qt4,qstring,qpainter,Html,Qt,Qt4,Qstring,Qpainter,我有一个程序需要使用paintEvent()绘制一些文本。 我试过这个: 1. QPainter painter; // painter for the canvas 2. painter.drawText(QPoint location, QString canvasText); 在哪里 3. QString canvasText = variablesText.append("< b >"); 4. variablesText.append((*fieldIter).secon

我有一个程序需要使用paintEvent()绘制一些文本。 我试过这个:

1. QPainter painter; // painter for the canvas
2. painter.drawText(QPoint location, QString canvasText);
在哪里

3. QString canvasText = variablesText.append("< b >");
4. variablesText.append((*fieldIter).second.c_str());
5. variablesText.append(":< /b > ");
6. variablesText.append(someValue);
7. variablesText.append("\n");
3。QString canvasText=variablesText.append(“”);
4.variablesText.append((*fieldIter.second.c_str());
5.variablesText.append(“:”);
6.variablesText.append(someValue);
7.variablesText.append(“\n”);
我需要格式化文本,canvasText应该如下所示:

一些粗体文本:一些不是粗体文本。(新行)

一些粗体文本2:一些不粗体文本2。(新行)这会持续一段时间

我遇到的问题是QString中不能包含HTML代码,因此文本显示如下:

< b>Some bold text:< /b> some not bold text. < b>Some bold text 2: < /b>
some not bold text2.\n
一些粗体文本:一些非粗体文本。 一些粗体文字2:
有些不是粗体文本2。\n
有没有一种方法可以使用draw in paintEvent以我需要的方式显示文本?使用QString(或QLabel或其他东西)

我用的是Qt4


感谢帮助=)

假设您正试图通过
QPainter
painter在
QRect
rect上打印str格式的HTML文本内容。按以下步骤进行:

QString str; // this string contains HTML formatted contents 
QTextDocument * document = new QTextDocument(this) ;
document->setHtml(str) ;

painter.translate( rect.left() , rect.top() ) ;
document->drawContents( &painter , QRect( 0 , 0,  rect.width() , rect.height() ) ;

假设您正试图通过
QPainter
painter在
QRect
rect上打印str格式的HTML文本内容。按以下步骤进行:

QString str; // this string contains HTML formatted contents 
QTextDocument * document = new QTextDocument(this) ;
document->setHtml(str) ;

painter.translate( rect.left() , rect.top() ) ;
document->drawContents( &painter , QRect( 0 , 0,  rect.width() , rect.height() ) ;
似乎非常适合您的目标,特别是
setHtml()
QTextDocument
可以格式化文本,并通过小部件的
QPainter
将结果绘制到屏幕上。下面是最简单的解决方案:

void Test::paintEvent(QPaintEvent *)
{
  QPainter painter(this);

  QTextDocument doc;
  doc.setHtml("<b>Title</b><p>Body Text</p>");

  doc.drawContents(&painter, rect());
}
void测试::paintEvent(QPaintEvent*)
{
油漆工(本);
QTextDocument文档;
doc.setHtml(“标题正文”;
文档drawContents(&painter,rect());
}
但是,请记住,这可能是非常低效的。只有在源html发生更改时,您才可能希望将
drawContents()
添加到缓存的
QPixmap

似乎非常适合您所追求的,特别是
setHtml()
QTextDocument
可以格式化文本,并通过小部件的
QPainter
将结果绘制到屏幕上。下面是最简单的解决方案:

void Test::paintEvent(QPaintEvent *)
{
  QPainter painter(this);

  QTextDocument doc;
  doc.setHtml("<b>Title</b><p>Body Text</p>");

  doc.drawContents(&painter, rect());
}
void测试::paintEvent(QPaintEvent*)
{
油漆工(本);
QTextDocument文档;
doc.setHtml(“标题正文”;
文档drawContents(&painter,rect());
}

但是,请记住,这可能是非常低效的。只有在源html发生更改时,您才可能希望将
drawContents()
添加到缓存的
QPixmap

谢谢@sjtaheri!这很好,但是我需要给它一个我已经存储在QList中的特定位置。我做了这个
document->drawContents(&painter,QRect(QPoint.x(),QPoint.y(),rect().width(),rect().height())但它没有显示任何内容。如果我使用诸如
document->drawContents(&painter,QRect(20,20,rect().width(),rect().height())之类的数字显示矩形,但已修剪。有什么想法吗?对于每个点,首先,使用painter::translate方法将其转换为点的x和y位置作为参数。然后通过调用document->drawContents(&painter,QRect(0,0,width,height))来绘制文档。宽度和高度应足够大,以使整个文本适合。最后使用QPainter::restore方法还原绘制者。对所有点重复此过程。再次感谢@sjtaheri=D我尝试了此操作,但它不会显示绘制者上的其余Qpixmap,即使我执行还原:S这是我的代码:
(inti=0;igetIcon());QTextDocument*document=new QTextDocument(this);document->setHtml(iconList[i]->getVariablesText());painter.translate(iconList[i]->getVariablesLocation();document->->drawContents(&painter,rect());painter.restore())
qpixmap是不可见的,如果我开始单击,我最终会抓取qpixmap,但当我释放它时,它会再次消失。=(谢谢@sjtaheri!这很好,但我需要给它一个存储在QList中的特定位置。我这样做了
document->drawContents(&painter,QRect(QPoint.x(),QPoint.y(),rect().width(),rect().height()));
但它什么也不显示。如果我使用诸如
文档->绘图内容之类的数字(&painter,QRect(20,20,rect().width(),rect().height())
它显示的是矩形,但经过修剪。有什么想法吗?对于每个点,首先,使用painter::translate方法,以点的x和y位置为参数进行转换。然后通过调用document->drawContents(&painter,QRect(0,0,width,height)绘制文档。宽度和高度应足够大,以使整个文本适合。最后使用QPainter::restore方法还原绘制者。对所有点重复此过程。再次感谢@sjtaheri=D我尝试了此操作,但它不会显示绘制者上的其余Qpixmap,即使我执行还原:S这是我的代码:
(inti=0;igetIcon());QTextDocument*document=new QTextDocument(this);document->setHtml(iconList[i]->getVariablesText());painter.translate(iconList[i]->getVariablesLocation();document->->drawContents(&painter,rect());painter.restore())
qpixmap是不可见的,如果我开始单击,我最终会抓取qpixmap,但当我释放它时,它会再次褪色。=(感谢@sjwarner,但是,我需要给它一个特定的位置,以便于绘制它,我这样做了
document->drawContents(&painter,QRect(QPoint.x(),QPoint.y(),rect().width(),rect().height())
但它没有显示任何内容。@sjtaheri似乎让你走上了正确的轨道,所以我将把你交给它;-)谢谢@sjwarner,但是,我需要给它一个具体的绘制位置。我做了这个
文档->绘图内容(&painter,QPoint.x(),QPoint.y(),rect().width(),rect());
但它没有显示任何内容。@sjtaheri似乎有g