我正在使用Qt Creator创建一个简单的程序。
QPushButton已插入QGraphicsView和QGraphicsScene中。 但是,插入的PushButton周围有一条灰色边界线,如下图所示。
我尝试使用StyleSheet作为“边框:无”或“轮廓:无”将其删除,但无法删除。
如果再次按下“绘制按钮”或将此对话框窗口移至双屏的另一个屏幕,则灰色边框消失。但是,当我将鼠标移到按钮上时,灰色边界线再次出现。 我需要帮助才能摆脱它。
下面是我的代码。
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();
}
}
答案 0 :(得分:0)
如documentation中所述, QGraphicsView 使用QGraphicsItem::boundingRect()
来确定该项目是否需要重绘。
您对boundingRect
的实现返回了错误的值,从而使 QGraphicsView 愚弄了场景的哪一部分应该(重新)绘制为白色。
在 mroundrect.cpp 中替换:
QRectF mRoundRect::boundingRect() const { return QRectF(0, 0, 0, 0); }
具有:
QRectF mRoundRect::boundingRect() const { return rndRect; }