Hiding/Showing DockWidgets in Qt 5 in Designer

252 views Asked by At

I'm developing an application with Qt, a framework with which I'm not at all familiar, and I'm attempting to hide and show a DockWidget that I created using designer.

Unlike many of the seemingly similar questions about hiding and showing dockwidgets in Qt is that I made my widget entirely with Qt Designer, so I don't know how to link much of the code I've found in these questions' answers. Essentially, I have no mention of a dockwidget in my *.cpp files, but I do in my .ui file.

How can I incorporate this Designer-created dockwidget into my code to make it visible and invisible?

Sorry for such a nooby question.

Thanks, erip

1

There are 1 answers

0
Bowdzone On BEST ANSWER

When you build your application, qmake generates h from your ui files. So for instance ui_dlg_about.ui is translated into a ui_dlg_about.h automatically. Usually in a folder called GeneratedFiles or something like that. You can then create an actual customisable dialog class which you use in your application by creating something along the following:

dlg_about.h

#include "ui_dlg_about.h"
#include <QDialog>

class dlg_about : public QDialog, protected Ui::ui_dlg_about
{
    Q_OBJECT

    public:
        dlg_about(QWidget* = 0);

    public slots:
        void toggle_dockwidget();
};

dlg_about.cpp

#include "dlg_about.h"

dlg_about::dlg_about(QWidget* parent) : QDialog(parent)
{
    setupUi(this);

    QObject::connect(this->somebutton, SIGNAL(clicked()), this, SLOT(toggle_dockwidget()));
}

void dlg_about::toggle_dockwidget()
{
    if(something){
        this->dockwidget->setVisible(true);
    }else{
        this->dockwidget->setVisible(false);
    }
}

It is also possible for your dialog to not be derived from ui_dlg_about but having it as a member:

dlg_about.h

#include "ui_dlg_about.h"
#include <QDialog>

class dlg_about : public QDialog
{
    Q_OBJECT

    public:
        dlg_about(QWidget* = 0);

    public slots:
        void toggle_dockwidget();

    protected:
        Ui::ui_dlg_about ui;
};

dlg_about.cpp

#include "dlg_about.h"

dlg_about::dlg_about(QWidget* parent) : QDialog(parent)
{
    setupUi(this->ui);

    QObject::connect(this->ui.somebutton, SIGNAL(clicked()), this, SLOT(toggle_dockwidget()));
}

// ....