标签:显示 com 2-2 qpixmap 背景图 == setw pix pos
转发自邵发《C/C++系列教程》Qt界面开发 https://chuanke.baidu.com/4509752-209060.html
1.自己用代码画界面
2.用Laber控件放一张背景大图
hpp
1 #include <QtWidgets> 2 3 4 private: 5 virtual void paintEvent(QPaintEvent *event); 6 7 virtual void mouseMoveEvent(QMouseEvent * event); 8 virtual void mousePressEvent(QMouseEvent * event); 9 virtual void mouseReleaseEvent(QMouseEvent * event); 10 11 QPixmap m_imgTitle; 12 13 bool m_dragging; // 是否正在拖动 14 QPoint m_startPosition; // 拖动开始前的鼠标位置 15 QPoint m_framePosition; // 窗体的原始位置
cpp
1 //写在ui.setupUi(this);下面 2 m_dragging = false; 3 4 // 不显示标题栏(亦无边框) 5 setWindowFlags(Qt::Window | Qt::FramelessWindowHint); 6 7 // 背景透明(全自画) 8 setAttribute(Qt::WA_TranslucentBackground); 9 10 // 标题图片 11 m_imgTitle.load(":/Resources/title-bg.jpg"); 12 13 14 15 void TestAandB::paintEvent(QPaintEvent *event) 16 { 17 QPainter painter(this); 18 QRect frameRect = rect(); 19 frameRect.adjust(1, 1, -1, -1); 20 21 // 自画背景和边框 22 painter.setPen(QColor(0x66, 0x66, 0x66)); 23 painter.setBrush(QColor(0xFF, 0xFF, 0xFF)); 24 painter.drawRoundRect(frameRect, 2, 2); 25 26 // 画标题栏 27 QRect titleRect = frameRect; 28 titleRect.setBottom(frameRect.top() + 80); 29 30 QPainterPath path; 31 path.addRoundedRect(frameRect.adjusted(1, 1, 0, -1), 2, 2); 32 path.intersects(titleRect); 33 painter.setClipPath(path); 34 painter.drawPixmap(titleRect, m_imgTitle); 35 36 } 37 38 void TestAandB::mousePressEvent(QMouseEvent *event) 39 { 40 // 只响应左键 41 if (event->button() == Qt::LeftButton) 42 { 43 QRect titleRect = rect(); 44 titleRect.setBottom(titleRect.top() + 80); 45 46 if (titleRect.contains(event->pos())) 47 { 48 m_dragging = true; 49 m_startPosition = event->globalPos(); 50 m_framePosition = frameGeometry().topLeft(); 51 } 52 } 53 54 QWidget::mousePressEvent(event); 55 } 56 57 void TestAandB::mouseMoveEvent(QMouseEvent *event) 58 { 59 // 只响应左键 60 if (event->buttons() & Qt::LeftButton) 61 { 62 if (m_dragging) 63 { 64 // delta 相对偏移量, 65 QPoint delta = event->globalPos() - m_startPosition; 66 67 // 新位置:窗体原始位置 + 偏移量 68 move(m_framePosition + delta); 69 } 70 } 71 72 QWidget::mouseMoveEvent(event); 73 } 74 75 void TestAandB::mouseReleaseEvent(QMouseEvent * event) 76 { 77 m_dragging = false; 78 QWidget::mouseReleaseEvent(event); 79 }
2020年1月2日
Caesar卢尚宇
标签:显示 com 2-2 qpixmap 背景图 == setw pix pos
原文地址:https://www.cnblogs.com/nxopen2018/p/12166058.html