实现的效果如图:
代码如下比较粗糙
其中n_meetings为列表总个数, livelistwidget为自定义列表类,每行显示5个放到scrollArea中
while (n_meetings)
{
if (n_meetings > 5)
{
QWidget *m_livewidget = new QWidget(ui.scrollAreaWidgetContents);
m_livewidget->setObjectName("livewidget");
QHBoxLayout *layout = new QHBoxLayout;
QSpacerItem *horizontalSpacer = new QSpacerItem(1190, 50, QSizePolicy::Maximum, QSizePolicy::Minimum);
//verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
layout->addItem(horizontalSpacer);
//m_livewidget->setStyleSheet("QWidget{background-color:#333333;}");
m_livewidget->setFixedHeight(250);
m_livewidget->setLayout(layout);
//ui.scrollArea->setWidget(m_livewidget);
for (int i = 0; i < 5; i++)
{
livelistwidget *m_livelistwidget = new livelistwidget(bgurl, meetingtitle, starttime, endtime);
layout->addWidget(m_livelistwidget);
}
ui.verticalLayout_3->addWidget(m_livewidget);
//ui.verticalLayout_3->addItem(verticalSpacer);
n_meetings = n_meetings - 5;
}
else
{
QWidget *m_livewidget = new QWidget(ui.scrollAreaWidgetContents);
m_livewidget->setObjectName("livewidget");
QHBoxLayout *layout = new QHBoxLayout;
QSpacerItem *horizontalSpacer = new QSpacerItem(1190, 50, QSizePolicy::Maximum, QSizePolicy::Minimum);
//m_livewidget->setStyleSheet("QWidget{background-color:#333333;}");
m_livewidget->setFixedHeight(250);
m_livewidget->setLayout(layout);
//ui.scrollArea->setWidget(m_livewidget);
for (int i = 0; i < n_meetings; i++)
{
livelistwidget *m_livelistwidget = new livelistwidget(bgurl, meetingtitle, starttime, endtime);
layout->addWidget(m_livelistwidget);
}
ui.verticalLayout_3->addWidget(m_livewidget);
layout->addItem(horizontalSpacer);
n_meetings = 0;
}
}
verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
ui.verticalLayout_3->addItem(verticalSpacer);