2013-02-28 6 views
0

qt를 처음 사용하고 간단한 앱을 디자인하는 것이 첫 번째 앱이며, UI에는 내 비디오를 제어하는 ​​맞춤 위젯 (레이블 및 qsliser 및 스핀)이 있어야합니다 앱 그래서 내가 쓰기 같은이QScrollArea가 내 맞춤 위젯에서 작동하지 않습니다.

class Controls : public QWidget 
{ 


private: 

    QHBoxLayout *Layout ; 
    string Controlname; 
    QLabel *Label ; 

    QSpinBox *Spin ; 



public: 

    QSlider *Slider ; 
    Controls(QLayout &Parent , string name , const int &Default_value); 
    Controls(const Controls &copy); 
    explicit Controls(); 
    ~Controls(){} 


    QLabel * Get_Label() const { return Label ; } 
    QSlider *Get_Slider() const { return Slider ; } 
    QSpinBox * Get_Spin()const { return Spin ; } 
    QHBoxLayout * Get_Layout() {return Layout;} 

    void SetValue(const int &newvalue); 

    Controls &operator= (const Controls &copy); 


}; 

와 내가 같이 그것을 할이 위젯에서 개체를 만들 : 지금

QVBoxLayout layout ; 
Controls *gg =new Controls (layout , "test", 1); 
Controls *gg2 =new Controls (layout , "test2", 4); 

은 내가 qsliderarea 내이 객체를 만들려하고 그래서 내가 할이

QScrollArea gt ; 
gt.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
gt.setWidget(gg); 
gt.setWidget(gg2); 
gt.show(); 

하지만 내 응용 프로그램을 실행할 때 sliderarea는 볼 수 있지만 컨트롤은 보이지 않습니다. 내 코드의 문제점은 무엇입니까

답변

0

컨트롤이 들어있는 레이아웃이 포함 된 상위 위젯이 필요합니다. 또한 스택을 생성하는 메소드가 반환 될 때 삭제되므로 스택에 레이아웃을 생성하지 않아야합니다. 이 라인을 따라

뭔가 :

// It is not sufficient to set the layout as a parent, you need to add the 
// widgets to the layout. Note that this won't compile unless you change your 
// constructor to accept a QLayout* instead of a QLayout&. 
QVBoxLayout* layout = new QVBoxLayout(); 
layout->addWidget(new Controls(layout, "test1", 1); 
layout->addWidget(new Controls(layout, "test2", 4); 

// A parent widget 'scrollWidget' is required which contains the layout. 
QWidget* scrollWidget = new QWidget(/* maybe assign a parent here 
    so you don't have to worry about deletion */); 
scrollWidget->setLayout(layout); 

// Your scrollArea can now include that 'scrollWidget' which itself contains 
// everything else. 
QScrollArea* scrollArea; 
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 
scrollArea->setWidget(scrollWidget); 
scrollArea.show(); // Note it is more common to have the scroll area as part of 
        // another widget and show that instead 
0

코드는 이상한 일들이 많이 있습니다. 그 const int & 감각이 없다, 당신의 생성자는 코딩의 Qt 표준을 반영하지 않습니다.
QWidget에 대한 할당 연산자가 매우 큰 WTF이므로 모든 QObject에 개인 할당 연산자가있는 이유가 있습니다.
QScrollArea의 코드 및 버그에 훨씬 더 많은 문제가있는 것으로 의심되는 이러한 이상한 일들을 보는 것은 이러한 문제의 징후 일뿐입니다.

스크롤 영역이 자식 위젯의 크기를 제어하는 ​​방법에는 두 가지가 있습니다. 위젯 자체에 레이아웃 세트가 있습니다 (필요한 것입니다). 또는 sizeHint가 올바르게 구현되었습니다.

관련 문제