Results 1 to 3 of 3

Thread: Create QDialog on button click

  1. #1
    Join Date
    Mar 2015
    Posts
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Create QDialog on button click

    Hallo, I'm having difficulties doing something very easy (seemingly)...

    MyDialog is a subclass of QDialog

    .h

    Qt Code:
    1. class MyDialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MyDialog(QWidget * parent = 0);
    7. ~MyDialog(){ }
    8.  
    9. private:
    10. MyOkButton* m_pOkButton; // MyOkButton is derived from QPushButton
    11. MyCancelButton* m_pCancelButton; // MyCancelButton is derived from QPushButton
    12. QDialogButtonBox* m_pDialogButtonBox;
    13.  
    14. QVBoxLayout* m_pDialogLayout;
    15.  
    16.  
    17. // =====================================================================================
    18. // SIGNAL / SLOTS
    19. // =====================================================================================
    20. signals:
    21. public slots:
    22.  
    23. void OnOk_Slot(void);
    24. void OnCancel_Slot(void);
    25. };
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. MyDialog::MyDialog(QWidget * parent)
    2. :
    3. QDialog(parent)
    4. {
    5. m_pOkButton = new MyOkButton(this);
    6. connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(OnOk_Slot()));
    7. m_pOkButton->SetHint(QSize(100, 100)); // SetHint is a method of MyOkButton
    8.  
    9. m_pCancelButton = new MyCancelButton(QSize(), "", this);
    10. connect(m_pCancelButton, SIGNAL(clicked()), this, SLOT(OnCancel_Slot()));
    11. m_spCancelButton->SetHint(QSize(100, 100)); // SetHint is a method of MyCancelButton
    12.  
    13. m_pDialogButtonBox = new QDialogButtonBox(this);
    14. m_pDialogButtonBox->addButton(m_pOkButton, QDialogButtonBox::AcceptRole);
    15. m_pDialogButtonBox->addButton(m_pCancelButton, QDialogButtonBox::RejectRole);
    16.  
    17. m_pDialogLayout = new QVBoxLayout(this);
    18. m_pDialogLayout->addWidget(m_spDialogButtonBox);
    19.  
    20. setLayout(m_spDialogLayout);
    21. }
    22.  
    23. void MyDialog::OnOk_Slot(void)
    24. {
    25. accept();
    26. }
    27.  
    28. void MyDialog::OnCancel_Slot(void)
    29. {
    30. reject();
    31. }
    To copy to clipboard, switch view to plain text mode 

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

    [code]

    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:

    Qt Code:
    1. m_spDialogButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    2. QObject::connect(m_spDialogButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    3. QObject::connect(m_spDialogButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
    To copy to clipboard, switch view to plain text mode 


    Added after 15 minutes:


    What shall I do to have customized pushbuttons in my dialog?
    Last edited by ilariari; 30th April 2015 at 15:05.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,715
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Create QDialog on button click

    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.

  3. #3
    Join Date
    Mar 2015
    Posts
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Create QDialog on button click

    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!!

Similar Threads

  1. Replies: 15
    Last Post: 30th October 2012, 08:06
  2. create a QDialog from a QDialog
    By hichemnho in forum Newbie
    Replies: 5
    Last Post: 5th March 2012, 22:48
  3. Disable button click
    By "BumbleBee" in forum Newbie
    Replies: 19
    Last Post: 30th March 2011, 14:16
  4. How can I know which button click.
    By electronicboy in forum Qt Programming
    Replies: 10
    Last Post: 4th October 2009, 15:27
  5. QDialog and default button.
    By hicpalm in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2008, 12:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.