How to put QLabel over QWidget?



  • I'm writing program to watch video from few cameras.
    Video from cameras are showing in QWidget transformed to VlcWidgetVideo.
    I wan't to have message "No connection" when there is no connection and wanna it disappears when have connection again.

    I've tried to put QLabel over VlcWidgetVideo in Qt Designer but when video starts it disappears forever.
    Then i've tried to do ui->label->raise() when connection is lost but i don't know how to make it disappear when video signal is back.

    what should i do?



  • how about ui->label->hide() ?? or then again, ui->label-lower().
    I generally use show(), raise() and hide() for this kind of thing. If VlcWidgetVideo is derived from a QWidget it should also have the show() and hide() functions. Maybe you could show and hide it directly?



  • @kenchan yes, i've forgotten about this, thanks
    so ui->label->raise() at the beginning and then ui->label->show() / ui->label->hide () when it is needed is the solution of my problem



  • the only question is how to make these QLabels to change positions and sizes to always in the centers of VlcWidgetVideo



  • does a VlcWidgetVideo have a geometry() function?
    if i does the center of it is geometry().center() so you can use that point to help position your label.
    or you could use the rect of the geometry and reduce that by an amount.



  • @kenchan Yes, it has, many thanks again.



  • All that was great when all widgets were added on form in Designer without layouts.
    Now i try to do it all by the code so i discoverd that solution is to make the underwidget the parent of overwidget, like this:

    label1 = new QLabel(video1);
    label2 = new QLabel(video2);
    label3 = new QLabel(video3);
    label4 = new QLabel(video4);
    

    but i can see only label1 over video1
    and if i do it this way:

    label1 = new QLabel(video2);
    label2 = new QLabel(video1);
    label3 = new QLabel(video3);
    label4 = new QLabel(video4);
    

    i can see only label1 over video2
    so what kind of mistake could it be?



  • the whole code is:

        video1 = new VlcWidgetVideo();
        video2 = new VlcWidgetVideo();
        video3 = new VlcWidgetVideo();
        video4 = new VlcWidgetVideo();
    
        video1->setMinimumSize(440, 260);
        video2->setMinimumSize(440, 260);
        video3->setMinimumSize(440, 260);
        video4->setMinimumSize(440, 260);
    
        _instance1 = new VlcInstance(VlcCommon::args(), this);
        _instance2 = new VlcInstance(VlcCommon::args(), this);
        _instance3 = new VlcInstance(VlcCommon::args(), this);
        _instance4 = new VlcInstance(VlcCommon::args(), this);
    
        _player1 = new VlcMediaPlayer(_instance1);
        _player1->setVideoWidget(video1);
        _player1->setObjectName("_player1");
        _player2 = new VlcMediaPlayer(_instance2);
        _player2->setVideoWidget(video2);
        _player2->setObjectName("_player2");
        _player3 = new VlcMediaPlayer(_instance3);
        _player3->setVideoWidget(video3);
        _player3->setObjectName("_player3");
        _player4 = new VlcMediaPlayer(_instance4);
        _player4->setVideoWidget(video4);
        _player4->setObjectName("_player4");
    
        video1->setMediaPlayer(_player1);
        video2->setMediaPlayer(_player2);
        video3->setMediaPlayer(_player3);
        video4->setMediaPlayer(_player4);
    
        _player1->audio()->setMute(true);
        _player2->audio()->setMute(true);
        _player3->audio()->setMute(true);
        _player4->audio()->setMute(true);
    
        label1 = new QLabel(video1);
        label2 = new QLabel(video2);
        label3 = new QLabel(video3);
        label4 = new QLabel(video4);
    
        label1->setText("No connection 1");
        label1->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
        label2->setText("No connection 2");
        label2->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
        label3->setText("No connection 3");
        label3->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
        label4->setText("No connection 4");
        label4->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
    
        label1->setGeometry(100, 100, 200, 50);
        label2->setGeometry(100, 200, 200, 50);
        label3->setGeometry(100, 300, 200, 50);
        label4->setGeometry(100, 400, 200, 50);
    /*
        label1->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label2->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label3->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label4->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
    */
    /*
        label1->hide();
        label2->hide();
        label3->hide();
        label4->hide();
    */
        video_row1_layout = new QHBoxLayout;
        video_row1_layout->addWidget(video1);
        video_row1_layout->addWidget(video2);
        video_row2_layout = new QHBoxLayout;
        video_row2_layout->addWidget(video3);
        video_row2_layout->addWidget(video4);
    
        main_layout = new QVBoxLayout;
        main_layout->addLayout(video_row1_layout);
        main_layout->addLayout(video_row2_layout);
    
        this->setLayout(main_layout);
    

    any changes do nothing i still see only label1 anywhere
    it seems that setGeometry doesn't work because label1 shows over any of 4 videowidgets but always at the same position - near the center of widget



  • Hello again,
    That does not look like the whole code what does "this" refer to?
    Did you copy this from the ui code and modify it?



  • this is QWidget

    setGeometry works, i've commented resizeEvent code i can see all 4 messages in video1

    ok, the whole code:

    #include <VLCQtCore/Common.h>
    #include <VLCQtCore/Instance.h>
    #include <VLCQtCore/Media.h>
    #include <VLCQtCore/MediaPlayer.h>
    #include <VLCQtCore/Audio.h>
    
    #include "SimplePlayer.h"
    //#include "ui_SimplePlayer.h"
    
    SimplePlayer::SimplePlayer(QWidget *parent) : QWidget(parent), _media1(0), _media2(0), _media3(0), _media4(0)
    {
        open_Local = new QPushButton;
        open_Local->setText("open_Local");
        open_Url = new QPushButton;
        open_Url->setText("open_Url");
    
        video1 = new VlcWidgetVideo();
        video2 = new VlcWidgetVideo();
        video3 = new VlcWidgetVideo();
        video4 = new VlcWidgetVideo();
    
        video1->setMinimumSize(440, 260);
        video2->setMinimumSize(440, 260);
        video3->setMinimumSize(440, 260);
        video4->setMinimumSize(440, 260);
    
        _instance1 = new VlcInstance(VlcCommon::args(), this);
        _instance2 = new VlcInstance(VlcCommon::args(), this);
        _instance3 = new VlcInstance(VlcCommon::args(), this);
        _instance4 = new VlcInstance(VlcCommon::args(), this);
    
        _player1 = new VlcMediaPlayer(_instance1);
        _player1->setVideoWidget(video1);
        _player1->setObjectName("_player1");
        _player2 = new VlcMediaPlayer(_instance2);
        _player2->setVideoWidget(video2);
        _player2->setObjectName("_player2");
        _player3 = new VlcMediaPlayer(_instance3);
        _player3->setVideoWidget(video3);
        _player3->setObjectName("_player3");
        _player4 = new VlcMediaPlayer(_instance4);
        _player4->setVideoWidget(video4);
        _player4->setObjectName("_player4");
    
        video1->setMediaPlayer(_player1);
        video2->setMediaPlayer(_player2);
        video3->setMediaPlayer(_player3);
        video4->setMediaPlayer(_player4);
    
        _player1->audio()->setMute(true);
        _player2->audio()->setMute(true);
        _player3->audio()->setMute(true);
        _player4->audio()->setMute(true);
    
        label1 = new QLabel(video1);
        label2 = new QLabel(video2);
        label3 = new QLabel(video3);
        label4 = new QLabel(video4);
    
        label1->setText("qwer");
        label1->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
        label2->setText("asdf");
        label2->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
        label3->setText("zxcv");
        label3->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
        label4->setText("1234");
        label4->setStyleSheet("color: red; font: bold 14px; qproperty-alignment: AlignCenter;");
    
    /*
        int x = video2->geometry().center().x() - warn_w / 2;
        int y = video2->geometry().center().y() - warn_h / 2;
    */
        label1->setGeometry(100, 50, warn_w, warn_h);
        label2->setGeometry(100, 100, warn_w, warn_h);
        label3->setGeometry(100, 150, warn_w, warn_h);
        label4->setGeometry(100, 200, warn_w, warn_h);
        /*
        label1->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label2->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label3->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label4->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        */
    /*
        label1->hide();
        label2->hide();
        label3->hide();
        label4->hide();
    */
        connect(open_Local, &QPushButton::clicked, this, &SimplePlayer::openLocal);
        connect(open_Url, &QPushButton::clicked, this, &SimplePlayer::openUrl);
    
        connect(_player1, &VlcMediaPlayer::end, this, &SimplePlayer::reconnect);
        connect(_player1, &VlcMediaPlayer::error, this, &SimplePlayer::reconnect);
        connect(_player1, &VlcMediaPlayer::playing, this, &SimplePlayer::have_connection);
        //connect(_player1, &VlcMediaPlayer::stateChanged, this, &SimplePlayer::have_connection);
    
        connect(_player2, &VlcMediaPlayer::end, this, &SimplePlayer::reconnect);
        connect(_player2, &VlcMediaPlayer::error, this, &SimplePlayer::reconnect);
        connect(_player2, &VlcMediaPlayer::playing, this, &SimplePlayer::have_connection);
        //connect(_player2, &VlcMediaPlayer::stateChanged, this, &SimplePlayer::have_connection);
    
        connect(_player3, &VlcMediaPlayer::end, this, &SimplePlayer::reconnect);
        connect(_player3, &VlcMediaPlayer::error, this, &SimplePlayer::reconnect);
        connect(_player3, &VlcMediaPlayer::playing, this, &SimplePlayer::have_connection);
        //connect(_player3, &VlcMediaPlayer::stateChanged, this, &SimplePlayer::have_connection);
    
        connect(_player4, &VlcMediaPlayer::end, this, &SimplePlayer::reconnect);
        connect(_player4, &VlcMediaPlayer::error, this, &SimplePlayer::reconnect);
        connect(_player4, &VlcMediaPlayer::playing, this, &SimplePlayer::have_connection);
        //connect(_player4, &VlcMediaPlayer::stateChanged, this, &SimplePlayer::have_connection);
    
        buttons_layout = new QHBoxLayout;
        buttons_layout->addWidget(open_Local);
        buttons_layout->addWidget(open_Url);
    
        video_row1_layout = new QHBoxLayout;
        video_row1_layout->addWidget(video1);
        video_row1_layout->addWidget(video2);
        video_row2_layout = new QHBoxLayout;
        video_row2_layout->addWidget(video3);
        video_row2_layout->addWidget(video4);
    
        main_layout = new QVBoxLayout;
        main_layout->addLayout(buttons_layout);
        main_layout->addLayout(video_row1_layout);
        main_layout->addLayout(video_row2_layout);
    
        this->setLayout(main_layout);
    
    }
    
    SimplePlayer::~SimplePlayer()
    {
        delete _player1;
        delete _player2;
        delete _player3;
        delete _player4;
    
        delete _media1;
        delete _media2;
        delete _media3;
        delete _media4;
    
        delete _instance1;
        delete _instance2;
        delete _instance3;
        delete _instance4;
    }
    
    void SimplePlayer::openLocal()
    {
        url1.clear();
        url2.clear();
        url3.clear();
        url4.clear();
    
        url1 = tr("rtsp://192.168.3.154:8554/");
        if (url1.isEmpty())
            return;
        _media1 = new VlcMedia(url1, _instance1);
        _player1->open(_media1);
    
        url2 = tr("rtsp://192.168.3.154:8558/");
        if (url2.isEmpty())
            return;
        _media2 = new VlcMedia(url2, _instance2);
        _player2->open(_media2);
    
        url3 = tr("rtsp://192.168.3.154:8562/");
        if (url3.isEmpty())
            return;
        _media3 = new VlcMedia(url3, _instance3);
        _player3->open(_media3);
    
        url4 = tr("rtsp://192.168.3.154:8566/");
        if (url4.isEmpty())
            return;
        _media4 = new VlcMedia(url4, _instance4);
        _player4->open(_media4);
    
        label1->raise();
        label2->raise();
        label3->raise();
        label4->raise();
    }
    
    void SimplePlayer::openUrl()
    {
        url1.clear();
        url2.clear();
        url3.clear();
        url4.clear();
    
        url1 = tr("rtsp://192.168.3.99:8554/");
        if (url1.isEmpty())
            return;
        _media1 = new VlcMedia(url1, _instance1);
        _player1->open(_media1);
    
        url2 = tr("rtsp://192.168.3.99:8558/");
        if (url2.isEmpty())
            return;
        _media2 = new VlcMedia(url2, _instance2);
        _player2->open(_media2);
    
        url3 = tr("rtsp://192.168.3.99:8562/");
        if (url3.isEmpty())
            return;
        _media3 = new VlcMedia(url3, _instance3);
        _player3->open(_media3);
    
        url4 = tr("rtsp://192.168.3.99:8566/");
        if (url4.isEmpty())
            return;
        _media4 = new VlcMedia(url4, _instance4);
        _player4->open(_media4);
    
        label1->raise();
        label2->raise();
        label3->raise();
        label4->raise();
    }
    
    void SimplePlayer::reconnect()
    {
        int x, y;
        VlcMediaPlayer* player = qobject_cast<VlcMediaPlayer*>(sender());
        player->open(player->currentMedia());
        if (QString(player->objectName()) == "_player1")
        {
            x = video1->geometry().center().x() - warn_w / 2;
            y = video1->geometry().center().y() - warn_h / 2;
            label1->setGeometry(x, y, warn_w, warn_h);
            label1->show();
        }
        if (QString(player->objectName()) == "_player2")
        {
            x = video2->geometry().center().x() - warn_w / 2;
            y = video2->geometry().center().y() - warn_h / 2;
            label2->setGeometry(x, y, warn_w, warn_h);
            label2->show();
        }
        if (QString(player->objectName()) == "_player3")
        {
            x = video3->geometry().center().x() - warn_w / 2;
            y = video3->geometry().center().y() - warn_h / 2;
            label3->setGeometry(x, y, warn_w, warn_h);
            label3->show();
        }
        if (QString(player->objectName()) == "_player4")
        {
            x = video4->geometry().center().x() - warn_w / 2;
            y = video4->geometry().center().y() - warn_h / 2;
            label4->setGeometry(x, y, warn_w, warn_h);
            label4->show();
        }
    }
    
    void SimplePlayer::have_connection()
    {
    /*
        VlcMediaPlayer* player = qobject_cast<VlcMediaPlayer*>(sender());
        player->open(player->currentMedia());
        //QString temp = player->objectName() ;
        qDebug() << QString(player->objectName());
        qDebug() << player->state();
        */
        /*
        if (QString(player->objectName()) == "_player1"  && player->state() == 3) // playing
            if (label1->isHidden() == false)
                label1->hide();
        if (QString(player->objectName()) == "_player2"  && player->state() == 3) // playing
            if (label2->isHidden() == false)
                label2->hide();
        if (QString(player->objectName()) == "_player3"  && player->state() == 3) // playing
            if (label3->isHidden() == false)
                label3->hide();
        if (QString(player->objectName()) == "_player4"  && player->state() == 3) // playing
            if (label4->isHidden() == false)
                label4->hide();
                */
    /*
        label1->hide();
        label2->hide();
        label3->hide();
        label4->hide();
        */
    }
    
    void SimplePlayer::resizeEvent(QResizeEvent *event)
    {
    /*
        label1->setGeometry(video1->geometry().center().x() - warn_w / 2, video1->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label2->setGeometry(video2->geometry().center().x() - warn_w / 2, video2->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label3->setGeometry(video3->geometry().center().x() - warn_w / 2, video3->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        label4->setGeometry(video4->geometry().center().x() - warn_w / 2, video4->geometry().center().y() - warn_h / 2, warn_w, warn_h);
        */
    }
    
    


  • seems i've found a problem:
    each label think that its videoparent topleftcorner is (0, 0)



  • can you post the SimplePlayer header please?



  • #ifndef SIMPLEPLAYER_H_
    #define SIMPLEPLAYER_H_
    
    #include <QMainWindow>
    #include <VLCQtWidgets/WidgetVideo.h>
    #include <QDebug>
    #include <QLabel>
    #include <QGridLayout>
    #include <QPushButton>
    
    class VlcInstance;
    class VlcMedia;
    class VlcMediaPlayer;
    
    class SimplePlayer : public QWidget
    {
        Q_OBJECT
    public:
        explicit SimplePlayer(QWidget *parent = 0);
        ~SimplePlayer();
        QHBoxLayout *buttons_layout;
        QHBoxLayout *video_row1_layout;
        QHBoxLayout *video_row2_layout;
        QVBoxLayout *main_layout;
    
    protected:
        void resizeEvent(QResizeEvent *event);
    
    private slots:
        void openLocal();
        void openUrl();
        void reconnect();
        void have_connection();
    
    private:
        VlcInstance *_instance1;
        VlcInstance *_instance2;
        VlcInstance *_instance3;
        VlcInstance *_instance4;
    
        VlcMedia *_media1;
        VlcMedia *_media2;
        VlcMedia *_media3;
        VlcMedia *_media4;
    
        VlcMediaPlayer *_player1;
        VlcMediaPlayer *_player2;
        VlcMediaPlayer *_player3;
        VlcMediaPlayer *_player4;
    
        QPushButton *open_Local;
        QPushButton *open_Url;
    
        VlcWidgetVideo * video1;
        VlcWidgetVideo * video2 ;
        VlcWidgetVideo * video3 ;
        VlcWidgetVideo * video4 ;
    
        QLabel *label1;
        QLabel *label2;
        QLabel *label3;
        QLabel *label4;
    
        QString url1;
        QString url2;
        QString url3;
        QString url4;
    
        int warn_w = 200; // warning width
        int warn_h = 40; // warning_height
    };
    
    #endif // SIMPLEPLAYER_H_
    

    ok, by this changes in code:

        int x = video1->geometry().center().x();// - warn_w / 2;
        int y = video1->geometry().center().y();// - warn_h / 2;
    
        label1->setGeometry(x, y, warn_w, warn_h);
        label2->setGeometry(x, y, warn_w, warn_h);
        label3->setGeometry(x, y, warn_w, warn_h);
        label4->setGeometry(x, y, warn_w, warn_h);
    

    i can see all 4 warnings over each video and all them at the bottom-right-corner, because x = 319 and y = 239
    and of course if i get coordinates from main widget and put it into videowidget when top-left-corner is (0, 0) i wouldn't see it - it's beyond the borders



  • Great i will see it a can build something like it. You probably need to map the coordinates



  • What version of Qt are you using and what is the platform?



  • @kenchan win7x64 Qt5.7.1



  • OK, your resize event should probably look something like this

    label1->setGeometry(video1->rect().adjusted(video1->rect().width()/4,video1->rect().height()/4,-video1->rect().width()/4,-video1->rect().height()/4));
    label2->setGeometry(video2->rect().adjusted(video2->rect().width()/4,video2->rect().height()/4,-video2->rect().width()/4,-video2->rect().height()/4));
    label3->setGeometry(video3->rect().adjusted(video3->rect().width()/4,video3->rect().height()/4,-video3->rect().width()/4,-video3->rect().height()/4));
    label4->setGeometry(video4->rect().adjusted(video4->rect().width()/4,video4->rect().height()/4,-video4->rect().width()/4,-video4->rect().height()/4));


  • my solution is:

        QRect videoRect = video1->geometry();
        label1->setGeometry(videoRect.width()/2 - warn_w/2, videoRect.height()/2, warn_w, warn_h);
    

    for others just put 2, 3, 4 instead of 1 and it works, but again many thanks



  • Good, glad you got it working.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.