选中多个窗口中一个,绘制蓝色边框
1.需求描述
多个播放的窗口,选中其中的一个,用蓝色线框标记出来,如下图所示;
2.实现方式
通过mousePressEvent函数判断是点击了哪个窗口,然后用paintEvent函数来绘制窗口的边框;
#ifndef MULTLAYOUT_H #define MULTLAYOUT_H #include <QWidget> #include "ui_MultLayout.h" #include "LayoutElementMgr.h" #include <QColor> #include <QPainter> #include <QPen> #include <QBrush> #include <QMouseEvent> #include <QPaintEvent> class MultLayout : public QWidget { Q_OBJECT public: MultLayout(QWidget *parent = 0); ~MultLayout(); void SetLayOutType(HUIControl::SplitTypeData::SplitType splitType); protected: virtual void paintEvent(QPaintEvent *ptr_event) override; virtual void mousePressEvent(QMouseEvent *ptr_event) override; private: QColor sel_widget_border_color_ = QColor(59, 144, 248); // 选中窗口边框颜色 Ui::MultLayout ui; QList<QWidget*> m_listWidget; int m_SelectWinId = -1; }; #endif // MULTLAYOUT_H
#include "MultLayout.h" #include "hlog1.h" MultLayout::MultLayout(QWidget *parent) : QWidget(parent) { ui.setupUi(this); } MultLayout::~MultLayout() { } void MultLayout::SetLayOutType(HUIControl::SplitTypeData::SplitType splitType) { CLayoutElemConf* pconfig = LayoutElementMgr::Instance().GetLayoutElementConfig(splitType); if (pconfig==NULL) { //获取失败 LOG_ERROR("GetLayoutElementConfig failed %d", splitType); return; } QVector<LayoutElemItem>* listlayout= pconfig->GetElementItems(); if (listlayout==NULL) { return; } //先清除 QLayoutItem *child; while ((child = ui.gridLayout->takeAt(0)) != nullptr) { if (child->widget()) { child->widget()->setParent(nullptr);//setParent(nullptr),防止删除之后界面不消失 } } //补充widget数量 int widgetcount = pconfig->GetLayoutWndCount(); if (m_listWidget.size()<widgetcount) { for (int i= m_listWidget.size();i<widgetcount;i++) { QWidget* pwisget = new QWidget(this); pwisget->setStyleSheet(QLatin1String("QWidget\n" "{\n" "background:#000000;\n" "}")); m_listWidget.append(pwisget); } } //设置窗口布局 QVector<LayoutElemItem>* layoutitems = pconfig->GetElementItems(); if (layoutitems->size()!= widgetcount) { LOG_ERROR("windows count %d is not equal to %d layout count ", widgetcount, layoutitems->size()); return; } for (int i=0;i<layoutitems->size();i++) { LayoutElemItem item = layoutitems->at(i); ui.gridLayout->addWidget(m_listWidget[i], item.iRow, item.iColumn, item.iRowSpan, item.iColumnSpan); } } void MultLayout::paintEvent(QPaintEvent *ptr_event) { if (m_SelectWinId>=0&&m_SelectWinId<m_listWidget.size()) { QPainter painter(this); QPen pen; QBrush brush; painter.save(); pen.setColor(sel_widget_border_color_); pen.setWidth(1); pen.setStyle(Qt::SolidLine); painter.setPen(pen); brush.setStyle(Qt::NoBrush); painter.setBrush(brush); QRect sel = m_listWidget[m_SelectWinId]->geometry(); QRect sel_widget_border = QRect(sel.x() - 1, sel.y() - 1, sel.width() + 1, sel.height() + 1); painter.drawRect(sel_widget_border); painter.restore(); } } void MultLayout::mousePressEvent(QMouseEvent *ptr_event) { //QPoint press_point_ = ptr_event->pos(); for (int i=0;i<m_listWidget.size();i++) { if (m_listWidget.at(i)->underMouse()) { m_SelectWinId = i; break; } } repaint(); return QWidget::mousePressEvent(ptr_event); }
自己开发了一个股票智能分析软件,功能很强大,需要的关注微信公众号:QStockView