Thank you for your ideas.
1) & 4) The reason I emit a signal on accept() is because I need to get the index of the spinbox that was selected. If there's a better way to do this, let me know!
2) Why would this be a bad thing? I need to free the memory before the dialog ceases to exist, right?
3) Yes, that was just a mistake.
Revised code that still produces a seg fault:
#ifndef GENERATENEWDIALOG_H
#define GENERATENEWDIALOG_H
#include <QDialog>
#include <QSpinBox>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QPushButton>
#include <QDebug>
class GenerateNewDialog
: public QDialog{
Q_OBJECT
private:
void setupInterface();
void setupActions();
int m_difficulty;
public:
explicit GenerateNewDialog
(QWidget *parent
= 0);
virtual ~GenerateNewDialog();
signals:
void difficultySelected(int difficulty);
private slots:
virtual void accept();
virtual void reject();
public slots:
};
#endif // GENERATENEWDIALOG_H
#ifndef GENERATENEWDIALOG_H
#define GENERATENEWDIALOG_H
#include <QDialog>
#include <QSpinBox>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QPushButton>
#include <QDebug>
class GenerateNewDialog : public QDialog
{
Q_OBJECT
private:
void setupInterface();
void setupActions();
QVBoxLayout * m_mainLayout;
QHBoxLayout * m_firstSetting;
QLabel * m_label1;
QSpinBox * m_difficultyPicker;
QPushButton * m_cancel, * m_start;
QDialogButtonBox *buttonBox;
int m_difficulty;
public:
explicit GenerateNewDialog(QWidget *parent = 0);
virtual ~GenerateNewDialog();
signals:
void difficultySelected(int difficulty);
private slots:
virtual void accept();
virtual void reject();
public slots:
};
#endif // GENERATENEWDIALOG_H
To copy to clipboard, switch view to plain text mode
#include "generatenewdialog.h"
GenerateNewDialog
::GenerateNewDialog(QWidget *parent
) :{
setupInterface();
setupActions();
}
//----------------------------------------------------------------------------
// Constructor & destructor.
GenerateNewDialog::~GenerateNewDialog(void) {
// Is automatically deleted, because it's a child of of the main widget.
//delete m_mainLayout;
delete m_firstSetting;
delete m_label1;
delete m_difficultyPicker;
delete m_cancel;
delete m_start;
}
//----------------------------------------------------------------------------
// Private slots.
/**
* The dialog was accepted. Emit the newGame() signal.
*/
void GenerateNewDialog::accept(void) {
m_difficulty = m_difficultyPicker->value();
emit difficultySelected(m_difficulty);
}
//----------------------------------------------------------------------------
// Private methods.
/**
* Create the UI.
*/
void GenerateNewDialog::setupInterface(void) {
setContextMenuPolicy(Qt::NoContextMenu);
setModal(true);
// Configure layouts.
m_mainLayout->addLayout(m_firstSetting);
// Set up the buttons.
this->buttonBox->setOrientation(Qt::Horizontal);
m_start->setDefault(true);
m_mainLayout->addWidget(buttonBox);
// Difficulty label.
m_label1->setText(tr("Difficulty level"));
m_firstSetting->addWidget(m_label1);
m_firstSetting->setAlignment(m_label1, Qt::AlignLeft);
// Difficulty spinbox.
m_difficultyPicker
= new QSpinBox(this);
m_difficultyPicker->setMinimum(1);
m_difficultyPicker->setMaximum(5);
m_difficultyPicker->setSingleStep(1);
m_difficultyPicker->setValue(1);
m_firstSetting->addWidget(m_difficultyPicker);
m_firstSetting->setAlignment(m_difficultyPicker, Qt::AlignRight);
}
void GenerateNewDialog::setupActions(){
// Connect the signals from the buttonbox to the NewGameDialog.
connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}
void GenerateNewDialog::reject(){
qDebug() << "GenerateNewDialog received reject signal.";
close();
}
#include "generatenewdialog.h"
GenerateNewDialog::GenerateNewDialog(QWidget *parent) :
QDialog(parent, Qt::Sheet)
{
setupInterface();
setupActions();
}
//----------------------------------------------------------------------------
// Constructor & destructor.
GenerateNewDialog::~GenerateNewDialog(void) {
// Is automatically deleted, because it's a child of of the main widget.
//delete m_mainLayout;
delete m_firstSetting;
delete m_label1;
delete m_difficultyPicker;
delete m_cancel;
delete m_start;
}
//----------------------------------------------------------------------------
// Private slots.
/**
* The dialog was accepted. Emit the newGame() signal.
*/
void GenerateNewDialog::accept(void) {
m_difficulty = m_difficultyPicker->value();
emit difficultySelected(m_difficulty);
QDialog::accept();
}
//----------------------------------------------------------------------------
// Private methods.
/**
* Create the UI.
*/
void GenerateNewDialog::setupInterface(void) {
setContextMenuPolicy(Qt::NoContextMenu);
setModal(true);
// Configure layouts.
m_mainLayout = new QVBoxLayout(this);
m_firstSetting = new QHBoxLayout();
m_mainLayout->addLayout(m_firstSetting);
// Set up the buttons.
this->buttonBox = new QDialogButtonBox(this);
this->buttonBox->setOrientation(Qt::Horizontal);
m_cancel = new QPushButton(tr("&Cancel"));
this->buttonBox->addButton(m_cancel, QDialogButtonBox::RejectRole);
m_start = new QPushButton(tr("&Start!"));
m_start->setDefault(true);
this->buttonBox->addButton(m_start, QDialogButtonBox::AcceptRole);
m_mainLayout->addWidget(buttonBox);
// Difficulty label.
m_label1 = new QLabel(this);
m_label1->setText(tr("Difficulty level"));
m_firstSetting->addWidget(m_label1);
m_firstSetting->setAlignment(m_label1, Qt::AlignLeft);
// Difficulty spinbox.
m_difficultyPicker = new QSpinBox(this);
m_difficultyPicker->setMinimum(1);
m_difficultyPicker->setMaximum(5);
m_difficultyPicker->setSingleStep(1);
m_difficultyPicker->setValue(1);
m_firstSetting->addWidget(m_difficultyPicker);
m_firstSetting->setAlignment(m_difficultyPicker, Qt::AlignRight);
}
void GenerateNewDialog::setupActions(){
// Connect the signals from the buttonbox to the NewGameDialog.
connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}
void GenerateNewDialog::reject(){
qDebug() << "GenerateNewDialog received reject signal.";
QDialog::reject();
close();
}
To copy to clipboard, switch view to plain text mode
Bookmarks