Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QtHideAndShowDialog

 

QtQt CreatorLubuntu

 

QtHideAndShowDialog is a Qt class to allow a dialog display a new modal dialog, with the parent hiding.

Technical facts

 

 

 

 

 

 

./CppQtHideAndShowDialog/CppQtHideAndShowDialog.pri

 

INCLUDEPATH += \
    ../../Classes/CppQtHideAndShowDialog

SOURCES += \
    ../../Classes/CppQtHideAndShowDialog/qthideandshowdialog.cpp

HEADERS  += \
    ../../Classes/CppQtHideAndShowDialog/qthideandshowdialog.h

OTHER_FILES += \
    ../../Classes/CppQtHideAndShowDialog/Licence.txt

 

 

 

 

 

./CppQtHideAndShowDialog/qthideandshowdialog.h

 

//---------------------------------------------------------------------------
/*
QtHideAndShowDialog, Qt dialog to display children modally while hidden
Copyright (C) 2012-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppQtHideAndShowDialog.htm
//---------------------------------------------------------------------------
#ifndef RIBI_QTHIDEANDSHOWDIALOG_H
#define RIBI_QTHIDEANDSHOWDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QDialog>
#pragma GCC diagnostic pop

namespace ribi {

///Dialog that lets parent dialog display children dialogs while being hidden
///from http://richelbilderbeek.nl/CppQtHideAndShowDialog.htm
class QtHideAndShowDialog : public QDialog
{
    Q_OBJECT
    
public:
  explicit QtHideAndShowDialog(QWidget* parent = 0) noexcept;

  ///Virtual destructor as this is a base class
  virtual ~QtHideAndShowDialog() noexcept {}

  ///Obtain the version of this class
  static std::string GetVersion() noexcept;

  ///Obtain the version history of this class
  static std::vector<std::string> GetVersionHistory() noexcept;

  ///Hide the parent, show a child, show the parent
  void ShowChild(QtHideAndShowDialog * const dialog);

  ///Disable the parent, show a child, enable the parent
  void ShowModal(QtHideAndShowDialog * const dialog);

protected:
  ///Shows a child until it emits a close_me signal
  bool m_show_child;

  ///closeEvent that emits the close_me signal
  void closeEvent(QCloseEvent *) override;

  ///Close on escape key
  virtual void keyPressEvent(QKeyEvent* event) override;

signals:
  ///Emit the closeEvent of this dialog
  void close_me();

protected slots:
  ///Slot that needs to be called when a child signals close_me
  void close_child();

};

} //~namespace ribi

#endif // RIBI_QTHIDEANDSHOWDIALOG_H

 

 

 

 

 

./CppQtHideAndShowDialog/qthideandshowdialog.cpp

 

//---------------------------------------------------------------------------
/*
QtHideAndShowDialog, Qt dialog to display children modally while hidden
Copyright (C) 2012-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppQtHideAndShowDialog.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "qthideandshowdialog.h"

#include <cassert>
#include <QKeyEvent>

#include "trace.h"

#pragma GCC diagnostic pop

ribi::QtHideAndShowDialog::QtHideAndShowDialog(QWidget* parent) noexcept
  : QDialog(parent),
    m_show_child { false }
{

}

//ribi::QtHideAndShowDialog::~QtHideAndShowDialog() noexcept
//{
//
//}

void ribi::QtHideAndShowDialog::close_child()
{
  m_show_child = false;
}

void ribi::QtHideAndShowDialog::closeEvent(QCloseEvent*)
{
  //QDialog::closeEvent(event); //Not needed
  emit close_me();
  //QDialog::closeEvent(event); //Not needed
}

std::string ribi::QtHideAndShowDialog::GetVersion() noexcept
{
  return "1.4";
}

std::vector<std::string> ribi::QtHideAndShowDialog::GetVersionHistory() noexcept
{
  std::vector<std::string> v {
    "2012-11-13: version 1.0: initial version",
    "2012-11-18: version 1.1: added ShowModal member function",
    "2012-11-18: version 1.2: added ",
    "2012-12-31: version 1.3: added keyPressEvent to close on escape"
    "2013-09-16: version 1.4: noexcept"
  };
  return v;
}

void ribi::QtHideAndShowDialog::keyPressEvent(QKeyEvent* event)
{
  if (event->key() == Qt::Key_Escape)
  {
    close();
    return;
  }
  QDialog::keyPressEvent(event);
}

void ribi::QtHideAndShowDialog::ShowChild(QtHideAndShowDialog * const dialog)
{
  assert(dialog);
  this->hide();
  QObject::connect(dialog,&ribi::QtHideAndShowDialog::close_me,this,&ribi::QtHideAndShowDialog::close_child);
  m_show_child = true;
  while (m_show_child)
  {
    dialog->exec();
  }
  this->show();
}

void ribi::QtHideAndShowDialog::ShowModal(QtHideAndShowDialog * const dialog)
{
  assert(dialog);
  this->setEnabled(false);
  QObject::connect(dialog,&ribi::QtHideAndShowDialog::close_me,this,&ribi::QtHideAndShowDialog::close_child);
  m_show_child = true;
  while (m_show_child)
  {
    dialog->exec();
  }
  this->setEnabled(true);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml