相关文章推荐
曾经爱过的西装  ·  Vim 7.3 on Ubuntu ...·  1 年前    · 
苦闷的水龙头  ·  Test-Json ...·  1 年前    · 


文章目录

1 问题重现

QPainter::begin: Painter already active问题解决方案_QPainter

2 原因

重新实现paintEvent函数时,如果使用​ ​QPainter painter(this);​ ​,再使用begin、end则会重复包含QPainter。官方给出的主意事项:

Note that most of the time, you can use one of the constructors instead of begin(), and that end() is automatically done at destruction.

Warning: A paint device can only be painted by one painter at a time.
void ShowImage::paintEvent(QPaintEvent *event)
{
if(ui->stackedWidget->currentIndex() == 0){
QPainter painter(this);
/painter.begin(this);//会出现QPainter::begin: Painter already active
painter.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(0, 28, this->width(), 28);
painter.drawLine(1006, 28, 1006, this->height());


painter.setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(60, 254, 1006, 254);//194
painter.drawLine(60, 452, 1006, 452);//392
painter.drawLine(60, 648, 1006, 648);//588
painter.drawLine(60, 844, 1006, 844);//

painter.end();
}else if(ui->stackedWidget->currentIndex() == 1){
QPainter painter(this);
painter.begin(this);
painter.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(0, 28, this->width(), 28);
painter.drawLine(1006, 28, 1006, this->height());//1030

painter.end();
}
}

3 解决方法

在使用​ ​QPainter painter(this)​ ​时,不使用begin和end

void ShowImage::paintEvent(QPaintEvent *event)
{
if(ui->stackedWidget->currentIndex() == 0){
QPainter painter(this);
// painter.begin(this);//会出现QPainter::begin: Painter already active
painter.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(0, 28, this->width(), 28);
painter.drawLine(1006, 28, 1006, this->height());


painter.setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(60, 254, 1006, 254);//194
painter.drawLine(60, 452, 1006, 452);//392
painter.drawLine(60, 648, 1006, 648);//588
painter.drawLine(60, 844, 1006, 844);//

//painter.end();
}else if(ui->stackedWidget->currentIndex() == 1){
QPainter painter(this);
//painter.begin(this);
painter.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap));
painter.drawLine(0, 28, this->width(), 28);
painter.drawLine(1006, 28, 1006, this->height());//1030

// painter.end();
}
}

QPainter::begin: Paint device returned engine == 0, type: 3 QPainter::end: Painter not active, abort

最近在开发软件的时候一直报这个错,影像中QPainter也是可以在paintEvent外面使用的,为什么会这样呢,于是经过一番调试才发现QImage在创建的时候size大小为0,所以才会报上面的错误,修正后就没错了,特此记录一下附QT在QImage上绘图的代码 QImage m_shade; m_shade = QImage(100,100, QImage::Format_ARGB32/*_Premultiplied*/); QPainter p(&m_shade);

匹配url: 匹配文件名: (\S+\.\w{3,4}) linux路径+文件名: \/([\w\.]+/?)*/([\S ]+\.\w{3,4}) 支持空格(1)排列组合 import itertools 排列mylist = list(itertools.permutations([1,2,3,4],3)) print(mylist) print

mysql SQL禁用索引 mysql如何避免索引失效

全值匹配:对索引中所有的列都指定具体值最左前缀法则:查询从索引的最左前列开始,并且不跳过索引中的列. - 是指查询条件包含了哪些索引,跟查询条件的列的位置没有关系. - 假如有ABC三个字段创建了组合索引,查询条件只有A和C,会有A的索引,不会走C的索引.范围查询右边的列不能使用索引--address索引失效了.其中name,status,address是符合索引 select * from tb