Adding to Ginsengelf's reply, I have a very simple helper class derived from QDialog which allows hosting of any widget inside of a QDialog. I create the widget first, hook up all of its signals, then add it to the helper dialog class and call exec(). This saves me the work of having to repeat the same code over and over for every custom dialog, and allows me to use the same widgets in other, non-dialog parts of the UI. Maybe this can help in your case, especially if you devise a custom QWidget class that builds itself from XML and provides a generic set of signals that are emitted when things are edited.
I assume you realize that Qt's UI files are just fancier versions of the XML file that you have devised, right? And that there is a QUiLoader class that will load and build a GUI from a UI file at runtime?
/*
QtWidgetHostingDialog
This is a generic QDialog class designed to host a single composite widget.
The dialog has a vertical layout with the hosted widget on top and a
standard QDialogButtonBox below. The button box's accepted and rejected
signals are connected to the QDialog accept and reject slots.
The pointer to the hosted widget is passed during construction and
ownership is passed to the QDialog. The hosted widget may not be replaced
once the dialog is constructed.
Convenience methods are provided to retrieve the hosted widget and button box
pointers.
Example:
MyWidget * pWidget = new MyWidget;
// configure pWidget, connect signals, etc.
CQtWidgetHostingDialog dlg( pWidget, this );
dlg.setWindowTitle( "Edit My Parameters" );
if ( QDialog::Accepted == dlg.exec() )
// retrieve edited information from pWidget
*/
#include <QDialog>
class CQtWidgetHostingDialog
: public QDialog{
Q_OBJECT
public:
CQtWidgetHostingDialog
( QWidget * pHostedWidget,
QWidget * pParent
= nullptr
);
virtual ~CSAQtWidgetHostingDialog();
protected:
QWidget * mpHostedWidget
= nullptr;
};
/*
QtWidgetHostingDialog
This is a generic QDialog class designed to host a single composite widget.
The dialog has a vertical layout with the hosted widget on top and a
standard QDialogButtonBox below. The button box's accepted and rejected
signals are connected to the QDialog accept and reject slots.
The pointer to the hosted widget is passed during construction and
ownership is passed to the QDialog. The hosted widget may not be replaced
once the dialog is constructed.
Convenience methods are provided to retrieve the hosted widget and button box
pointers.
Example:
MyWidget * pWidget = new MyWidget;
// configure pWidget, connect signals, etc.
CQtWidgetHostingDialog dlg( pWidget, this );
dlg.setWindowTitle( "Edit My Parameters" );
if ( QDialog::Accepted == dlg.exec() )
// retrieve edited information from pWidget
*/
#include <QDialog>
class QDialogButtonBox;
class CQtWidgetHostingDialog : public QDialog
{
Q_OBJECT
public:
CQtWidgetHostingDialog( QWidget * pHostedWidget, QWidget * pParent = nullptr );
virtual ~CSAQtWidgetHostingDialog();
QWidget * hostedWidget() const;
QDialogButtonBox * buttonBox() const;
protected:
QWidget * mpHostedWidget = nullptr;
QDialogButtonBox * mpButtonBox = nullptr;
};
To copy to clipboard, switch view to plain text mode
#include "QtWidgetHostingDialog.h"
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <cassert>
CQtWidgetHostingDialog
::CQtWidgetHostingDialog( QWidget * pHostedWidget,
QWidget * pParent
/*= nullptr */ ) , mpHostedWidget( pHostedWidget )
{
assert( mpHostedWidget != nullptr );
setLayout( pLayout );
pLayout->addWidget( mpHostedWidget );
pLayout->addWidget( mpButtonBox );
connect( mpButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
connect( mpButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
}
CQtWidgetHostingDialog::~CQtWidgetHostingDialog()
{
}
QWidget * CQtWidgetHostingDialog
::hostedWidget() const {
return mpHostedWidget;
}
{
return mpButtonBox;
}
#include "QtWidgetHostingDialog.h"
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <cassert>
CQtWidgetHostingDialog::CQtWidgetHostingDialog( QWidget * pHostedWidget, QWidget * pParent /*= nullptr */ )
: QDialog( pParent )
, mpHostedWidget( pHostedWidget )
{
assert( mpHostedWidget != nullptr );
QVBoxLayout * pLayout = new QVBoxLayout;
setLayout( pLayout );
pLayout->addWidget( mpHostedWidget );
mpButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
pLayout->addWidget( mpButtonBox );
connect( mpButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
connect( mpButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
}
CQtWidgetHostingDialog::~CQtWidgetHostingDialog()
{
}
QWidget * CQtWidgetHostingDialog::hostedWidget() const
{
return mpHostedWidget;
}
QDialogButtonBox * CQtWidgetHostingDialog::buttonBox() const
{
return mpButtonBox;
}
To copy to clipboard, switch view to plain text mode
Access to the QDialogButtonBox is provided so you can customize by adding additional buttons and connect to their signals.
Bookmarks