我正在尝试在 QT 中创建一个如下所示的自定义小部件:
红色方 block 将显示图像/图标。 我怎样才能通过编写小部件来实现这种布局?我将需要创建许多布局相同但标签值不同的小部件。理想情况下,我会在主窗口中将这些小部件显示为带有滚动条的列表。但现在我正在努力通过代码为这些小部件创建布局。非常感谢任何帮助。
最佳答案
您需要将设计拆分到不同的部分。每个部分可以是单独的子布局或小部件。在您的示例中,我看到以下部分:
- 大红色图标,
- 两个标签:TextLabel 和 06-November-2014...
- 两个标签组成一个垂直的盒子布局,
- 垂直框布局和红色大图标构成水平框布局,
- 红色小矩形单独布局,
- 所有布局构成一个主布局。
现在让我们编写这个组合:
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);
[..]
关于c++ - 如何为小部件创建自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26802294/