如何在 Qt 中为水平布局添加滚动区域? [英] How to add a Scroll area for a horizontal layout in Qt?
问题描述
如何在 Qt 中为水平布局添加滚动区域?我最近开发的应用程序有一个水平布局.水平布局包含一系列标签小部件,因此我需要将它们放在滚动区域中,以便我们可以相应地滚动它.例如,它就像在滚动区域内列出的购物车站点中的最新产品.如何为此定义滚动区域?下面是我使用的示例代码:
How to add a Scroll area for a horizontal layout in Qt? I have a horizontal layout for my application I have been developing recently. The horizontal layout contains a series of widgets of labels so I need to put them in scroll area so that we can scroll it accordingly. For example it's just like the recent products in cart sites which are listed inside a scroll area. How can I define a scroll area for that? Below is the sample code I use:
// 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
添加滚动区域.如何添加?
I need to add the scroll area for horizontalLayout
. How to add it?
推荐答案
您不想为 QHBoxLayout
(水平布局)添加 QScrollArea
...你想要的是一个 QScrollArea
WITH 一个 QHBoxLayout
像这样:
You don't want to add a QScrollArea
for a QHBoxLayout
(horizontal layout) ... what you want is a QScrollArea
WITH a QHBoxLayout
on it like this:
这篇关于如何在 Qt 中为水平布局添加滚动区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!