【问题标题】:Custom border around QGraphicsViewQGraphicsView 周围的自定义边框
【发布时间】:2016-03-11 12:52:42
【问题描述】:

我有这张图片:

我希望我的GraphicsView的边框与这张图片完全一样。

(考虑下图我希望顶部的白色矩形变成那个形状)

我从QGraphicsView 继承了一个类,并尝试在drawBackgroundpaintEvent 中绘制此图像,但它们都不起作用。

我的代码:

.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

有什么建议吗?

【问题讨论】:

  • 您能否在问题中添加您不工作的drawBackground 实现?
  • 如何设置 GW 的大小?你能登录rectQImage("suggestionBorder.png").rect()吗?
  • @Ilya 我希望视图形成图像的形状。我不知道你为什么认为尺寸很重要?
  • 反之亦然,图像可以拉伸以适应视图,但您必须设置视图大小。

标签: c++ qt border qgraphicsview


【解决方案1】:

1) 创建一个与您的图像大小相同的QGraphicsScene

2) 设置为视图的场景

3) 像现在这样在drawBackground 中画图

【讨论】:

  • 它在视图周围绘制边框,但这不是我想要的。我不想要图像之外的白色区域。我希望小部件的形状与边框完全匹配..i.stack.imgur.com/Zn539.png
  • 所以你想通过去除线外的白色部分来裁剪图像?
  • 使用QImage::copy()然后裁剪它。
【解决方案2】:

我试过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"));
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    相关资源
    最近更新 更多