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);
}
相关推荐
Stone_OverLooking5 小时前
Qt6.5.3 mingw64 Ninja编译oracle oci驱动
数据库·qt·oracle
小尧嵌入式9 小时前
QT软件开发知识点流程及记事本开发
服务器·开发语言·数据库·c++·qt
努力学习的小廉10 小时前
【QT(三)】—— 信号和槽
开发语言·qt
mengzhi啊10 小时前
qt自制文本,应该没什么用
qt
神仙别闹10 小时前
基于QT(C++)实现(图形界面)连连看
java·c++·qt
sailing-data11 小时前
【UI Qt】入门笔记
开发语言·qt·ui
mengzhi啊11 小时前
qt自绘制,蜂巢网格,感觉没什么用
qt
Y1rong12 小时前
QT之串口调试助手
qt
小尧嵌入式13 小时前
深入理解C/C++指针
java·c语言·开发语言·c++·qt·音视频
零小陈上(shouhou6668889)13 小时前
增加PyQt5界面的交通流量预测(模型为CNN_GRU,CNN_BiGRU_ATTENTION,LSTM,Python代码)
qt·cnn·gru