2017-09-08 3 views
-1

문제는 QGraphicsView가 비율 크기를 저장하도록하는 것입니다. 나는 Keeping the aspect ratio of a sub-classed QWidget during resize 을 읽었고, 예를 들어 QWidget을 배치하는 데 도움이되었습니다.고정 비율로 QGraphicsView를 배치 할 수 없습니다.

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    QWidget* wid = new QWidget; 
    wid->setStyleSheet("background-color: red"); 

    QWidget* wid2 = new QWidget(wid); 
    QVBoxLayout *vbl = new QVBoxLayout(wid); 

    wid2->setStyleSheet("background-color: yellow"); 
    AspectRatioWidget w(wid2, 4, 3); 
    w.setStyleSheet("background-color: blue"); 

    vbl->addWidget(&w); 

    wid->show(); 

    return a.exec(); 
} 

클래스 AspectRatioWidget : 나는 내 요소를 구현하려고 할 때

// header 
class AspectRatioWidget : public QWidget 
{ 
public: 
    AspectRatioWidget(QWidget *widget, float width, float height, QWidget  *parent = 0); 
    void resizeEvent(QResizeEvent *event); 

private: 
    QBoxLayout *layout; 
    float arWidth; // aspect ratio width 
    float arHeight; // aspect ratio height 
}; 

// cpp 
AspectRatioWidget::AspectRatioWidget(QWidget *widget, float width, float  height, QWidget *parent) : 
QWidget(parent), arWidth(width), arHeight(height) 
{ 
    layout = new QBoxLayout(QBoxLayout::LeftToRight, this); 

    // add spacer, then your widget, then spacer 
    layout->addItem(new QSpacerItem(0, 0)); 
    layout->addWidget(widget); 
    layout->addItem(new QSpacerItem(0, 0)); 
} 

void AspectRatioWidget::resizeEvent(QResizeEvent *event) 
{ 
    float thisAspectRatio = (float)event->size().width()/event->size().height(); 
    int widgetStretch, outerStretch; 

    if (thisAspectRatio > (arWidth/arHeight)) // too wide 
    { 
    layout->setDirection(QBoxLayout::LeftToRight); 
    widgetStretch = height() * (arWidth/arHeight); // i.e., my width 
    outerStretch = (width() - widgetStretch)/2 + 0.5; 
    } 
    else // too tall 
    { 
    layout->setDirection(QBoxLayout::TopToBottom); 
    widgetStretch = width() * (arHeight/arWidth); // i.e., my height 
    outerStretch = (height() - widgetStretch)/2 + 0.5; 
    } 

    layout->setStretch(0, outerStretch); 
    layout->setStretch(1, widgetStretch); 
    layout->setStretch(2, outerStretch); 
} 

Situation when height is too big

Normal ratio

그러나, AspectRatioWidget 보이지 않는이었다.

//main: 
int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    wboard w; 
    w.show(); 

    return a.exec(); 
} 

//and constructor: 

wboard::wboard(QWidget *parent) : 
QWidget(parent) 
{ 
    QWidget* wid= new QWidget(this); 
    wid->setStyleSheet("background-color: red"); 

    QVBoxLayout *vbl = new QVBoxLayout(this); 
    vbl->addWidget(wid); 

    QWidget *window = new QWidget(wid); 
    window->setStyleSheet("background-color: yellow"); 
    QVBoxLayout* vbl2 = new QVBoxLayout(wid); 

    AspectRatioWidget w(window, 3, 1); 
    w.setStyleSheet("background-color: blue"); 

    vbl2->addWidget(&w); 
} 

빨간색 및 노란색 위젯이 없습니다.

코드는 위와 함께 매우 simular하지만 idk입니다.

답변

0

좋아, 문제는 새로운 연산자없이 AspectRatioWidget을 구성하는 것이 었습니다. 첫 번째 예에서는 AspectRatioWidget이 프로그램 이벤트의 전체주기에서 지속되었습니다. 두 번째 예제에서는 해당 객체가 wboard 생성자의 끝 부분에서 삭제되었습니다.

관련 문제