简体   繁体   中英

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. It doesnt work. So the problem is not the code. Its just the way that QFRame cant be displayed on QFrame. Anyone know how to fix this??

/************************************************PREVIOUSLY STUFFFFFFFF (IGNORE) **********************************************************/

I have a class from QWidget class, but I want to add a list of 5 widgets into that top level widget.

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. 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.

在此输入图像描述

Cheers, Nick

This is the UI form code... on request...

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.

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.

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.

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 .

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 .

I changed my QFRAME to QWIDGET and it works fine. Thanks guys!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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