Qt World Summit: Submit your Presentation

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'

    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?

  • Lifetime Qt Champion

    its wants true or false

  • Lifetime Qt Champion

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

  • I mean :

    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),
    //      _equalizerDialog(new EqualizerDialog(this))
        _instance = new VlcInstance(VlcCommon::args(), this);
        _player = new VlcMediaPlayer(_instance);
    //    _equalizerDialog->setMediaPlayer(_player);
    //    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);
        delete _player;
        delete _media;
        delete _instance;
        delete ui;
    void SimplePlayer::openLocal()
        QString file =
                QFileDialog::getOpenFileName(this, tr("Open file"),
                                             tr("Multimedia files(*)"));
        if (file.isEmpty())
        _media = new VlcMedia(file, true, _instance);
    void VlcWidgetVolumeSlider::mousePressEvent(QMouseEvent *event)
       // ...
       // ...
    void VlcWidgetVolumeSlider::mouseReleaseEvent(QMouseEvent *event)
        // ...
       // ...
    void SimplePlayer::on_pushButton_clicked()
       isFullScreen() ? showNormal() : showFullScreen();
    void SimplePlayer::on_checkBox_clicked(bool checked)
        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