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);
}
相关推荐
blasit13 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
范特西.i6 天前
QT聊天项目(8)
开发语言·qt
枫叶丹46 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
十五年专注C++开发6 天前
Qt deleteLater作用及源码分析
开发语言·c++·qt·qobject
kangzerun6 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
金刚狼886 天前
qt和qt creator的下载安装
开发语言·qt
追烽少年x6 天前
Qt中使用Zint库显示二维码
qt
谁刺我心6 天前
qt源码、qt在线安装器镜像下载
开发语言·qt
金刚狼886 天前
在qt creator中创建helloworld程序并构建
开发语言·qt
扶尔魔ocy7 天前
【转载】QT使用linuxdeployqt打包
开发语言·qt