我希望GraphicsView
的边框与此图像完全相同。
我从QGraphicsView
继承了一个类,并尝试在drawBackground
和paintEvent
中绘制此图像,但它们都不起作用。
我的代码:
.h文件
class GraphicsTraxSuggestionView : public QGraphicsView {
Q_OBJECT
public:
GraphicsTraxSuggestionView(QWidget* widget);
protected:
// void paintEvent(QPaintEvent *event);
void drawBackground(QPainter *p, const QRectF &rect);
private:
};
.cpp文件
GraphicsTraxSuggestionView::GraphicsTraxSuggestionView(QWidget* widget)
: QGraphicsView(widget)
{
//setFrameShadow(QFrame::Raised);
setFrameStyle(QFrame::NoFrame);
setStyleSheet("QGraphicsView { border-style: none; }");
}
void GraphicsTraxSuggestionView::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->drawImage(rect, QImage("suggestionBorder.png"));
}
我的代码结果:http://i.stack.imgur.com/r0waP.png
有什么建议吗?
答案 0 :(得分:0)
1)创建一个具有图像大小的QGraphicsScene
2)将其设置为视图的场景
3)在你正在做的drawBackground
中画画
答案 1 :(得分:0)
我尝试了setMask()
,现在效果很好。
GraphicsTraxSuggestionView::GraphicsTraxSuggestionView(QWidget* widget,QGraphicsScene* scene)
: QGraphicsView(widget),
scene_(scene)
{
setStyleSheet("background-color: transparent;");
QPixmap myPixmap = QPixmap(":/Game/Tiles//suggestionBorder.png").scaled
(scene_->sceneRect().size().width(),scene_->sceneRect().size().height());
setMask(myPixmap.mask());
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void GraphicsTraxSuggestionView::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->drawImage(scene_->sceneRect(), QImage(":/Game/Tiles//suggestionBorder.png"));
}
结果: