简体   繁体   English

Qt5:从BitBlt到QPainter :: drawImage

[英]Qt5: From BitBlt to QPainter::drawImage

Hello StackOverflow experts, 您好StackOverflow专家,

I am pretty noob at Qt and I am currently upgrading a profesionnal application from QT4 to QT5. 我在Qt相当菜鸟,目前正在将专业应用程序从QT4升级到QT5。

I have a problem with a bitblt that a need to upgrade to QPainter::drawImage. 我有一个bitblt问题,需要升级到QPainter :: drawImage。

The application is compiling and running but I only have a black image displayed whereas I should have green lines painted on this black image. 该应用程序正在编译并正在运行,但是我只显示了一个黑色图像,而我应该在该黑色图像上绘制绿线。 It is like the background is always at the front and nothing can be paint on top of it. 就像背景总是在最前面,在它上面什么也不能画。

Here is my previous code 这是我以前的代码

void View::paintEvent ( QPaintEvent * Event)
{   
    QRect   rcBounds=Event->rect();
    QPainter tmp(this);

    for (int lay=0;lay<(int)m_RectTable.size();lay++)
    {
        if (!m_RectTable[lay].isEmpty())
        {       
            if (lay != 0)
            {
                bitBlt(m_BitmapTable[lay], m_RectTable[lay].left(), m_RectTable[lay].top(), m_BitmapTable[lay - 1], m_RectTable[lay].left(), m_RectTable[lay].top(), m_RectTable[lay].width(), m_RectTable[lay].height(), QPainter::CompositionMode_SourceOver);
            }

            tmp.begin(m_BitmapTable[lay]);

            if (lay==0)
                tmp.fillRect(m_RectTable[lay], *m_pBrush);

            OnDraw(&tmp, lay);
            tmp.end();
            m_RectTable[lay].setRect(0, 0, -1, -1);
        }
    }
    bitBlt(this, rcBounds.left(), rcBounds.top(),m_BitmapTable[m_LayerNb-1],rcBounds.left(), rcBounds.top(),rcBounds.width(), rcBounds.height(), QPainter::CompositionMode_SourceOver); 
}

And I replaced: 我替换了:

bitBlt(m_BitmapTable[lay], m_RectTable[lay].left(), m_RectTable[lay].top(), m_BitmapTable[lay - 1], m_RectTable[lay].left(), m_RectTable[lay].top(), m_RectTable[lay].width(), m_RectTable[lay].height(), QPainter::CompositionMode_SourceOver);

and

bitBlt(this, rcBounds.left(), rcBounds.top(),m_BitmapTable[m_LayerNb-1],rcBounds.left(), rcBounds.top(),rcBounds.width(), rcBounds.height(), QPainter::CompositionMode_SourceOver); 

with: 与:

tmp.drawPixmap(m_RectTable[lay].left(), m_RectTable[lay].top(), *m_BitmapTable[lay - 1], m_RectTable[lay].left(), m_RectTable[lay].top(), m_RectTable[lay].width(), m_RectTable[lay].height());
    tmp.drawPixmap(rcBounds.left(), rcBounds.top(), *m_BitmapTable[m_LayerNb - 1], rcBounds.left(), rcBounds.top(), rcBounds.width(), rcBounds.height());

This paintEvent function is used to display the entire elements of my application such as Pop-up Window etc... (lay is for the different graphical layers). 此paintEvent函数用于显示我的应用程序的整个元素,例如弹出窗口等(布局用于不同的图形层)。

  • Is there something wrong with my way of upgrading bitblt ? 我升级bitblt的方式有问题吗?
  • Should I have a different architecture because bitblt and drawImage are not working the same way ? 因为bitblt和drawImage的工作方式不同,我是否应该使用其他体系结构?

If there is any missing information to have a better understanding of my problem feel free to ask me ! 如果缺少任何信息以更好地了解我的问题,请随时问我!

Thank you very much for your help ! 非常感谢您的帮助 !

I figured it out thanks to Frank Osterfeld. 感谢Frank Osterfeld,我弄清楚了。 The problem was coming from these beign() and end() methods. 问题来自这些beign()和end()方法。 There were probably not well placed. 可能位置不好。 I don't know exactly what they are used for but I removed those and it seems to work pretty well now. 我不确切知道它们的用途,但我删除了它们,现在看来效果很好。

So here is the new code: 因此,这是新代码:

void View::paintEvent ( QPaintEvent * Event)
{
    QRect   rcBounds=Event->rect();

    QPainter tmp(this);

    for (int lay=0;lay<(int)m_RectTable.size();lay++)
    {
        if (!m_RectTable[lay].isEmpty())
        {          
            if (lay != 0)
            {
                tmp.drawPixmap(m_RectTable[lay], *m_BitmapTable.at(lay - 1), m_RectTable[lay]);
            }

            if (lay==0)
                tmp.fillRect(m_RectTable[lay], *m_pBrush);

            OnDraw(&tmp, lay);
            m_RectTable[lay].setRect(0, 0, -1, -1);
        }
    }
    tmp.drawPixmap(rcBounds, *m_BitmapTable.at(m_LayerNb - 1), rcBounds);
}

I still have one question though, what are QPainter::begin and QPainter::end methods intended for ? 我仍然有一个问题,QPainter :: begin和QPainter :: end方法打算用于什么?

Thank you for your help 谢谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM