如何为小部件创建自定义布局 [英] How to create custom layout for widget
问题描述
我正在尝试在 QT 中创建一个自定义小部件,如下所示:
I'm trying to create a custom widget in QT that looks something like this:
红色方块将显示图像/图标.如何通过对小部件进行编码来实现这种布局?我需要创建许多具有相同布局但标签值不同的小部件.理想情况下,我会将这些小部件显示为在主窗口中带有滚动条的列表.但是现在我正在努力通过代码为这些小部件创建布局.非常感谢任何帮助.
The red squares will be displaying an image/icon. How can I achieve this layout through coding the widget? I will need to create many of those widgets with the same layout but different values in their labels. Ideally, I will display those widgets as a list with a scrollbar in my mainwindow. But right now I'm struggling to just create the layout for those widgets through code. Any help is much appreciated.
推荐答案
您需要将您的设计拆分为单独的部分.每个段可以是单独的子布局或小部件.在您的示例中,我看到以下部分:
You need to split you design on to separate segments. Each segment can be either a separate sub layout or a widget. In you example, I see the following segments:
- 大红色图标,
- 两个标签:TextLabel 和 06-November-2014...
- 两个标签构成垂直框布局,
- 垂直框布局和红色大图标构成水平框布局,
- 红色小矩形形成一个单独的布局,
- 所有布局构成一个主要布局.
现在让我们编码这个组合:
Now lets code this composition:
QLabel *largeRed = new QLabel(this); // Should set an image for this label
QLabel *lbl1 = new QLabel("06-November-2014...", this);
QLabel *lbl2 = new QLabel("TextLabel", this);
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(lbl1);
vLayout->addWidget(lbl2);
vLayout->addStretch();
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(largeRed);
hLayout->addLayout(vLayout);
QLabel *smallRed = new QLabel(this); // Should set an image for this label
QHBoxLayout *hLayout2 = new QHBoxLayout;
hLayout2->addWidget(smallRed, 0, Qt::AlignRight);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(hLayout);
mainLayout->addLayout(hLayout2);
[..]
这篇关于如何为小部件创建自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!