2011-10-20 165 views
0

QScrollArea,出於某種原因,當我將QGraphicsView設置爲其小部件時,忽略了contentMargins設置。看下面的代碼片段,有人能告訴我是否做錯了什麼,或者它可能是SDK中的錯誤?QScrollArea不尊重contentMargins設置

片段1(完美的作品):

QWidget *appWindow = new QWidget; 

QScrollArea *sa = new QScrollArea(appWindow); 
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
sa->setContentMargins(50, 50, 50, 50); 

QWidget *widgetToScroll = new QWidget(sa); 
widgetToScroll->resize(5000, 5000); 
sa->setWidget(widgetToScroll); 

QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow); 
appWindowLayout->addWidget(sa); 
appWindow->setLayout(appWindowLayout); 

appWindow->show(); 

片段2(這就像setContentMargins()呼叫被完全忽略):

QWidget *appWindow = new QWidget; 

QScrollArea *sa = new QScrollArea(appWindow); 
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
sa->setContentMargins(50, 50, 50, 50); 

QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa); 
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop); 
widgetToScroll->resize(5000, 5000); 
sa->setWidget(widgetToScroll); 

QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow); 
appWindowLayout->addWidget(sa); 
appWindow->setLayout(appWindowLayout); 

appWindow->show(); 

感謝。

回答

3

看起來你是在混淆的結構如何嵌套的QGraphicsView和QGraphicsScene。 (也許這只是一個錯字?)當你添加一個QWidget到佈局

QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa); 

應改爲

QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa); 

QGraphicsView *widgetToScroll = new QGraphicsView(); 
    sa->setWidget(widgetToScroll); 

,你改變小部件的父。當您設置一個小部件(或的QGraphicsView)到QScrollArea,你改變小部件的父。有關更多信息,請參閱Object Trees & Ownership。所以,如果你想建立一個QScrollArea裏面你的QGraphicsView你的代碼應該是這樣的:

QWidget *appWindow = new QWidget; 

    QScrollArea *sa = new QScrollArea(); // No need to specify a parent here if 
             // you add it to a layout later 
    sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
    sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
    sa->setContentsMargins(50, 50, 50, 50); 

    QGraphicsView *widgetToScroll = new QGraphicsView(); 
    widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop); 
    widgetToScroll->resize(5000, 5000); 
    sa->setWidget(widgetToScroll); // This sets the parent for widgetToScroll 

    QVBoxLayout *appWindowLayout = new QVBoxLayout(); 
    appWindowLayout->addWidget(sa); // This sets the parent for sa 
    appWindow->setLayout(appWindowLayout); // This sets the parent for appWindowLayout 

    appWindow->show(); 

作爲一個側面說明...

使用時使用QGraphicsViews與QGraphicsScene,而不是設置頁邊距一個QScrollArea的setContentsMargins,我使用的QGraphicsView自動滾動,只是設置場景RECT有較大的餘量,我的內容的大小,像這樣:

QWidget *appWindow = new QWidget; 

    QGraphicsView *widgetToScroll = new QGraphicsView(); 
    QGraphicsScene *scene = new QGraphicsScene(); 
    scene->addRect(0,0, 5000, 5000); 

    widgetToScroll->setSceneRect(-50,-50, 5050, 5050); 
    widgetToScroll->setScene(scene); 

    QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow); 
    appWindowLayout->addWidget(widgetToScroll); 

    appWindow->setLayout(appWindowLayout); 
    appWindow->show(); 

的的QGraphicsView不僅僅包括自動scrolli相當多當需要時。你可以調整它裏面的所有內容,並且可以更多一些。它非常適合2D佈局,交互和動畫。有關更多信息,請參閱Qt的圖形視圖框架http://doc.qt.io/qt-5/graphicsview.html

下面是使用邊距和補時可能有用的信息由The Box Model QStyleSheets使用。

+0

我還是個新手,Qt的;您的詳細答案確實幫助我更多地理解Qt概念。我一定會嘗試你的建議。 – gplusplus

+0

沒問題。祝你好運。 – phyatt

4

使內容利潤率正常工作的QScrollArea控件我繼承它,並手動設置視邊距(這是在QT 4.7受保護的方法)

// Extended class 
class QScrollAreaWithMargins : public QScrollArea 
{ 
public: 

    virtual void resizeEvent(QResizeEvent *event) override 
    { 
     // Define content margins here 
     setViewportMargins(5, 0, 0, 0); // <<<<< SET MARGINS HERE 
     QScrollArea::resizeEvent(event); 
    } 
}; 

// Usage 
//... 
mEditorScrollArea = new QScrollAreaWithMargins(); 
//... 
+0

這是正確的答案。 – waldyrious

+0

看不到樹木。也是正確的答案。 – Kamajii

相關問題