关于QscrollArea的使用


最近在使用PyQt4 进行图形界面设计,当控件显示的内容过大时,需要在控件的旁边显示滚动条,这时需要用到QscrollArea这个控件。在网上找了好多天,都没有解决问题,最后还是参考模块说明文档,才弄明白这个控件怎么用。

QScrollArea 能够为 widget(或其子类) 添加滚动视图 。滚动视图可以在widget的尺寸超过整个框架的尺寸时,显示滚动条。

示例一:

 QLabel *imageLabel = new QLabel;  QImage image("happyguy.png");  imageLabel->setPixmap(QPixmap::fromImage(image));   scrollArea = new QScrollArea;  scrollArea->setBackgroundRole(QPalette::Dark);  scrollArea->setWidget(imageLabel);

效果图如下:


注意!:当使用QScrollArea 能够 widget 添加滚动视图时,一定要先设置widget的sizehint(可以使用setMinimumSize() 来进行设置。),从而保证widget的内容能够正常显示


示例2
<p><span class="kwd">import</span><span class="pln"> sys</span>
<span class="kwd">from</span><span class="pln"> </span><span class="typ">PyQt4</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> </span><span class="typ">QtCore</span><span class="pun">,</span><span class="pln"> </span><span class="typ">QtGui</span>
<span class="kwd">import</span><span class="pln"> classblock</span>


<span class="kwd">class</span><span class="pln"> </span><span class="typ">MainWindow</span><span class="pun">(</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QMainWindow</span><span class="pun">):</span>
<span class="pln"> </span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> parent</span><span class="pun">=</span><span class="kwd">None</span><span class="pun">):</span>
<span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QMainWindow</span><span class="pun">.</span><span class="pln">__init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> parent</span><span class="pun">)</span>
<span class="pln"> w </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QWidget</span><span class="pun">()</span>
<span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setCentralWidget</span><span class="pun">(</span><span class="pln">w</span><span class="pun">)</span>
<span class="pln"> </span>
<span class="pln"> topFiller </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QWidget</span><span class="pun">()</span>
<span class="pln"> </span><span style="color: rgb(255, 0, 0);"><span class="pln">topFiller</span><span class="pun">.</span><span class="pln">setMinimumSize </span><span class="pun">(</span><span class="lit">1200</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1200</span><span class="pln"> </span><span class="pun">)</span></span>
<span class="pln"> </span><span class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span class="pln"> range</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">40</span><span class="pun">):</span>
<span class="pln"> label </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QLabel</span><span class="pun">(</span><span class="pln"> </span><span class="str">"<--------->"</span><span class="pun">,</span><span class="pln"> topFiller</span><span class="pun">)</span>
<span class="pln"> label</span><span class="pun">.</span><span class="pln">move</span><span class="pun">(</span><span class="pln">i</span><span class="pun">*</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">*</span><span class="lit">10</span><span class="pun">)</span>
<span class="pln"> </span>

<span class="pln"> scroll </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QScrollArea</span><span class="pun">()</span>
<span class="pln"> </span><span style="color: rgb(255, 0, 0);"><span class="pln">scroll</span><span class="pun">.</span><span class="pln">setWidget</span><span class="pun">(</span><span class="pln">topFiller</span><span class="pun">)</span></span>
<span class="pln"> scroll</span><span class="pun">.</span><span class="pln">setAutoFillBackground</span><span class="pun">(</span><span class="kwd">True</span><span class="pun">)</span>
<span class="pln"> </span><span style="color: rgb(255, 0, 0);"><span class="pln">scroll</span><span class="pun">.</span><span class="pln">setWidgetResizable</span><span class="pun">(</span><span class="kwd">True</span><span class="pun">)</span></span>

<span class="pln"> vbox </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QVBoxLayout</span><span class="pun">()</span>
<span class="pln"> </span><span style="color: rgb(255, 0, 0);"><span class="pln">vbox</span><span class="pun">.</span><span class="pln">addWidget</span><span class="pun">(</span><span class="pln">scroll</span><span class="pun">)</span><span class="pln"> </span></span>
<span class="pln"> w</span><span class="pun">.</span><span class="pln">setLayout</span><span class="pun">(</span><span class="pln">vbox</span><span class="pun">)</span>

<span class="pln"> </span>
<span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">statusBar</span><span class="pun">().</span><span class="pln">showMessage</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tr</span><span class="pun">(</span><span class="str">"A context menu is available by right-clicking"</span><span class="pun">))</span>
<span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setWindowTitle</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tr</span><span class="pun">(</span><span class="str">"Menus"</span><span class="pun">))</span>
<span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">resize</span><span class="pun">(</span><span class="lit">480</span><span class="pun">,</span><span class="lit">320</span><span class="pun">)</span>
<span class="pln"> </span>

<span class="pln"> </span>
<span class="kwd">if</span><span class="pln"> __name__ </span><span class="pun">==</span><span class="pln"> </span><span class="str">"__main__"</span><span class="pun">:</span>
<span class="pln"> app </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">(</span><span class="pln">sys</span><span class="pun">.</span><span class="pln">argv</span><span class="pun">)</span>
<span class="pln"> mainwindow </span><span class="pun">=</span><span class="pln"> </span><span class="typ">MainWindow</span><span class="pun">()</span>
<span class="pln"> mainwindow</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>
<span class="pln"> sys</span><span class="pun">.</span><span class="kwd">exit</span><span class="pun">(</span><span class="pln">app</span><span class="pun">.</span><span class="pln">exec_</span><span class="pun">())</span><span style="font-family: Verdana, 'DejaVu Sans', Geneva, sans-serif;"><span style="line-height: 15px;">
</span></span></p><div style="line-height: 15px; font-family: Verdana, 'DejaVu Sans', Geneva, sans-serif;">
</div>
效果图如下:
智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告