使小部件在Qt布局中部分重叠 [英] Making widgets partly overlap in a Qt layout

查看:1319
本文介绍了使小部件在Qt布局中部分重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使小部件在Qt布局中部分重叠.我当前的布局如下,通过 QVBoxLayout 包含四个 QHBoxLayout 子级:

I am trying to make widgets overlap partly in a Qt layout. My current layout is the following, achieved with a QVBoxLayout containing four QHBoxLayout children:

我正在尝试将相同花色的卡片重新组合,以实现这样的效果(请注意,水平垂直重叠):

I'm trying to regroup cards of the same suit, in order to achieve something like this (note that there is horizontal and vertical overlapping):

不幸的是,我读过的所有Qt文档和所有Stack Overflow帖子都试图避免小部件重叠而不是寻找它们.也许有一种方法可以在小部件之间设置负间距,或者强制布局具有计算出的最大宽度(例如,根据这种情况下根据一套西装的纸牌数量)?还是我必须创建一个自定义布局?也许我根本不应该使用布局?

Unfortunately, all the Qt documentation and all the Stack Overflow posts I have read try to avoid widgets overlapping rather than to seek it. Maybe there's a way to set negative spacing between widgets, or forcing a layout to have a maximum width calculated (e.g. according to the number of cards of one suit in this case)? Or do I have to create a custom layout? Maybe I should not be using layouts at all?

如果有帮助,我将使用以下代码将小部件添加到布局中:

If it's any help, I'm adding the widgets to the layouts with code that looks like this:

hLayout[card.getSuit()-1]->addWidget(cardWidget, 0, align);

推荐答案

您将需要实现QLayout的子类. QT文档中有一个详细的示例可以解决您的问题:布局管理

You will need to implement a subclass of QLayout. There is a detailed example in the QT documentation addressing exactly you problem: Layout Management

基本上,您需要定义以下内容:

Basically, you will need to define the following:

  • 一种数据结构,用于存储布局处理的项目.每个项目都是一个QLayoutItem.

  • A data structure to store the items handled by the layout. Each item is a QLayoutItem.

addItem(),如何向布局添加项目.

addItem(), how to add items to the layout.

setGeometry(),如何执行布局.

setGeometry(), how to perform the layout.

sizeHint(),首选的布局大小.

sizeHint(), the preferred size of the layout.

itemAt(),如何遍历布局.

itemAt(), how to iterate over the layout.

takeAt(),如何从布局中删除项目.

takeAt(), how to remove items from the layout.

在大多数情况下,您还将实现minimumSize().

In most cases, you will also implement minimumSize().

为方便起见,下面我复制了示例中代码的最重要部分:

Below I have copied the most important part of the code in the example, for your convenience:

class CardLayout : public QLayout
{
public:
    CardLayout(QWidget *parent, int dist): QLayout(parent, 0, dist) {}
    CardLayout(QLayout *parent, int dist): QLayout(parent, dist) {}
    CardLayout(int dist): QLayout(dist) {}
    ~CardLayout();

    void addItem(QLayoutItem *item);
    QSize sizeHint() const;
    QSize minimumSize() const;
    int count() const;
    QLayoutItem *itemAt(int) const;
    QLayoutItem *takeAt(int);
    void setGeometry(const QRect &rect);

private:
    QList<QLayoutItem*> list;
};


void CardLayout::setGeometry(const QRect &r)
{
    QLayout::setGeometry(r);

    if (list.size() == 0)
        return;

    int w = r.width() - (list.count() - 1) * spacing();
    int h = r.height() - (list.count() - 1) * spacing();
    int i = 0;
    while (i < list.size()) {
        QLayoutItem *o = list.at(i);
        QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h);
        o->setGeometry(geom);
        ++i;
    }
}

这篇关于使小部件在Qt布局中部分重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆