0
自定義項目CPP:添加自定義的QGraphicsItem到場景禁用ScrollBarAsNeeded和對齊選項
MapNode::MapNode(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
{
this->x = x;
this->y = y;
this->w = w;
this->h = h;
}
QRectF MapNode::boundingRect() const
{
return QRectF(DeafultX, DeafultY, DeafultW, DeafultH);
}
void MapNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush redBrush(Qt::red);
QPen blackPen(Qt::black);
blackPen.setWidth(1);
painter->setBrush(redBrush);
painter->setPen(blackPen);
painter->drawRect(x,y,w,h);
}
添加到場景:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QBrush redBrush(Qt::red);
QPen blackPen(Qt::black);
blackPen.setWidth(1);
for(int i = 0; i < 992; i+=62)
{
for(int j = 0; j < 992; j+=62)
{
QGraphicsItem *myItem = new MapNode(i,j,60,60);
scene->addItem(myItem);
//scene->addRect(i,j,60,60,blackPen,redBrush); //working fine
}
}
}
- 當添加我的項目,他們開始從圖紙在圖形視圖的中間(我將圖形視圖對齊設置爲中心,這對addRect非常有用),當添加更多項目時,圖形視圖屏幕可以查看滾動條正在工作,添加我的項目時,他們被禁用。
- 我試圖從圖形視圖中得到相同的行爲,因爲我在將rect(addRect)添加到場景時特別是ScrollBarAsNeeded和對齊選項。
謝謝!
什麼是DeafultX,DeafultY,DeafultW和DeafultH? – Anthony
他們是(0,0,100,100)。 我也將滾動條設置爲始終打開,在圖形視圖中顯示,但顯示爲禁用 - 意味着我無法使用它。 – GoldenAxe
嘗試使你的邊界矩形函數返回'QRectF(x,y,w,h)' – Anthony