Qwidget 添加 滚动条 QScrollArea
2014-01-23 14:23
495 查看
QScrollArea 可以方便的为Qwidget添加上滚动条;
一: 一般的方法:
来自帮助文档:
The QScrollArea class provides a scrolling view onto another widget.
A scroll area is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed. The child widget must be specified with
setWidget(). For example:
QLabel *imageLabel = new QLabel;
QImage image("happyguy.png");
imageLabel->setPixmap(QPixmap::fromImage(image));
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
scrollArea->show(); // 注意添加这句话显示;
二:自定义QWidget子类:
有帮助文档:The QScrollArea class provides a scrolling view onto another widget ,这句话可以知道,QScrollArea 可以给任何QWidget添加滚动条;
但是一般自定义窗体添加滚动条不显示,如:
class CWidget : public QWidget
{
Q_OBJECT
public:
CWidget( QWidget * parent = 0 ): QWidget(parent)
{
}
virtual ~CWidget()
{
}
protected:
void paintEvent(QPaintEvent * pQPaintEvent )
{
QPainter painter( this );
painter.drawLine( 0,0, 100,100 );
}
};
void showCWidget() //这个函数没有显示滚动框中的CWidget ;
{
CWidget * pwidget = new CWidget;
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(pwidget );
scrollArea->show();
}
查看帮助文档:
Size Hints and Layouts
When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. If a standard QWidget is used for the child widget,
it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area.
If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you
intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will cause the size of
the scroll area to be updated whenever the contents of the layout changes.
For a complete example using the QScrollArea class, see the Image Viewer example. The example shows how to combine QLabel and QScrollArea to display an image.
所以上述函数可以修改为:
void showCWidget() //这个函数没有显示滚动框中的CWidget ;
{
CWidget * pwidget = new CWidget;
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
pwidget->setMinimumSize (100, 100 );
[b]pwidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); //可选;
[/b]scrollArea->setWidget(pwidget );
scrollArea->show();
}
一: 一般的方法:
来自帮助文档:
The QScrollArea class provides a scrolling view onto another widget.
A scroll area is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed. The child widget must be specified with
setWidget(). For example:
QLabel *imageLabel = new QLabel;
QImage image("happyguy.png");
imageLabel->setPixmap(QPixmap::fromImage(image));
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
scrollArea->show(); // 注意添加这句话显示;
二:自定义QWidget子类:
有帮助文档:The QScrollArea class provides a scrolling view onto another widget ,这句话可以知道,QScrollArea 可以给任何QWidget添加滚动条;
但是一般自定义窗体添加滚动条不显示,如:
class CWidget : public QWidget
{
Q_OBJECT
public:
CWidget( QWidget * parent = 0 ): QWidget(parent)
{
}
virtual ~CWidget()
{
}
protected:
void paintEvent(QPaintEvent * pQPaintEvent )
{
QPainter painter( this );
painter.drawLine( 0,0, 100,100 );
}
};
void showCWidget() //这个函数没有显示滚动框中的CWidget ;
{
CWidget * pwidget = new CWidget;
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(pwidget );
scrollArea->show();
}
查看帮助文档:
Size Hints and Layouts
When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. If a standard QWidget is used for the child widget,
it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area.
If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you
intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will cause the size of
the scroll area to be updated whenever the contents of the layout changes.
For a complete example using the QScrollArea class, see the Image Viewer example. The example shows how to combine QLabel and QScrollArea to display an image.
所以上述函数可以修改为:
void showCWidget() //这个函数没有显示滚动框中的CWidget ;
{
CWidget * pwidget = new CWidget;
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
pwidget->setMinimumSize (100, 100 );
[b]pwidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); //可选;
[/b]scrollArea->setWidget(pwidget );
scrollArea->show();
}
相关文章推荐
- 给QWidget添加滚动条(QScrollArea)
- QScrollArea可以帮助我们实现让一个widget的内容带有滚动条(QWidget里内置QScrollArea,QScrollArea里再内置其它QWidget)
- 【Qt开发】QScrollArea添加布局后没有出现滚动条的解决方法
- QT 利用QSplitter 分割区域, 并添加QScrollArea 滚动区域,滚动条
- QT 利用QSplitter 分割区域, 并添加QScrollArea 滚动区域,滚动条
- Qt QScrollArea显示滚动条(添加自定义控件)
- Qt---为窗体添加滚动条:QScrollArea
- C# WPF 为Frame添加滚动条(Scroll)
- 在Qt4.8下为窗体添加滚动区域QScrollArea
- css滚动条 scroll问题 添加scroll 显示scroll
- C# WPF 为Frame添加滚动条(Scroll)
- 动态地往datagirdview中添加数据,当所添加的数据超过datagridview的显示范围后,滚动条就无法显示,程序出现假死状态,(scrollbars=both)
- QScrollArea里面添加控件的时候,自动滚到到最后
- CCScrollView添加滚动条和滚动背景
- QScrollArea 自动完成滚动条的功能
- 解决QScrollArea不能出现滚动条的问题
- Qt ScrollArea不出现滚动条及滚动条失效的解决办法以及所有控件保持最小尺寸而不再缩小的方法
- QScrollArea不能显示滚动条
- java swt TitleAreaDialog 添加滚动条
- Qt 第六章 QScrollArea类给QWidget添加滚动条