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);
}
相关推荐
长沙红胖子Qt21 小时前
VTK开发笔记(八):示例Cone5,交互器的实现方式,在Qt窗口中详解复现对应的Demo
qt·vtk·交互·交互器
进击ing小白1 天前
QGraphicsEffect控件添加特效
qt
迷失的walker1 天前
【Qt C++ QSerialPort】QSerialPort fQSerialPortInfo::availablePorts() 执行报错问题解决方案
数据库·c++·qt
B站计算机毕业设计之家1 天前
计算机视觉:pyqt5+yoloV5目标检测平台 python实战 torch 目标识别 大数据项目 目标跟踪(建议收藏)✅
深度学习·qt·opencv·yolo·目标检测·计算机视觉·1024程序员节
上去我就QWER2 天前
解锁Qt元对象系统:C++编程的超强扩展
c++·qt
莫听穿林打叶声儿2 天前
关于Qt开发UI框架Qt Advanced Docking System测试
开发语言·qt·ui
freedom_1024_2 天前
【c++ qt】QtConcurrent与QFutureWatcher:实现高效异步计算
java·c++·qt
KL41802 天前
【QT】窗口
c++·qt
有时间要学习2 天前
Qt——界面优化
开发语言·qt
sulikey2 天前
Qt 入门简洁笔记:常用控件
c++·qt·控件·qwidget·qlabel·qpushbutton·qlineedit