调整qlabel大小时不更新布局

时间:2018-02-09 10:50:23

标签: c++ qt

我正在尝试在Qt中实现相机视图应用,但我不知道为什么它没有正确修复布局/按钮。首先,在更新qlabel之前,我有这个:

enter image description here

但是当我点击开始按钮时。 GUI更改为:

enter image description here

创建窗口的代码是:

cameraLabel = new QLabel(tr("No camera loaded"));
cameraLabel->setAlignment(Qt::AlignCenter);
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
cameraLabel->setBackgroundRole(QPalette::Dark);
cameraLabel->setAutoFillBackground(true);
createButtons();
...
..
.

mainLayout = new QHBoxLayout;
mainLayout->addWidget(cameraLabel);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);

void CameraPlayer::createButtons()
{
    QSize iconSize(36, 36);

    playButton = new QToolButton;
    playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    playButton->setIconSize(iconSize);
    playButton->setToolTip(tr("Play"));
    connect(playButton, SIGNAL(clicked()), this, SLOT(play()));


    snapshotButton = new QToolButton;
                snapshotButton->setIcon(style()->standardIcon(QStyle::SP_DialogSaveButton));
    snapshotButton->setIconSize(iconSize);
    snapshotButton->setToolTip(tr("Snapshot"));
    connect(snapshotButton, SIGNAL(clicked()), this, SLOT(snapshot()));

    stopButton = new QToolButton;    cameraLabel = new QLabel(tr("No camera loaded"));
    cameraLabel->setAlignment(Qt::AlignCenter);
    cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    cameraLabel->setBackgroundRole(QPalette::Dark);
    cameraLabel->setAutoFillBackground(true);
    createButtons();

    SParameters& globalParams = GlobalParameters::Instance().params;

    globalParams.IPAddress = "169.251.0.1";
    globalParams.isColor = true;
    globalParams.timer_s =10;


    timerIdCamera = 0;

    mainLayout = new QHBoxLayout;
    mainLayout->addWidget(cameraLabel);
    mainLayout->addLayout(buttonsLayout);
    setLayout(mainLayout);

    stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
    stopButton->setIconSize(iconSize);
    stopButton->setToolTip(tr("Stop"));
    connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));

    colorButton = new QPushButton;
    colorButton->setText(tr("WB / Color"));

    connect(colorButton, SIGNAL(clicked()), this, SLOT(changeColor()));


    buttonsLayout = new QVBoxLayout;
    buttonsLayout->addStretch();
    buttonsLayout->addWidget(playButton);
    buttonsLayout->addWidget(snapshotButton);
    buttonsLayout->addWidget(stopButton);
    buttonsLayout->addWidget(colorButton);
    buttonsLayout->addStretch();

}

要更新qLabel,它配置了一个定时器/触发器,每x个milsecs调用一次并更新qlabel对象:

void CameraPlayer::timerEvent(QTimerEvent *event){
    try
    {

        if (timerIdCamera!=0)
        {
            if (camera.CameraHasImage())
            {
                QImage *qImage = camera.CameraGrab();
                if (qImage!=NULL &&     SIZE_IMAGE!=QString::number(qImage->byteCount()))
                {
                    cameraLabel->setPixmap(QPixmap::fromImage(*qImage));
                    cameraLabel->setFixedWidth(qImage->width());
                    cameraLabel->show();
                 }
                delete[] qImage->bits();
                delete qImage;
            }

        }
    }
    catch (std::exception &ex)
    {
        QMessageBox qMBox;
        qMBox.setText(QString::fromUtf8(ex.what()));
        qMBox.exec();
    }
}

1 个答案:

答案 0 :(得分:0)

问题出在这里

cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

我忽略了其他小部件/布局的布局策略。我对此发表评论并开始运作良好。