2014-10-07 114 views
0

如何在Qt中爲水平佈局添加滾動區域?我爲最近開發的應用程序提供了一個水平佈局。水平佈局包含一系列標籤小部件,所以我需要將它們放在滾動區域中,以便我們可以相應地滾動它。例如,它就像滾動區域中列出的購物車網站中的最近產品。我怎樣才能爲此定義一個滾動區域?下面是示例代碼,我使用:如何在Qt中爲水平佈局添加滾動區域?

// val is a list containing some names 
for (int i = 0; i < val.size(); ++i) { 
    if (session->getUserName()!=val[i]) { 
    { 
     QLabel *label4; 
     label4=new QLabel(); 
     label4->width(); 
     label4->height(); 
     QPainter painter(this); 
     painter.setPen(Qt::blue); 
     painter.drawEllipse(0, 0, 20, 20); 
     QPixmap icon(QString::fromUtf8(":/new/prefix1/singleuser.png")); 

     QImage fixedImage(20, 20, QImage::Format_ARGB32_Premultiplied); 
     fixedImage.fill(0); // Make sure you don't have garbage in there 

     QPainter imgPainter(&fixedImage); 
     QPainterPath clip; 
     clip.addEllipse(0, 0, 20, 20); // this is the shape we want to clip to 
     imgPainter.setClipPath(clip); 
     imgPainter.drawPixmap(0, 0, 20, 20, icon); 
     imgPainter.end(); 

     label4->setPixmap(QPixmap::fromImage(fixedImage)); 
     ui->horizontalLayout->addWidget(label4); 
     listforgroup<<label4; 
     QLabel *label; 
     label=new QLabel(val[i]); 

     listforgroup<<label; 

     ui->horizontalLayout->addWidget(label); 
    } 

我需要添加滾動區域的horizontalLayout。如何添加它?

+1

你可以採取看看['QScrollArea'](http://qt-project.org/doc/qt-5/qscrollarea.html)。但是如果你正在創建一個列表,那麼[QListView'](http://qt-project.org/doc/qt-5/qlistview.html)將是一個更好的選擇。 – thuga 2014-10-07 10:45:16

回答

3

你不想添加QScrollAreaQHBoxLayout(水平佈局)......你想要的是與就可以了QHBoxLayout這樣一個QScrollArea

enter image description here