Qwidget 添加 滚动条 QScrollArea

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

pwidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); //可选;
scrollArea->setWidget(pwidget );

scrollArea->show();  

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值