如何去除QGraphicsView&Scene上添加的QPushButton的边界线

Posted

技术标签:

【中文标题】如何去除QGraphicsView&Scene上添加的QPushButton的边界线【英文标题】:How to remove boundary line of QPushButton which is added on QGraphicsView&Scene 【发布时间】:2018-08-22 06:05:11 【问题描述】:

我正在使用 Qt Creator 创建一个简单的程序。

QPushButton 已插入 QGraphicsView 和 QGraphicsScene。 但是,插入的 PushButton 周围有一条灰色的边界线,如下图所示。

我尝试使用 StyleSheet 作为 'border: none' 或 'outline: none' 将其删除,但无法删除。

如果我再次按下“绘图按钮”或将此对话窗口移动到双屏的另一个屏幕,灰色边框会消失。但是,当我将鼠标移到按钮上时,再次出现灰色边界线。 我需要帮助才能摆脱它。

下面是我的代码。

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>

#include "drawscene.h"

namespace Ui  class MainWindow; 

class MainWindow : public QMainWindow

    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
    QGraphicsScene *mainScene;

    bool is_drawed;

    drawScene *mDrawScene;
;

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    ui->setupUi(this);

    mainScene = new QGraphicsScene(this);
    ui->graphicsView->setScene(mainScene);

    is_drawed = false;
    mDrawScene = new drawScene(mainScene, this);


MainWindow::~MainWindow()

    delete ui;


void MainWindow::on_pushButton_clicked()

    ui->graphicsView->setBackgroundBrush(QBrush("#e7e7e7"));

    if (!is_drawed) 
        mDrawScene->drawBttnView();
        is_drawed = true;
    


void MainWindow::on_pushButton_2_clicked()

    ui->graphicsView->setBackgroundBrush(QBrush("white"));

    if (is_drawed) 
        mDrawScene->clearBttnView();
        is_drawed = false;
    

mroundrect.h

#ifndef MROUNDRECT_H
#define MROUNDRECT_H

#include <QGraphicsItem>
#include <QPainter>
#include <QRectF>

class mRoundRect : public QGraphicsItem

public:
    mRoundRect();

    void setRndRectPos(QRect rect);

private:
    QRect rndRect;

protected:
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
;

#endif // MROUNDRECT_H

mroundrect.cpp

#include "mroundrect.h"

mRoundRect::mRoundRect()  

void mRoundRect::setRndRectPos(QRect rect)  rndRect = rect; 

QRectF mRoundRect::boundingRect() const  return QRectF(0, 0, 0, 0); 

void mRoundRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setBrush(QBrush(Qt::white));
    painter->setPen(QPen(Qt::gray));
    painter->drawRoundedRect(rndRect, 15.0, 15.0);

drawscene.h

#ifndef DRAWSCENE_H
#define DRAWSCENE_H

#include <QGraphicsProxyWidget>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QPushButton>
#include <QStringList>
#include <QRect>
#include <QList>

#include "mroundrect.h"

class drawScene

public:
    drawScene(QGraphicsScene *parentScene, QWidget *parent = 0);

    void drawBttnView();
    void clearBttnView();

private:
    QGraphicsScene *mScene;
    QPushButton *bttn1, *bttn2, *bttn3, *bttn4;
    mRoundRect  *rndRect;

    void setBttnItem(QPushButton *bttn, QStringList info, QRect rect);
    void freeWidget(QPushButton *bttn);
;

#endif // DRAWSCENE_H

drawscene.cpp

#include "drawscene.h"

drawScene::drawScene(QGraphicsScene *parentScene, QWidget *parent)

    mScene = parentScene;
    bttn1 = new QPushButton();
    bttn2 = new QPushButton();
    bttn3 = new QPushButton();
    bttn4 = new QPushButton();
    rndRect = new mRoundRect();


void drawScene::setBttnItem(QPushButton *bttn, QStringList info, QRect rect)

    QString name  = info[0],
            text  = info[1];

    QFont bttnfnt("Calibri", 12);

    bttn->setObjectName(name);
    bttn->setGeometry(rect);
    bttn->setText(text);
    bttn->setFont(bttnfnt);
    mScene->addWidget(bttn);


void drawScene::drawBttnView()

    QStringList bttnInfo;

    QRect bttnPos;

    rndRect->setRndRectPos(QRect(30, 30, 400, 300));
    mScene->addItem(rndRect);

    bttnInfo.clear();
    bttnInfo << "bttn1" << "TEST1";
    bttnPos = QRect(100, 100, 100, 30);
    setBttnItem(bttn1, bttnInfo, bttnPos);

    bttnInfo.clear();
    bttnInfo << "bttn2" << "TEST2";
    bttnPos = QRect(250, 100, 100, 30);
    setBttnItem(bttn2, bttnInfo, bttnPos);

    bttnInfo.clear();
    bttnInfo << "bttn3" << "TEST3";
    bttnPos = QRect(100, 200, 100, 30);
    setBttnItem(bttn3, bttnInfo, bttnPos);

    bttnInfo.clear();
    bttnInfo << "bttn4" << "TEST4";
    bttnPos = QRect(250, 200, 100, 30);
    setBttnItem(bttn4, bttnInfo, bttnPos);


void drawScene::freeWidget(QPushButton *bttn)

    QGraphicsProxyWidget *proxy;
    proxy = bttn->graphicsProxyWidget();
    mScene->removeItem(proxy);
    proxy->setWidget(NULL);
    //bttn->setParent(NULL);
    delete proxy;


void drawScene::clearBttnView()

    freeWidget(bttn1);
    freeWidget(bttn2);
    freeWidget(bttn3);
    freeWidget(bttn4);

    QList<QGraphicsItem *> allItems = mScene->items();
    foreach (QGraphicsItem *item, allItems) 
        mScene->removeItem(item);
        mScene->update();
    

【问题讨论】:

【参考方案1】:

原因

如documentation 中所述,QGraphicsView 使用QGraphicsItem::boundingRect() 来确定项目是否需要重绘。

boundingRect 的实现返回了错误的值,从而欺骗了 QGraphicsView 场景的哪个部分应该(重新)涂成白色。

解决方案

mroundrect.cpp 中替换:

QRectF mRoundRect::boundingRect() const  return QRectF(0, 0, 0, 0); 

与:

QRectF mRoundRect::boundingRect() const  return rndRect; 

结果

【讨论】:

@YOLOGO,不客气。如果您喜欢这个答案,不妨考虑点赞。

以上是关于如何去除QGraphicsView&Scene上添加的QPushButton的边界线的主要内容,如果未能解决你的问题,请参考以下文章

ng-bind-html指令和$sce服务

QGraphicsView Scale & QGraphicsRectItem 绘画未能调用

从 QGraphicsScene/QgraphicsItemGroup/QGraphicsView 中正确删除项目

未调用 QGraphicsView 的 MousePressEvent

如何将 $sce.trustAsResourceUrl 与 $http 服务一起使用

如何使用 $sce 插入带有 angular.js 的 iframe?