|
本帖最后由 LongHai915 于 2016-11-18 13:21 编辑
本人菜鸟一枚,正在学习QT。目前正在学习设计自定义标题栏,我希望通过代码设置自定标题栏的背景色,但是无效,请大家帮帮忙,看看怎么回事。
自定义的CustomWidget类,构造函数如下:- CustomWidget::CustomWidget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::CustomWidget)
- {
- //设置无边框
- setWindowFlags(Qt::FramelessWindowHint);
- //设置透明效果
- //setAttribute(Qt::WA_TranslucentBackground);
- setWindowOpacity(1);
- ui->setupUi(this);
- m_bPlayCloseAnimation = true;
- closeAnimationState = notplay;
- QPalette pal(palette());
- pal.setColor(QPalette::Background, QColor(200, 200, 200, 255));
- setAutoFillBackground(true);
- setPalette(pal);
- TitleBar *pTitleBar = new TitleBar(this);
- pTitleBar->setDefault();
- //installEventFilter必须在setWindowTitle、setWindowIcon之前调用,
- //因为必须先安装事件过滤器,相应事件触发时,才会进入标题栏的eventFilter事件中
- installEventFilter(pTitleBar);
- setWindowTitle(tr("Custom Window"));
- //setWindowIcon(QIcon(":/Images/logo.png"));
- QVBoxLayout *pLayout = new QVBoxLayout(this);
- pLayout->addWidget(pTitleBar);
- pLayout->addStretch();
- pLayout->setSpacing(0);
- pLayout->setContentsMargins(0, 0, 0, 0);
- setLayout(pLayout);
- //this->setStyleSheet("border-radius:15px;");
- }
复制代码 自定义的TitleBar,通过titlebar的setDefault函数设置titlebar的背景色,具体代码如下:- TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
- {
- this->setFixedHeight(30);
- this->setAutoFillBackground(true);
- //图标
- m_pIconLb = new QLabel(this);
- //标题
- m_pTitleLb = new QLabel(this);
- //最小化、最大化、关闭按钮
- m_pMinimizeBtn = new QToolButton(this);
- m_pMaximizeBtn = new QToolButton(this);
- m_pCloseBtn = new QToolButton(this);
- //设置对象名称
- m_pTitleLb->setObjectName("whiteLabel");
- m_pMinimizeBtn->setObjectName("minimizeButton");
- m_pMaximizeBtn->setObjectName("maximizeButton");
- m_pCloseBtn->setObjectName("closeButton");
- //初始化左键单击状态
- m_ptPressed = pos();
- m_bPressed = false;
- //设置图标
- m_pIconLb->setFixedSize(20, 20);
- m_pIconLb->setScaledContents(true);
- //设置标题
- m_pTitleLb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- //获取最小化、关闭按钮图标
- QPixmap minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
- QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
- QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
- //设置按钮图标
- m_pMinimizeBtn->setIcon(minPix);
- m_pMaximizeBtn->setIcon(maxPix);
- m_pCloseBtn->setIcon(closePix);
- //设置按钮大小
- m_pMinimizeBtn->setFixedSize(22, 22);
- m_pMaximizeBtn->setFixedSize(22, 22);
- m_pCloseBtn->setFixedSize(22, 22);
- //设置按钮提示信息
- m_pMinimizeBtn->setToolTip(tr("Minimize"));
- m_pMaximizeBtn->setToolTip(tr("Maximize"));
- m_pCloseBtn->setToolTip(tr("Close"));
- //设置按钮样式
- // m_pMinimizeBtn->setStyleSheet("background-color:transparent;");
- // m_pMaximizeBtn->setStyleSheet("background-color:transparent;");
- // m_pCloseBtn->setStyleSheet("background-color:transparent;");
- //布局
- QHBoxLayout *pLayout = new QHBoxLayout(this);
- pLayout->addWidget(m_pIconLb);
- pLayout->addSpacing(5);
- pLayout->addWidget(m_pTitleLb);
- pLayout->addWidget(m_pMinimizeBtn);
- pLayout->addWidget(m_pMaximizeBtn);
- pLayout->addWidget(m_pCloseBtn);
- pLayout->setSpacing(0);
- pLayout->setContentsMargins(1, 0, 1, 0);
- this->setLayout(pLayout);
- //绑定信号槽
- connect(m_pMinimizeBtn, &QToolButton::clicked, this, &TitleBar::onClicked);
- connect(m_pMaximizeBtn, &QToolButton::clicked, this, &TitleBar::onClicked);
- connect(m_pCloseBtn, &QToolButton::clicked, this, &TitleBar::onClicked);
- }
复制代码- /**
- * @brief TitleBar::setDefault
- * @brief default setting of font and color
- * @brief font:consola bold
- * @brief foreground:white
- * @brief background:100,100,100
- */
- void TitleBar::setDefault(){
- QFont font;
- font.setFamily("Consola");
- font.setBold(true);
- this->setTitleFont(font);
- this->setTitleColor(QColor (255,255,255,255));
- this->setTitleBkColor(QColor(100, 100, 100, 255));
- }
复制代码- void TitleBar::setTitleBkColor(const QColor &color)
- {
- QPalette pal;
- pal.setColor(QPalette::Background, color);
- this->setPalette(pal);
- }
复制代码 麻烦大家看一下,为什么titlebar的背景色设置失败了?
运行效果如下:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|