Qt客户端技巧 -- 窗口美化 -- 圆角窗口

不解析,直接给代码例子

利用窗口重绘事件处理函数paintEvent

main.cpp

cpp 复制代码
#include <QtCore/qglobal.h>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QApplication>
#else
#include <QtGui/QApplication>
#endif

#include "roundedwindow.h"

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);

  RoundedWindow wnd{};
  wnd.init();

  return a.exec();
}

roundedwindow.h

cpp 复制代码
#ifndef ROUNDEDWINDOW_H
#define ROUNDEDWINDOW_H

#include <QtCore/qglobal.h>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QWidget>
#else
#include <QtGui/QWidget>
#endif

class RoundedWindow : public QWidget {
  Q_OBJECT
public:
  explicit RoundedWindow(QWidget *parent = nullptr);

  void init();

protected:
  void paintEvent(QPaintEvent *event) override;

signals:
};

#endif // ROUNDEDWINDOW_H

roundedwindow.cpp

cpp 复制代码
#include "roundedwindow.h"
#include <QPainter>
#include <QPushButton>
#include <QVBoxLayout>

RoundedWindow::RoundedWindow(QWidget *parent) : QWidget{parent} {

  /* setAttribute 与 setWindowFlag 尽量写在构造函数,写在其他操作之前 */
  // 背景透明
  this->setAttribute(Qt::WA_TranslucentBackground, true);
  // 无边框
  this->setWindowFlag(Qt::FramelessWindowHint);
}

void RoundedWindow::init() {
  this->show();
  QLayout *mainLayout = new QVBoxLayout{this};
  QPushButton *btn = new QPushButton{this};
  btn->setText(QStringLiteral("你好"));
  mainLayout->addWidget(btn);
  this->setLayout(mainLayout);
}

void RoundedWindow::paintEvent(QPaintEvent *event) {
  QPainter painter{this};
  painter.setRenderHint(QPainter::Antialiasing); // 抗据齿,绘图更加平滑
  painter.setBrush(QBrush{QColor{155, 54, 54}});
  painter.drawRoundedRect(this->rect(), 15, 15);
}

利用qss改变主窗口样式

main.cpp

cpp 复制代码
#include <QtCore/qglobal.h>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QApplication>
#else
#include <QtGui/QApplication>
#endif

#include "roundedwindow.h"

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);

  RoundedWindow wnd{};
  wnd.init();

  return a.exec();
}

roundedwindow.h

cpp 复制代码
#ifndef ROUNDEDWINDOW_H
#define ROUNDEDWINDOW_H

#include <QtCore/qglobal.h>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QWidget>
#else
#include <QtGui/QWidget>
#endif

class RoundedWindow : public QWidget {
  Q_OBJECT
public:
  explicit RoundedWindow(QWidget *parent = nullptr);

  void init();

protected:
  void paintEvent(QPaintEvent *event) override;

signals:
};

#endif // ROUNDEDWINDOW_H

roundedwindow.cpp

cpp 复制代码
#include "roundedwindow.h"
#include <QPainter>
#include <QPushButton>
#include <QStyleOption>
#include <QVBoxLayout>

RoundedWindow::RoundedWindow(QWidget *parent) : QWidget{parent} {

  /* setAttribute 与 setWindowFlag 尽量写在构造函数,写在其他操作之前 */
  // 背景透明
  this->setAttribute(Qt::WA_TranslucentBackground, true);
  // 无边框
  this->setWindowFlag(Qt::FramelessWindowHint);
}

void RoundedWindow::init() {
  this->show();
  this->setStyleSheet("QWidget {"
                      " background-color:gray;"
                      " border-radius:15px;"
                      "}");
}

void RoundedWindow::paintEvent(QPaintEvent *event) {
  Q_UNUSED(event);
  /* qwidget的qss仅支持background、background-clip和background-origin属性。
   * 如果您继承QWidget,则需要为自定义QWidget提供paintEvent,如下所示 */
  QStyleOption opt;
  opt.initFrom(this);
  QPainter p(this);
  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
相关推荐
FPGAI12 分钟前
Qt编程之信号与槽
开发语言·qt
只因在人海中多看了你一眼2 小时前
B.50.10.09-RPC核心原理与电商应用
qt·网络协议·rpc
yudiandian20144 小时前
【QT 5.12.12 下载 Windows 版本】
开发语言·qt
炮院李教员7 小时前
使用Qt Core模块(无GUI依赖),确保程序作为后台服务/daemon运行,与任何GUI完全无交互。
开发语言·qt
歪歪1007 小时前
Qt Creator 打包应用程序时经常会遇到各种问题
开发语言·c++·qt·架构·编辑器
滴滴滴嘟嘟嘟.7 小时前
Qt自定义列表项与QListWidget学习
开发语言·qt·学习
滴滴滴嘟嘟嘟.10 小时前
Qt对话框与文件操作学习
开发语言·qt·学习
滴滴滴嘟嘟嘟.12 小时前
Qt图表功能学习
开发语言·qt·学习
钱彬 (Qian Bin)13 小时前
一文掌握工业缺陷检测项目实战(Pytorch算法训练、部署、C++ DLL制作、Qt集成)
c++·pytorch·python·qt·实战·工业缺陷检测·faster rcnn
FPGAI16 小时前
Qt的入门
笔记·qt·学习