×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    QDialog and QDialogButtonBox problem

    I have some problems with a QDialog that I open from a QMenu. The QDialog shows up fine and in the QDialog I have a QDialogButtonBox with a button to Close the QDialog. BUT if I close the QDialog and then open it from the QMenu again, it will show up but the button from the QDialogButtonBox will not show up. Instead the buttons from the QMainWindow will show but they are grayed out.

    How can I get the QDialog buttons to show every time? Maybe I have some problems with setting focus on the QDialog? I really can't see what I'm doing wrong here, the QDialog works fine if I open it from the QMainWindow directly without the QMenu.

    It's not much code that I use, you can try it yourself. This is my code:

    In QMainWindow I use the following to create the menu:

    Code:
    QAction *menuButton = new QAction("Menu", this);
    menuButton->setSoftKeyRole(QAction::PositiveSoftKey);
    
    QMenu *menu = new QMenu(this);
    menuButton->setMenu(menu);
    
    QAction *popup = new QAction("Show popup",this);
    connect(popup, SIGNAL(triggered()), this, SLOT(showPopup()));
    menu->addAction(popup);
    
    addAction(menuButton);
    This shows the QDialog:

    Code:
    void MyMainWindow::showPopup(){
    TestDialog *test = new TestDialog(this);
    test->setAttribute(Qt::WA_DeleteOnClose);
    test->show();
    }
    This is the TestDialog:

    Code:
    TestDialog::TestDialog(QWidget *parent)
    : QDialog(parent)
    {
    ui.setupUi(this);
    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect rect = desktopWidget->availableGeometry();
    this->setFixedWidth(rect.width());
    }

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QDialog and QDialogButtonBox problem

    Normally you launch dialog using QDialog::exec(). Also you have a memory leak. Strictly speaking memory has not leaked as you still have reference to the object, but it will be destroyed at the end of application execution.
    Things you do TestDialog constructor are also quite strange. It's enough to use QWidget::showMaximized(), when you show dialog.

  3. #3
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: QDialog and QDialogButtonBox problem

    Quote Originally Posted by divanov View Post
    Normally you launch dialog using QDialog::exec(). Also you have a memory leak. Strictly speaking memory has not leaked as you still have reference to the object, but it will be destroyed at the end of application execution.
    Things you do TestDialog constructor are also quite strange. It's enough to use QWidget::showMaximized(), when you show dialog.
    Thanks for your answer!

    About the memory leak, I'm using test->setAttribute(Qt::WA_DeleteOnClose) and from what I understand this will delete the QDialog when I close it. Isn't that right?

    About the strange constructor, QWidget::showMaximized() will show the dialog maximized, I just want to show it in the bottom of the screen, so this was my solution.


    Using exec I still have the same problem with the buttons. Any ideas?

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QDialog and QDialogButtonBox problem

    Quote Originally Posted by stegemann View Post
    Thanks for your answer!

    About the memory leak, I'm using test->setAttribute(Qt::WA_DeleteOnClose) and from what I understand this will delete the QDialog when I close it. Isn't that right?

    About the strange constructor, QWidget::showMaximized() will show the dialog maximized, I just want to show it in the bottom of the screen, so this was my solution.

    Using exec I still have the same problem with the buttons. Any ideas?
    Yes, you are correct. I've missed the attribute.
    I don't know what availableGeometry returns on Symbian, but on all other platforms it's exactly the same as maximized.
    http://doc.trolltech.com/4.6/qdeskto...ilableGeometry
    Did you try your application on the desktop?

  5. #5
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: QDialog and QDialogButtonBox problem

    Quote Originally Posted by divanov View Post
    Yes, you are correct. I've missed the attribute.
    I don't know what availableGeometry returns on Symbian, but on all other platforms it's exactly the same as maximized.
    http://doc.trolltech.com/4.6/qdeskto...ilableGeometry
    Did you try your application on the desktop?
    Yes, but I use this->setFixedWidth(rect.width()) to make the QDialog expand in width but not in height so thats why I'm not using showMaximized, and that seems to work good. But the problem is still with the buttons, I have tested this on Symbian Emulator, Nokia 5800 phone, Nokia N97 phone. Any ideas what can be wrong with the buttons?

  6. #6
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QDialog and QDialogButtonBox problem

    Quote Originally Posted by stegemann View Post
    Any ideas what can be wrong with the buttons?
    The truth is whatever could be wrong. Could you attach full source code here?

  7. #7
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: QDialog and QDialogButtonBox problem

    Hi!

    This is my small test project:

    http://share1t.com/oqcfmd

    Please say if you rather see that I post all code here. Thanks really much!

  8. #8
    Regular Contributor
    Join Date
    Oct 2008
    Location
    Oslo, Norway
    Posts
    329

    Re: QDialog and QDialogButtonBox problem

    Hi,

    Thanks for the complete project. This smells a bit of a bug; if you add a small delay for displaying the dialog:

    Code:
    	QAction *popup = new QAction("Show popup",this);
            connect(popup, SIGNAL(triggered()), this, SLOT(showPopupDelayed()));
    	menu->addAction(popup);
    
    void MyTest::showPopupDelayed(){
        QTimer::singleShot(100, this, SLOT(showPopup()));
    }
    
    void MyTest::showPopup() {
        TestDialog *test = new TestDialog(this);
        test->setAttribute(Qt::WA_DeleteOnClose);
        test->show();
    }
    Then it works every time.

  9. #9
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QDialog and QDialogButtonBox problem

    Is there any chance to know if this is Symbian specific issue or not?

  10. #10
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: QDialog and QDialogButtonBox problem

    Nice, worked perfect!! :-)

    Maybe I should post it as a bug?

    Thanks to you all!

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    48

    Re: QDialog and QDialogButtonBox problem

    For the record, I think you should be deleting the dialog, somewhere.

    If you want it to be non-modal, put the pointer as a member in the class and then delete it when it's done (set the pointer to NULL and test as appropriate to insure you're not creating multiple instances.)

    Otherwise, if you use modal, create it on the stack (as a local), rather than as a pointer. It will get deleted when it goes out of scope.

    Small memory footprint device like this, it's better to create and destroy dialogs and temporary windows, rather than having them hang around between invocations.

  12. #12
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: QDialog and QDialogButtonBox problem

    Quote Originally Posted by rickbsgu View Post
    For the record, I think you should be deleting the dialog, somewhere.

    If you want it to be non-modal, put the pointer as a member in the class and then delete it when it's done (set the pointer to NULL and test as appropriate to insure you're not creating multiple instances.)

    Otherwise, if you use modal, create it on the stack (as a local), rather than as a pointer. It will get deleted when it goes out of scope.

    Small memory footprint device like this, it's better to create and destroy dialogs and temporary windows, rather than having them hang around between invocations.
    I think test->setAttribute(Qt::WA_DeleteOnClose) will delete the QDialog when I close it?

  13. #13
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QDialog and QDialogButtonBox problem

    Quote Originally Posted by stegemann View Post
    I think test->setAttribute(Qt::WA_DeleteOnClose) will delete the QDialog when I close it?
    Yes, it will.

  14. #14
    Regular Contributor
    Join Date
    Oct 2008
    Location
    Oslo, Norway
    Posts
    329

    Re: QDialog and QDialogButtonBox problem

    Hi again,

    This definitely is a Symbian/S60-only issue related to softkey handling. I took a closer look a noticed that it's caused by the fact that the app defines its own PositiveSoftKey. You can fix it by using the menu bar already provided by QMainWindow, no delay hacks needed:

    Code:
    #include <QMenuBar>
    
    MyTest::MyTest(QWidget *parent)
        : QMainWindow(parent)
    {
    	ui.setupUi(this);
            // createMenus();  // not needed
            menuBar()->addAction(tr("Show"), this, SLOT(showPopup()));
    }
    I'd always recommend using menuBar() instead of defining your own positive softkey. If you want to change the softkey label to something else than 'Options' (which is localized, by the way), that is possible as well, see here.

  15. #15
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: QDialog and QDialogButtonBox problem

    Thanks! I think I need to check my solution to use the menuBar instead. Using my own way seems like it always gives these kind of bugs..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×