gpt4 book ai didi

c++ - 如何删除在 QGraphicsView&Scene 上添加的 QPushButton 的边界线

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:21 25 4
gpt4 key购买 nike

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

QPushButton 已被插入到QGraphicsView & QGraphicsScene 中。但是,插入的按钮周围有一条灰色的边界线,如下图所示。

我尝试使用样式表将其删除为“边框:无”或“大纲:无”,但我无法删除它。

enter image description here

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

下面是我的代码。

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

主窗口.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();
}
}

最佳答案

原因

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

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

解决方案

mroundrect.cpp 中替换:

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

与:

QRectF mRoundRect::boundingRect() const { return rndRect; }

结果

enter image description here

关于c++ - 如何删除在 QGraphicsView&Scene 上添加的 QPushButton 的边界线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51960669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com