• 6007阅读
  • 4回复

关于自定义窗体,请大家帮忙,谢谢. [复制链接]

上一主题 下一主题
离线julianfox
 
只看楼主 正序阅读 楼主  发表于: 2010-12-28
最近在尝试自定义窗体,在网上看了一个例子,决定自己试试.效果基本出来了.但是titlebar的圆角后面还有一个背景的矩形尖角不知道如何才能去掉,希望大家帮忙,谢谢.
代码如下:
void customDlg::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event)

    QPainter painter(this);
    QStyle* style = this->style();
    QRect active_area = this->rect();

    qDebug() << active_area;

    int titlebar_height = 0;

    // Titlebar.
    QStyleOptionTitleBar t_opt;
    t_opt.initFrom(this);

    titlebar_height = style->pixelMetric(QStyle::PM_TitleBarHeight, &t_opt, this);

    qDebug() << titlebar_height;

    t_opt.rect = QRect(0, 0, this->width(), titlebar_height);
    t_opt.titleBarState = this->windowState();
    t_opt.text = t_opt.fontMetrics.elidedText(this->windowTitle(), Qt::ElideRight, t_opt.rect.width());
    style->drawComplexControl(QStyle::CC_TitleBar, &t_opt, &painter, this);
    style->drawItemText(&painter, t_opt.rect, Qt::AlignCenter, t_opt.palette, true, t_opt.text, QPalette::ToolTipText);

    // Background widget.
    active_area.setTopLeft(QPoint(0, titlebar_height));
    this->setContentsMargins(0, titlebar_height, 0, 0);

    QStyleOption w_opt;
    w_opt.initFrom(this);
    w_opt.rect = active_area;

    qDebug() << w_opt.rect;

    style->drawPrimitive(QStyle::PE_Widget, &w_opt, &painter, this);
}
ps:效果如附件图片所示
只看该作者 4楼 发表于: 2010-12-29
设置背景颜色为外面那个蓝色就行.

或者设置没有背景
background:none;
离线julianfox
只看该作者 3楼 发表于: 2010-12-29
其实这个可以通过setMask去掉,但是为什么会有那个边角我不是很理解,希望能有人指点一下.谢谢.
离线julianfox
只看该作者 2楼 发表于: 2010-12-29
感谢回复,我试了一下但是还是不行,效果如图:

离线roywillow

只看该作者 1楼 发表于: 2010-12-28
加上setAttribute(Qt::WA_TranslucentBackground, true);试试呢?
这个会让窗口背景有alpha通道,只要没有绘制和填充的地方或者绘制填充了alpha为0的颜色,alpha都是0,是穿透效果
专业维修核潜艇,回收二手航母、二手航天飞机,大修核反应堆,拆洗导弹发动机更换机油,无人侦察机手动挡改自动,航天飞机保养换三滤,飞碟外太空年检 ,各型号导弹加装迎宾踏板,高空作业擦洗卫星表面除尘、打蜡及抛光,东风全系列巡航导弹。并提供原子对撞机。量大从优,有正规发票。
快速回复
限100 字节
 
上一个 下一个