English 中文(简体)
在项目代表(QStyledItemDelegate)上,QLabel isnt transparency
原标题:QLabel isnt transparent when rendered on item delegate (QStyledItemDelegate)

I have strange problem I have item delegate inherit from QStyledItemDelegate the background color is gradient color that looks like this:

void ItemDelegate::paintActiveOverlay( QPainter* painter,
                                       qreal x,
                                       qreal y,
                                       qreal w,
                                       qreal h ) const 
{
    QPalette palette;
    QColor highlightColor = palette.color(QPalette::Highlight);
    QColor backgroundColor = palette.color(QPalette::Base);
    const float animation = 0.25;
    const int gradientRange = 16;

    QColor color2 = QColor::fromHsv(
        highlightColor.hue(),
        (int) (backgroundColor.saturation() * (1.0f - animation) + 
        highlightColor.saturation() * animation),
        (int) (backgroundColor.value() * (1.0f - animation) + 
        highlightColor.value() * animation) );

    QColor color1 = QColor::fromHsv(
        color2.hue(),
        qMax(color2.saturation() - gradientRange, 0),
        qMin(color2.value() + gradientRange, 255) );

    QRect rect( (int)x, (int)y, (int)w, (int)h);

    painter->save();
    painter->setPen(Qt::NoPen);

    QLinearGradient linearGradient(0, 0, 0, rect.height());
    linearGradient.setColorAt(0.0, color1);
    linearGradient.setColorAt(1.0, color2);

    painter->setBrush(linearGradient);
    painter->drawRect(rect);
    painter->restore();
}

It s called from the paint method also in the ItemDelegate constructor I have set QLabel like this:

 QRect rect(40, 30, 401, 31);
 Qt::TextInteractionFlags flags =     
      Qt::LinksAccessibleByKeyboard | 
      Qt::LinksAccessibleByMouse    |
      Qt::TextBrowserInteraction    |
      Qt::TextSelectableByKeyboard  |
      Qt::TextSelectableByMouse;
 Qt::TextFormat txtFormat = Qt::PlainText;

 pTextEdit_title = new QLabel();
 pTextEdit_title->setTextFormat(txtFormat);
 pTextEdit_title->setTextInteractionFlags(flags);
 pTextEdit_title->setGeometry(rect);

以及<代码>的油漆方法 项目Delegate i set the QLabel to make such as:

  pTextEdit_title->setText(Title);
  QRect TextEditRect(option.rect.x()+THUMB_WIDTH+THUMB_WIDTH+PADDING, option.rect.y() ,
  pTextEdit_title->width(), pTextEdit_title->height());
  QPixmap pixmap(pTextEdit_title->size());
  pTextEdit_title->render(&pixmap);
  painter->drawPixmap(TextEditRect,pixmap);

it render the QLabel file , but the problem is that it has gray background and doesnt act as transparent background , my question is how to set the QLabel background to be transparent?
also why the TextInteractionFlags i set bean ignored i can t do any thing to the text.

问题回答

有色透明度取决于alpha 输送:

The alpha channel of a color specifies the transparency effect, 0 represents a fully transparent color, while 255 represents a fully opaque color.

By default it is 255 so you should specify the desired value as a fourth argument at the fromHsv functions you are using.

也可使用setAlpha

在提供植被时,总是会发现植被背景。 有一种特别的旗帜,可以消除植被背景。

qwidget api的手册对此作了解释。

我也遇到了同样的问题。

最终通过确定属性<代码>开展工作。 Qt:WA_TranslucentBackground on http://www.wdget->render()





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签