简体   繁体   English

qt小部件在添加到另一个小部件时会重叠

[英]qt widgets are overlapping when added to another widget

Okay so I have done some debugging. 好的,所以我做了一些调试。 I wrote a code to display my QFrame on another QFrame in a simple layout manager. 我编写了一个代码,在一个简单的布局管理器中在另一个QFrame上显示我的QFrame。 It doesnt work. 它不起作用。 So the problem is not the code. 所以问题不在于代码。 Its just the way that QFRame cant be displayed on QFrame. 它只是QFRame无法在QFrame上显示的方式。 Anyone know how to fix this?? 有人知道怎么修这个东西吗??

/************************************************PREVIOUSLY STUFFFFFFFF (IGNORE) **********************************************************/ /************************************************先前STUFFFFFFFF(IGNORE)********************************************** ************ /

I have a class from QWidget class, but I want to add a list of 5 widgets into that top level widget. 我有一个来自QWidget类的类,但我想在这个顶级小部件中添加一个包含5个小部件的列表。

When I add them they seem to overlap, even if I arranged them in a horizontal layout. 当我添加它们时,它们似乎重叠,即使我将它们排列在水平布局中。

Is there some parameter I m not setting or missing anything? 是否有一些参数我没有设置或遗漏任何东西?

The picture has 5 widget spaces and at runtime I create the widgets and fill them. 图片有5个小部件空间,在运行时我创建小部件并填充它们。 But when I do they get crowded at left for some reason. 但是当我这样做时,由于某种原因,他们会在左边拥挤。

sensor1 = new LightWidget(green, this);
sensor2 = new LightWidget(green, this);
sensor3 = new LightWidget(green, this);
sensor4 = new LightWidget(green, this);
sensor5 = new LightWidget(green, this);

Here is the output.. Instead of seeing 5 green circles I see all of them at left on top of other. 这是输出..而不是看到5个绿色圆圈,我看到所有这些都在左边的另一个上面。

在此输入图像描述

Cheers, Nick 干杯,尼克

This is the UI form code... on request... 这是UI表单代码......根据要求...

void setupUi(QWidget *Form)
{
    if (Form->objectName().isEmpty())
        Form->setObjectName(QString::fromUtf8("Form"));
    Form->resize(762, 150);
    QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(Form->sizePolicy().hasHeightForWidth());
    Form->setSizePolicy(sizePolicy);
    Form->setMinimumSize(QSize(700, 150));
    Form->setMaximumSize(QSize(16777215, 150));
    layoutWidget = new QWidget(Form);
    layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
    layoutWidget->setGeometry(QRect(10, 20, 721, 101));
    layout = new QHBoxLayout(layoutWidget);
    layout->setObjectName(QString::fromUtf8("layout"));
    layout->setContentsMargins(0, 0, 0, 0);
    dataLayout = new QVBoxLayout();
    dataLayout->setObjectName(QString::fromUtf8("dataLayout"));
    posLayout = new QHBoxLayout();
    posLayout->setObjectName(QString::fromUtf8("posLayout"));
    posLabel = new QLabel(layoutWidget);
    posLabel->setObjectName(QString::fromUtf8("posLabel"));

    posLayout->addWidget(posLabel);

    posValue = new QLabel(layoutWidget);
    posValue->setObjectName(QString::fromUtf8("posValue"));
    posValue->setMaximumSize(QSize(16777215, 16777215));

    posLayout->addWidget(posValue);


    dataLayout->addLayout(posLayout);

    speedLayout = new QHBoxLayout();
    speedLayout->setObjectName(QString::fromUtf8("speedLayout"));
    speedLabel = new QLabel(layoutWidget);
    speedLabel->setObjectName(QString::fromUtf8("speedLabel"));

    speedLayout->addWidget(speedLabel);

    speedValue = new QLabel(layoutWidget);
    speedValue->setObjectName(QString::fromUtf8("speedValue"));

    speedLayout->addWidget(speedValue);


    dataLayout->addLayout(speedLayout);

    angleLayout = new QHBoxLayout();
    angleLayout->setObjectName(QString::fromUtf8("angleLayout"));
    angleLabel = new QLabel(layoutWidget);
    angleLabel->setObjectName(QString::fromUtf8("angleLabel"));

    angleLayout->addWidget(angleLabel);

    angleValue = new QLabel(layoutWidget);
    angleValue->setObjectName(QString::fromUtf8("angleValue"));

    angleLayout->addWidget(angleValue);


    dataLayout->addLayout(angleLayout);


    layout->addLayout(dataLayout);

    sensorLayout = new QHBoxLayout();
    sensorLayout->setObjectName(QString::fromUtf8("sensorLayout"));
    sensor1 = new LightWidget(layoutWidget);
    sensor1->setObjectName(QString::fromUtf8("sensor1"));
    sensor1->setEnabled(true);

    sensorLayout->addWidget(sensor1);

    sensor2 = new LightWidget(layoutWidget);
    sensor2->setObjectName(QString::fromUtf8("sensor2"));

    sensorLayout->addWidget(sensor2);

    sensor3 = new LightWidget(layoutWidget);
    sensor3->setObjectName(QString::fromUtf8("sensor3"));

    sensorLayout->addWidget(sensor3);

    sensor4 = new LightWidget(layoutWidget);
    sensor4->setObjectName(QString::fromUtf8("sensor4"));

    sensorLayout->addWidget(sensor4);

    sensor5 = new LightWidget(layoutWidget);
    sensor5->setObjectName(QString::fromUtf8("sensor5"));

    sensorLayout->addWidget(sensor5);


    layout->addLayout(sensorLayout);


    retranslateUi(Form);

    QMetaObject::connectSlotsByName(Form);
} // setupUi

The widgets I m adding are of QFrame type. 我添加的小部件是QFrame类型。

I think you are trying to add the widgets in the layout shown on the right of your form (red rectangle to the right in displaypanel.ui. First determine what the layout is called by clicking on the red rectangle in UI designer and looking at the property inspector. I'll guess it's called horizontalLayout. 我想您正在尝试在窗体右侧显示的布局中添加窗口小部件(displaypanel.ui中右侧的红色矩形。首先通过单击UI设计器中的红色矩形并查看该布局来确定布局的调用方式物业检查员。我猜它叫做horizo​​ntalLayout。

You can access this layout from your code and add them there something like this, assuming you have a Ui::yourform object in the class you are doing this in. 您可以从代码中访问此布局,并将它们添加到这里,假设您在此类中使用了Ui :: yourform对象。

sensor1 = new LightWidget(green, this);
sensor2 = new LightWidget(green, this);
sensor3 = new LightWidget(green, this);
sensor4 = new LightWidget(green, this);
sensor5 = new LightWidget(green, this);
this->ui.horizontalLayout->addWidget(sensor1);
this->ui.horizontalLayout->addWidget(sensor2);
this->ui.horizontalLayout->addWidget(sensor3);
this->ui.horizontalLayout->addWidget(sensor4);
this->ui.horizontalLayout->addWidget(sensor5);

An alternative way to do this would be add them to your form as widgets, then promote the widgets to type LightWidget : take a look here . 另一种方法是将它们作为窗口小部件添加到窗体中,然后将窗口小部件提升为类型LightWidget:看看这里

You need to call 你需要打电话

layout->addWidget(...)

for each child widget where 为每个子窗口小部件

layout = new QSomethingLayout(parent)

It was just that QFRAME CANT BE INSERTED ONTO QWIDGET . 只是QFRAME无法插入QWIDGET

I changed my QFRAME to QWIDGET and it works fine. 我将QFRAME更改为QWIDGET,它运行正常。 Thanks guys!! 多谢你们!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM