用qt做网易云音乐--个人中心实现

先上完成图
在这里插入图片描述

大体设计及位置,主要是按钮和label
在这里插入图片描述
我这里图标采用的是QLabel的pixmap

在这里插入图片描述
记住要勾选scaledContents,缩放内容到label大小,不会导致图标只显示一部分

这样设计完成后有一个问题,就是label会遮挡button的事件
我们要用setAttribute(Qt::WA_TransparentForMouseEvents)让label事件穿透

以下是窗体cpp文件(供大家参考)

#include "personalcenterwindow.h"
#include "ui_personalcenterwindow.h"
#include <loadqssfile.h>

PersonalCenterWindow::PersonalCenterWindow(int x, int y, QWidget *parent):
    QWidget(parent),
    ui(new Ui::PersonalCenterWindow)
{
    ui->setupUi(this);
    this->setGeometry(x,y,this->width(),this->height());
    this->setAttribute(Qt::WA_StyledBackground);
    ui->mcIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->mcTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->memberStateLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->rightArrowLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->gIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->gTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->grabCountLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->rightArrowLabel2->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->sIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->sTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->rightArrowLabel3->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->pisIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->pisTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->rightArrowLabel4->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->bsaIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->bsaTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->rightArrowLabel5->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->msIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->msTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->microblogLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->rightArrowLabel6->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->lIconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->lTextLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
}

PersonalCenterWindow::~PersonalCenterWindow()
{
    delete ui;
}

void PersonalCenterWindow::setMemberState(bool state)
{
    if(state)
    {
        ui->memberStateLabel->setText("已订购");
    }
    else
    {
        ui->memberStateLabel->setText("未订购");
    }
}

void PersonalCenterWindow::setGrade(int grade)
{
    ui->grabCountLabel->setText(QString::asprintf("Lv%d",grade));
}

void PersonalCenterWindow::setDynamicCount(int count)
{
    ui->dynamicCountLabel->setText(QString::number(count));
}

void PersonalCenterWindow::setAttentionCount(int count)
{
    ui->attentionCountLabel->setText(QString::number(count));
}

void PersonalCenterWindow::setFansCount(int count)
{
    ui->fansCountLabel->setText(QString::number(count));
}

void PersonalCenterWindow::leaveEvent(QEvent *event)
{
    this->hide();
    QWidget::leaveEvent(event);
}

窗体的代码比较简单,没什么复杂的地方
然后是主窗口,我写了一个init函数来初始化

void MainWindow::initPersonalCenterWindow()
{
    if(personalCenterWindow == NULL)
    {
        int x = ui->iconButton->x()-40;
        int y = ui->iconButton->y()+ui->personButton->height()+10;
        personalCenterWindow = new PersonalCenterWindow(x,y,this);
        personalCenterWindow->setMemberState(false);
        personalCenterWindow->setGrade(8);
        personalCenterWindow->setDynamicCount(44);
        personalCenterWindow->setAttentionCount(64);
        personalCenterWindow->setFansCount(9648);
    }
}

这里的窗体点击后应该是有事件产生,我这里没写,事件建议大家采用qt信号和槽的方式,把信号发给主窗口, 让主窗口处理

最后是样式表,也没有什么难点

/*personalcenterwindow的qss样式表*/
QWidget#PersonalCenterWindow
{
    background-color:rgb(255,255,255);
    border-width:1px;
    border-radius:15px
}

QLabel#dynamicCountLabel,#attentionCountLabel,#fansCountLabel
{
    font-weight:bold;
    font-size:18px;
    color:rgb(51,51,51);
}

QPushButton#dynamicButton,#attentionButton,#fansButton
{
    color:rgb(102,102,102);
}

QPushButton#signInButton
{
    image:url(:/images/personalcenterwindow/签到.png);

}

QPushButton:hover#signInButton
{
    image:url(:/images/personalcenterwindow/签到2.png);

}

QPushButton#memberCentralButton,#gradeButton,#shopButton,#personalInformationSetButton,#bindSocialAccountButton,#myServiceButton,#logoutButton
{
    background-color:rgb(255,255,255);
}

QPushButton:hover#memberCentralButton,:hover#gradeButton,:hover#shopButton,:hover#personalInformationSetButton,:hover#bindSocialAccountButton,:hover#myServiceButton,:hover#logoutButton
{
    background-color:rgb(246,246,247);
}

这里注意qss如果不生效,可能是没有加
this->setAttribute(Qt::WA_StyledBackground);这句

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值