C++ Qt无框SwindowHint和WA_半透明背景字体渲染

C++ Qt无框SwindowHint和WA_半透明背景字体渲染,c++,qt,windows-7,C++,Qt,Windows 7,我正在编写一个带有自定义窗口外壳的应用程序。外壳具有圆角和透明度。以下是我如何执行此操作的示例代码: MyWindow::MyWindow (void) : QMainWindow (NULL, Qt::FramelessWindowHint) { setAttribute (Qt::WA_TranslucentBackground); setAttribute (Qt::WA_NoSystemBackground ); } 问题是,每当我将WA_半透明背景与无框架Swin

我正在编写一个带有自定义窗口外壳的应用程序。外壳具有圆角和透明度。以下是我如何执行此操作的示例代码:

MyWindow::MyWindow (void) : QMainWindow (NULL, Qt::FramelessWindowHint)
{
    setAttribute (Qt::WA_TranslucentBackground);
    setAttribute (Qt::WA_NoSystemBackground   );
}
问题是,每当我将WA_半透明背景与无框架SwindowHint一起使用时,字体渲染就会变得糟糕,请参见下图。我有一个通过全局css设置的自定义应用程序样式。我尝试使用其他字体,如Segoe UI,但该字体也会发生变化


关于为什么会发生这种情况以及我能做些什么来解决这个问题的任何想法。我用C++和Qt5.0.2

看起来好像我找到了一个解决方案。首先,如果您不想使用Qt::WA_半透明背景,可以使用QWidget::setMask获得圆角。下面是我提出的示例代码:

void MyWindow::setVisible (bool visible)
{
    // Call the default event
    QMainWindow::setVisible (visible);

    // Set a rounded mask (size() needs to be correct)
    QBitmap t (size());
    t.fill (Qt::color0);

    QPainter p (&t);
    p.setBrush (Qt::color1);
    p.drawRoundedRect (rect(), 5, 5);

    setMask (t);
}
为了透明,您必须使字体更倾向于抗锯齿。您可以将其放在应用程序的开头

    QFont font = QApplication::font();
    font.setStyleStrategy (QFont::PreferAntialias);
    QApplication::setFont (font);
虽然不完美,但它解决了我的问题