• 4973阅读
  • 1回复

[提问]自定义标题栏在调用时设置背景色无效 [复制链接]

上一主题 下一主题
离线longhai915
 

只看楼主 倒序阅读 楼主  发表于: 2016-11-18
本人菜鸟一枚,正在学习QT。目前正在学习设计自定义标题栏,我希望通过代码设置自定标题栏的背景色,但是无效,请大家帮帮忙,看看怎么回事。
自定义的CustomWidget类,构造函数如下:
  1. CustomWidget::CustomWidget(QWidget *parent) :
  2.     QWidget(parent),
  3.     ui(new Ui::CustomWidget)
  4. {
  5.     //设置无边框
  6.     setWindowFlags(Qt::FramelessWindowHint);
  7.     //设置透明效果
  8.     //setAttribute(Qt::WA_TranslucentBackground);
  9.     setWindowOpacity(1);
  10.     ui->setupUi(this);
  11.     m_bPlayCloseAnimation = true;
  12.     closeAnimationState = notplay;
  13.     QPalette pal(palette());
  14.     pal.setColor(QPalette::Background, QColor(200, 200, 200, 255));
  15.     setAutoFillBackground(true);
  16.     setPalette(pal);
  17.     TitleBar *pTitleBar = new TitleBar(this);
  18.     pTitleBar->setDefault();
  19.     //installEventFilter必须在setWindowTitle、setWindowIcon之前调用,
  20.     //因为必须先安装事件过滤器,相应事件触发时,才会进入标题栏的eventFilter事件中
  21.     installEventFilter(pTitleBar);
  22.     setWindowTitle(tr("Custom Window"));
  23.     //setWindowIcon(QIcon(":/Images/logo.png"));
  24.     QVBoxLayout *pLayout = new QVBoxLayout(this);
  25.     pLayout->addWidget(pTitleBar);
  26.     pLayout->addStretch();
  27.     pLayout->setSpacing(0);
  28.     pLayout->setContentsMargins(0, 0, 0, 0);
  29.     setLayout(pLayout);
  30. }
自定义的TitleBar,通过titlebar的setDefault函数设置titlebar的背景色,具体代码如下:
  1. TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
  2. {
  3.     this->setFixedHeight(30);
  4.     this->setAutoFillBackground(true);
  5.     //图标
  6.     m_pIconLb = new QLabel(this);
  7.     //标题
  8.     m_pTitleLb = new QLabel(this);
  9.     //最小化、最大化、关闭按钮
  10.     m_pMinimizeBtn = new QToolButton(this);
  11.     m_pMaximizeBtn = new QToolButton(this);
  12.     m_pCloseBtn = new QToolButton(this);
  13.     //设置对象名称
  14.     m_pTitleLb->setObjectName("whiteLabel");
  15.     m_pMinimizeBtn->setObjectName("minimizeButton");
  16.     m_pMaximizeBtn->setObjectName("maximizeButton");
  17.     m_pCloseBtn->setObjectName("closeButton");
  18.     //初始化左键单击状态
  19.     m_ptPressed = pos();
  20.     m_bPressed = false;
  21.     //设置图标
  22.     m_pIconLb->setFixedSize(20, 20);
  23.     m_pIconLb->setScaledContents(true);
  24.     //设置标题
  25.     m_pTitleLb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  26.     //获取最小化、关闭按钮图标
  27.     QPixmap minPix  = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
  28.     QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
  29.     QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
  30.     //设置按钮图标
  31.     m_pMinimizeBtn->setIcon(minPix);
  32.     m_pMaximizeBtn->setIcon(maxPix);
  33.     m_pCloseBtn->setIcon(closePix);
  34.     //设置按钮大小
  35.     m_pMinimizeBtn->setFixedSize(22, 22);
  36.     m_pMaximizeBtn->setFixedSize(22, 22);
  37.     m_pCloseBtn->setFixedSize(22, 22);
  38.     //设置按钮提示信息
  39.     m_pMinimizeBtn->setToolTip(tr("Minimize"));
  40.     m_pMaximizeBtn->setToolTip(tr("Maximize"));
  41.     m_pCloseBtn->setToolTip(tr("Close"));
  42.     //设置按钮样式
  43. //    m_pMinimizeBtn->setStyleSheet("background-color:transparent;");
  44. //    m_pMaximizeBtn->setStyleSheet("background-color:transparent;");
  45. //    m_pCloseBtn->setStyleSheet("background-color:transparent;");
  46.     //布局
  47.     QHBoxLayout *pLayout = new QHBoxLayout(this);
  48.     pLayout->addWidget(m_pIconLb);
  49.     pLayout->addSpacing(5);
  50.     pLayout->addWidget(m_pTitleLb);
  51.     pLayout->addWidget(m_pMinimizeBtn);
  52.     pLayout->addWidget(m_pMaximizeBtn);
  53.     pLayout->addWidget(m_pCloseBtn);
  54.     pLayout->setSpacing(0);
  55.     pLayout->setContentsMargins(1, 0, 1, 0);
  56.     this->setLayout(pLayout);
  57.     //绑定信号槽
  58.     connect(m_pMinimizeBtn, &QToolButton::clicked, this, &TitleBar::onClicked);
  59.     connect(m_pMaximizeBtn, &QToolButton::clicked, this, &TitleBar::onClicked);
  60.     connect(m_pCloseBtn, &QToolButton::clicked, this, &TitleBar::onClicked);
  61. }
  1. /**
  2. * @brief TitleBar::setDefault
  3. * @brief default setting of font and color
  4. * @brief font:consola bold
  5. * @brief foreground:white
  6. * @brief background:100,100,100
  7. */
  8. void TitleBar::setDefault(){
  9.     QFont font;
  10.     font.setFamily("Consola");
  11.     font.setBold(true);
  12.     this->setTitleFont(font);
  13.     this->setTitleColor(QColor (255,255,255,255));
  14.     this->setTitleBkColor(QColor(100, 100, 100, 255));
  15. }
  1. void TitleBar::setTitleBkColor(const QColor &color)
  2. {
  3.     QPalette pal;
  4.     pal.setColor(QPalette::Background, color);
  5.     this->setPalette(pal);
  6. }
麻烦大家看一下,为什么titlebar的背景色设置了,但是显示没有效果?
运行效果如下:



离线longhai915

只看该作者 1楼 发表于: 2016-11-19
哈哈已经结果了
快速回复
限100 字节
 
上一个 下一个