Hide/Show a widget



  • Hello! I have two widgets which are adjacent to each other.I want to place a pushbutton which on clicking automatically hide/show the 2nd widget.Can anyone tell me how to do this?Thanks!



  • You can set your push button checkable an d connect clicked(bool) with setVisible(bool) of your widget.


  • Moderators

    @abhay To add to @Roy44 's answer you can just connect button's clicked signal to hide and show slots.



  • @Roy44 I am new to Qt development.I did exactly what you told to do.But,I'm getting errors```
    /SimplePlayer.cpp:105: error: expected primary-expression before 'bool'
    setVisible(bool);
    ^

    Can you tell me why am I getting this errors? and also I am having a groupbox not widget(2nd widget).Is it possible to hide that groupbox?

  • Qt Champions 2016

    Hi
    its wants true or false
    setVisible(true);
    or
    setVisible(false);


  • Qt Champions 2016

    @abhay
    Yes also a GroupBox can be hidden. ( its also a widget)



  • I mean :

    myButton->seCheckable(true);
    QObject::connect(myButton, SIGNAL(clicked(bool)), myWidget, SLOT(setVisible(bool)));
    

    the visibility of your widget depends on button state (checked or not)



  • @Roy44 after changing myButton to checkBox and mywidget to groupBox,I am getting errors like checkBox and groupBox were not declared in this scope.Am I doing something wrong?



  • can you show your code ?



  • @Roy44

    #include <QFileDialog>
    #include <QInputDialog>
    
    #include <VLCQtCore/Common.h>
    #include <VLCQtCore/Instance.h>
    #include <VLCQtCore/Media.h>
    #include <VLCQtCore/MediaPlayer.h>
    
    //#include "EqualizerDialog.h"
    
    #include "SimplePlayer.h"
    #include "ui_SimplePlayer.h"
    
    SimplePlayer::SimplePlayer(QWidget *parent)
        : QMainWindow(parent),
          ui(new Ui::SimplePlayer),
          _media(0)
    //      _equalizerDialog(new EqualizerDialog(this))
    {
        ui->setupUi(this);
    
        _instance = new VlcInstance(VlcCommon::args(), this);
        _player = new VlcMediaPlayer(_instance);
        _player->setVideoWidget(ui->video);
    //    _equalizerDialog->setMediaPlayer(_player);
    
        ui->video->setMediaPlayer(_player);
        ui->volume->setMediaPlayer(_player);
        ui->volume->setVolume(50);
    //    ui->seek->setMediaPlayer(_player);
    
        connect(ui->actionOpenLocal, &QAction::triggered, this, &SimplePlayer::openLocal);
    //    connect(ui->actionOpenUrl, &QAction::triggered, this, &SimplePlayer::openUrl);
        connect(ui->actionPause, &QAction::toggled, _player, &VlcMediaPlayer::togglePause);
        connect(ui->actionStop, &QAction::triggered, _player, &VlcMediaPlayer::stop);
        connect(ui->openLocal, &QPushButton::clicked, this, &SimplePlayer::openLocal);
    //    connect(ui->openUrl, &QPushButton::clicked, this, &SimplePlayer::openUrl);
        connect(ui->pause, &QPushButton::toggled, ui->actionPause, &QAction::toggle);
        connect(ui->stop, &QPushButton::clicked, _player, &VlcMediaPlayer::stop);
    //    connect(ui->equalizer, &QPushButton::clicked, _equalizerDialog, &EqualizerDialog::show);
    }
    
    SimplePlayer::~SimplePlayer()
    {
        delete _player;
        delete _media;
        delete _instance;
        delete ui;
    }
    
    void SimplePlayer::openLocal()
    {
        QString file =
                QFileDialog::getOpenFileName(this, tr("Open file"),
                                             QDir::homePath(),
                                             tr("Multimedia files(*)"));
    
        if (file.isEmpty())
            return;
    
        _media = new VlcMedia(file, true, _instance);
    
        _player->open(_media);
    }
    
    void VlcWidgetVolumeSlider::mousePressEvent(QMouseEvent *event)
    {
       // ...
        QSlider::mousePressEvent(event);
       // ...
    }
    void VlcWidgetVolumeSlider::mouseReleaseEvent(QMouseEvent *event)
    {
        // ...
        QSlider::mouseReleaseEvent(event);
       // ...
    }
    
    
    void SimplePlayer::on_pushButton_clicked()
    {
    
       isFullScreen() ? showNormal() : showFullScreen();
    }
    
    
    void SimplePlayer::on_checkBox_clicked(bool checked)
    {
        checkBox->setCheckable(true);
        QObject::connect(checkBox, SIGNAL(clicked(bool)), groupBox, SLOT(setVisible(bool)));
    }
    

    This is my simpleplayer.cpp code



  • Where do you create your checkbox and your group box ?
    Are they created with designer ? in this case add ui-> brefore.



  • @Roy44 Yeah..That worked.Thanks! But how to expand the 1st widget automatically when the checkbox is not selected?(I mean 1st widget automatically occupies the 2nd widgets place)


  • Moderators

    @abhay The easiest way is to add them in layouts.



  • @p3c0 Thanks!


Log in to reply
 

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