PDA

View Full Version : Create QDialog on button click



ilariari
30th April 2015, 14:05
Hallo, I'm having difficulties doing something very easy (seemingly)...

MyDialog is a subclass of QDialog

.h




class MyDialog : public QDialog
{
Q_OBJECT

public:
explicit MyDialog(QWidget * parent = 0);
~MyDialog(){ }

private:
MyOkButton* m_pOkButton; // MyOkButton is derived from QPushButton
MyCancelButton* m_pCancelButton; // MyCancelButton is derived from QPushButton
QDialogButtonBox* m_pDialogButtonBox;

QVBoxLayout* m_pDialogLayout;


// ================================================== ===================================
// SIGNAL / SLOTS
// ================================================== ===================================
signals:
public slots:

void OnOk_Slot(void);
void OnCancel_Slot(void);
};



.cpp



MyDialog::MyDialog(QWidget * parent)
:
QDialog(parent)
{
m_pOkButton = new MyOkButton(this);
connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(OnOk_Slot()));
m_pOkButton->SetHint(QSize(100, 100)); // SetHint is a method of MyOkButton

m_pCancelButton = new MyCancelButton(QSize(), "", this);
connect(m_pCancelButton, SIGNAL(clicked()), this, SLOT(OnCancel_Slot()));
m_spCancelButton->SetHint(QSize(100, 100)); // SetHint is a method of MyCancelButton

m_pDialogButtonBox = new QDialogButtonBox(this);
m_pDialogButtonBox->addButton(m_pOkButton, QDialogButtonBox::AcceptRole);
m_pDialogButtonBox->addButton(m_pCancelButton, QDialogButtonBox::RejectRole);

m_pDialogLayout = new QVBoxLayout(this);
m_pDialogLayout->addWidget(m_spDialogButtonBox);

setLayout(m_spDialogLayout);
}

void MyDialog::OnOk_Slot(void)
{
accept();
}

void MyDialog::OnCancel_Slot(void)
{
reject();
}



In my main window, the slot related to a certain pushbutton click is:




void MyMainWindow::OnMyButton_slot(bool)
{

MyDialog * l_d = new MyDialog(this);
l_d->setWindowTitle("Title");
l_d->setFixedSize(300, 300);
l_d->exec();

}

[code]

When executing the MyDialog constructor (MyDialog * l_d = new MyDialog(this);) in the QT Creator Application Output area appears "Debugging has finished" and the execution stops.

Is it correct to create and "launch" a dialog in the slot related to a certain button pressure or shall I invoke some other method through the QMetaObject?
Is it correct to pass this (pointer to the QMainWindow) to MyDialog, so that its instance is child of the MainWindow?
Are there best practises to subclass QDialog?

Thank you for giving me some help!

Added after 16 minutes:

It's something related to the use of MyOkButton and MyCancelButton as ok/cancel.

If i use the code below everything ok:

[code]
m_spDialogButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialo gButtonBox::Ok);
QObject::connect(m_spDialogButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
QObject::connect(m_spDialogButtonBox, SIGNAL(rejected()), this, SLOT(reject()));


Added after 15 minutes:

What shall I do to have customized pushbuttons in my dialog?

d_stranz
30th April 2015, 15:48
What shall I do to have customized pushbuttons in my dialog?

What do your custom buttons do that can't be accomplished with standard QPushButtons or by creating the button box, getting the OK and Cancel buttons from it, and customizing them either through code or stylesheets?

The problem might be that you are creating your custom buttons as children of the QDialog, not the QDialogButtonBox. Try creating the button box first, then creating the custom buttons with the button box as their parent. This might cleanly replace then standard buttons.

If that doesn't work, you might try creating the button box, calling setStandardButtons(), retrieving the pointers to the standard buttons you want to replace, removing them, and then adding your custom buttons in their place. To be safe, call deleteLater() on the buttons you are removing to ensure they don't get deleted prematurely but are cleaned up the next time the event loop runs.

ilariari
30th April 2015, 17:22
My custom buttons have an image assigned through .qss file and - as the image associated is circular - an hint.
Your suggestion is interesting, i'll try it on monday. Thank you!

By now i used a workaround: <ok> and <cancel> are QPushButton with image associated through setStyleSheet and the property to be round associated through setFixedSize(QSize(w, h)) - where w=h
I don't have any QDialogButtonBox anymore, <ok> and <cancel> belong to a QHBoxLayout.
In fact, the only important thing, as far as i feel, is that ok is associated with accept() and <cancel> with reject().

Bye, have a good we!!