繁体   English   中英

QScrollArea在我的自定义小部件中不起作用

[英]QScrollArea doesn't work in my custom widget

我是使用qt的新手,我的第一个应用程序是设计简单的UI,该UI必须具有自定义窗口小部件(标签和qsliser和spin)才能控制我的视频应用程序,因此我编写了这样的内容

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();

但是当我运行我的应用程序时,我看到了滑块区域,但是里面没有控件; 我的代码有什么问题

您需要一个父小部件,该小部件包含应包含控件的布局。 另外,您不应该在堆栈上创建布局,因为在创建布局的方法返回时会被删除。

遵循以下原则:

// 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

您的代码有很多奇怪的事情。 那些const int &没有意义,您的构造函数无法反映Qt的编码标准。
QWidget的Assign运算符非常大WTF,因此所有QObject都有私有的Assign运算符是有原因的。
看到这些奇怪的事情,我怀疑在您的代码下面还有更多问题,而QScrollArea的错误只是这些问题的体现。

滚动区域可以通过两种方式控制子窗口小部件的大小。 小部件本身已设置布局(这是您需要的),或者正确实现了sizeHint。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM