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);
}
相关推荐
励志要当大牛的小白菜6 小时前
ART配对软件使用
开发语言·c++·qt·算法
程序员编程指南13 小时前
Qt 数据库连接池实现与管理
c语言·数据库·c++·qt·oracle
晨风先生14 小时前
如何Visual Studio 的配置从 Qt-Debug 切换到 x64-Debug
ide·qt·visual studio
程序员编程指南16 小时前
Qt OpenGL 集成:开发 3D 图形应用
c语言·数据库·c++·qt·3d
程序员编程指南18 小时前
Qt 网络编程进阶:RESTful API 调用
c语言·网络·c++·qt·restful
程序员编程指南19 小时前
Qt XML 与 JSON 数据处理方法
xml·c语言·c++·qt·json
程序员编程指南20 小时前
Qt 网络编程进阶:网络安全与加密
c语言·网络·c++·qt·web安全
2301_8035545221 小时前
【无标题】
开发语言·qt
程序员编程指南1 天前
Qt字符串处理与正则表达式应用
c语言·c++·qt·正则表达式
啊呦.超能力1 天前
QT开发---多线程编程
开发语言·qt