userprofilewidget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "userprofilewidget.h"
  2. #include <QApplication>
  3. #include <QHBoxLayout>
  4. #include <QPainter>
  5. #include <QPainterPath>
  6. #include <QStyle>
  7. UserProfileWidget::UserProfileWidget(QWidget *parent)
  8. : QWidget(parent)
  9. , avatarLabel(nullptr)
  10. , usernameLabel(nullptr)
  11. , statusLabel(nullptr)
  12. , settingsButton(nullptr)
  13. , logoutButton(nullptr)
  14. {
  15. setupUI();
  16. // 设置默认值
  17. setUsername("用户名");
  18. setStatus("在线");
  19. // 设置默认头像
  20. QPixmap defaultAvatar = QPixmap(64, 64);
  21. defaultAvatar.fill(Qt::gray);
  22. setAvatar(defaultAvatar);
  23. setMaximumHeight(165);
  24. }
  25. UserProfileWidget::~UserProfileWidget() {}
  26. void UserProfileWidget::setupUI()
  27. {
  28. // 创建主布局
  29. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  30. mainLayout->setContentsMargins(10, 10, 10, 10);
  31. mainLayout->setSpacing(5);
  32. // 创建头像和用户名的水平布局
  33. QHBoxLayout *profileLayout = new QHBoxLayout();
  34. // 创建头像标签
  35. avatarLabel = new QLabel(this);
  36. avatarLabel->setAlignment(Qt::AlignCenter);
  37. avatarLabel->setMinimumSize(48, 48);
  38. avatarLabel->setMaximumSize(48, 48);
  39. avatarLabel->setScaledContents(true);
  40. avatarLabel->setCursor(Qt::PointingHandCursor);
  41. // 创建用户信息的垂直布局
  42. QVBoxLayout *userInfoLayout = new QVBoxLayout();
  43. // 创建用户名标签
  44. usernameLabel = new QLabel(this);
  45. QFont usernameFont = usernameLabel->font();
  46. usernameFont.setBold(true);
  47. usernameFont.setPointSize(usernameFont.pointSize() + 1);
  48. usernameLabel->setFont(usernameFont);
  49. // 创建状态标签
  50. statusLabel = new QLabel(this);
  51. // 将用户名和状态添加到用户信息布局
  52. userInfoLayout->addWidget(usernameLabel);
  53. userInfoLayout->addWidget(statusLabel);
  54. userInfoLayout->setContentsMargins(0, 0, 0, 0);
  55. userInfoLayout->setSpacing(2);
  56. // 将头像和用户信息添加到水平布局
  57. profileLayout->addWidget(avatarLabel);
  58. profileLayout->addLayout(userInfoLayout, 1);
  59. profileLayout->setSpacing(10);
  60. // 创建按钮布局
  61. QHBoxLayout *buttonLayout = new QHBoxLayout();
  62. buttonLayout->setSpacing(5);
  63. // 创建设置按钮
  64. settingsButton = new QPushButton(this);
  65. settingsButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileDialogDetailedView));
  66. settingsButton->setToolTip("设置");
  67. settingsButton->setFlat(true);
  68. // 创建登出按钮
  69. logoutButton = new QPushButton(this);
  70. logoutButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton));
  71. logoutButton->setToolTip("退出登录");
  72. logoutButton->setFlat(true);
  73. // 添加按钮到布局
  74. buttonLayout->addWidget(settingsButton);
  75. buttonLayout->addWidget(logoutButton);
  76. // 添加所有组件到主布局
  77. mainLayout->addLayout(profileLayout);
  78. mainLayout->addLayout(buttonLayout);
  79. mainLayout->addStretch();
  80. // // 设置样式
  81. // setStyleSheet("QLabel#avatarLabel { border-radius: 24px; border: 1px solid #cccccc; }"
  82. // "QLabel#usernameLabel { color: #333333; }"
  83. // "QLabel#statusLabel { color: #666666; }");
  84. // 连接信号和槽
  85. connect(avatarLabel, &QLabel::linkActivated, this, &UserProfileWidget::profileClicked);
  86. connect(settingsButton, &QPushButton::clicked, this, &UserProfileWidget::settingsClicked);
  87. connect(logoutButton, &QPushButton::clicked, this, &UserProfileWidget::logoutClicked);
  88. }
  89. void UserProfileWidget::setUsername(const QString &username)
  90. {
  91. usernameLabel->setText(username);
  92. }
  93. void UserProfileWidget::setAvatar(const QPixmap &avatar)
  94. {
  95. // 创建圆形头像
  96. QPixmap roundedAvatar(avatar.size());
  97. roundedAvatar.fill(Qt::transparent);
  98. QPainter painter(&roundedAvatar);
  99. painter.setRenderHint(QPainter::Antialiasing);
  100. QPainterPath path;
  101. path.addEllipse(0, 0, avatar.width(), avatar.height());
  102. painter.setClipPath(path);
  103. painter.drawPixmap(0, 0, avatar);
  104. avatarLabel->setPixmap(roundedAvatar);
  105. }
  106. void UserProfileWidget::setStatus(const QString &status)
  107. {
  108. if (statusLabel) {
  109. statusLabel->setText(status);
  110. }
  111. }