Qt QScrollArea顯示滾動條(添加自定義控件)


  最近在做項目,想要使用一個帶滾動條的窗體來顯示一些信息,可以自己重寫一個區域再關聯一個QScrollBar,但是這樣一來,工作量貌似挺大,之前就知道有QScrollArea對象,但是一直沒用過,心里想着應該可以在上面布一些控件對象,但是后來查了幫助文檔,怎么也沒發現類似於append或者insert之類的方法,有從網上找了些資料,原來QScrollArea可以使用setWidget方法來設置需要使用滾動條來控制顯示的窗口(現在想想,這樣其實是很合理的,我自己當初想偏了,滾動條本來是應該用來控制窗口的滾動,而不能是一個button或者lineEdit)。不過這樣也簡單,只需要子類化QWidget,或者僅僅創建一個QWidget對象,然后在其上使用一個布局,放需要顯示的部件就可以了,於是就上手試了一下,源碼如下:

類及方法的聲明

 1 class ChatList : public QWidget  2 {  3  Q_OBJECT  4 public:  5     explicit ChatList(QWidget *parent = 0);  6     ~ChatList();  7  
 8 private:  9     void initComponent(); 10  
11 signals: 12  
13 public slots: 14  
15 private: 16     QScrollArea* m_pChatListScrollArea; 17     QVBoxLayout* m_pSCVLayout; 18 };

類方法的實現

 1 ChatList::ChatList(QWidget *parent) : QWidget(parent)  2 {  3  initComponent();  4 }  5  
 6 ChatList::~ChatList()  7 {  8     delete m_pChatListScrollArea;  9 } 10  
11 void ChatList::initComponent() 12 { 13     this->m_pChatListScrollArea = new QScrollArea(this); 14     this->m_pChatListScrollArea->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); 15     this->m_pChatListScrollArea->setWidgetResizable(true); 16  
17     this->m_pSCVLayout = new QVBoxLayout(this); 18     this->m_pSCVLayout->setSizeConstraint(QVBoxLayout::SetMinAndMaxSize); 19  
20     QWidget* widget = new QWidget(this); 21     widget->setMinimumSize(72, 32); 22     widget->setMaximumSize(80,32); 23  
24     int i = 0; 25     QString str("pushButton %1"); 26     QPushButton* pushButton; 27     for (i=0; i<10; ++i) 28  { 29         pushButton = new QPushButton(str.arg(i+1), widget); 30         pushButton->setMinimumSize(pushButton->size()); 31         this->m_pSCVLayout->addWidget(pushButton); 32  } 33     widget->setLayout(this->m_pSCVLayout); 34     this->m_pChatListScrollArea->setWidget(widget); 35 }

但是這樣並沒有自己想要的效果,沒有滾動條出現,或者滾動條不可以用,繼續從網上找資料,在QTCN開發網是發現了一個求助帖,在最后一條回答中找到了答案,只需要在initCompont()方法的末尾給this添加一個Layout並且將QScrollArea對象放在該Layout中就可以了,具體需要添加的代碼如下:

1     QVBoxLayout *mainLayout = new QVBoxLayout(this); 2     mainLayout->addWidget(this->m_pChatListScrollArea); 3     this->setLayout(mainLayout);

main函數為:

 1 #include "ChatWidget.h"
 2 #include "ScrollArea.h"
 3 #include <QApplication>
 4 #include <QVBoxLayout>
 5  
 6 int main(int argc, char* argv[])  7 {  8  QApplication app(argc, argv);  9  
10  ChatList chatList; 11  chatList.show(); 12  
13     return app.exec(); 14 }

運行效果如下:

后來又梳理了一下,在使用QScrollArea大體上需要按照這么一個框圖順序

關於initComponent()方法中的一下設置屬性的問題,是根據幫助文檔中關於QScrollArea部分的Size Hints and Layouts部分中建議說明設置的,具體如下:

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.

具體內部原理也不太清楚,只是根據各種資料摸索出來的,也不確定這樣寫是不是合理,如果有更好的建議還望告知,在此先拜謝~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM