Results 1 to 20 of 21

Thread: enabling/disabling a QMainWindow?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default enabling/disabling a QMainWindow?

    I'm working with a QMainWindow and would like every widget within it to become disabled. Since we're talking about practically the entire GUI, i'm looking for a way to disable it all in one sweep instead of going through all the children one-by-one (there are a bunch of child widgets!).

    Since QMainWindow is a subclass of QWidget, i was hoping that the setEnabled(bool) and setDisabled(bool) slots would work. Unfortunately, they doesn't seem to.

    Is there a way of doing this simply or am i trying to squeeze blood from a stone?

    thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: enabling/disabling a QMainWindow?

    The methods you mentioned should work. Could you further explain what does "not work" mean?

  3. #3
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enabling/disabling a QMainWindow?

    Sure thing wysota!

    My class is called Gui and inherits QMainWindow. The following code works in the sense that the close event is ignored and the signal is emited correctly. However, the main window does not become disabled.

    Qt Code:
    1. void Gui::closeEvent(QCloseEvent *event)
    2. {
    3. event->ignore();
    4. this->setEnabled(false);
    5. emit someSignalThatDoesStuff();
    6. }
    To copy to clipboard, switch view to plain text mode 

    after calling the setEnabled(false) or setDisabled(true) slots i can still manipulate the gui.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: enabling/disabling a QMainWindow?

    Is this a complete method code? Because if it is, how will you close your window?

    Anyway, this code works correctly for me:
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QCloseEvent>
    4. #include <QLayout>
    5. #include <QPushButton>
    6. #include <QMainWindow>
    7.  
    8. class Wgt : public QMainWindow {
    9. public:
    10. Wgt():QMainWindow(){}
    11. protected:
    12. void closeEvent(QCloseEvent *e){
    13. if(width()<500){
    14. e->ignore();
    15. setDisabled(true);
    16. }
    17. return;
    18. }
    19. };
    20.  
    21. int main(int a, char **b){
    22. QApplication app(a,b);
    23. Wgt mw;
    24. QWidget *w = new QWidget;
    25. QHBoxLayout *l = new QHBoxLayout(w);
    26. l->addWidget(new QPushButton());
    27. l->addWidget(new QPushButton());
    28. mw.setCentralWidget(w);
    29. mw.show();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enabling/disabling a QMainWindow?

    wow..just...wow...it is so weird, the only discernible difference is that ive used Designer to create my gui. But other than that, its basically the same...and yet, using setDisabled(true) does nothing. The signal i emit leads to a slot that does some things before issuing a qApp->quit().

    Can the problem have to do with the fact that i'm using the oh-so-sexy CDE-style (i'm running in unix)? I'm inclined to doubt it though since the interface isn't disabled at all. it would be conceivable that the interface would be visually enabled but functionally disabled..but this does not seem to be the case. I've had this problem before in other projects (running in windows) where i've wanted to disable entire windows and have, until now, just assumed that it wasn't possible and that i had to manually disable every child widget.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: enabling/disabling a QMainWindow?

    I don't think this is the case but you may just try with another widget style and see for yourself.

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enabling/disabling a QMainWindow?

    Quote Originally Posted by TheRonin View Post
    The signal i emit leads to a slot that does some things before issuing a qApp->quit().
    Have you tried commenting out this signal emission? BTW If you issue a "qApp->quit()" your app should actually get closed and not disabled or am I missing something?
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #8
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enabling/disabling a QMainWindow?

    hey guys, thanks for your help thus far...i hope there's a solution right around the corner!

    Quote Originally Posted by wysota View Post
    I don't think this is the case but you may just try with another widget style and see for yourself.
    I get the desired behavior if instead of disabling the QMainWindow i disable the root widget (a QWidget called centralwidget by the designer). I would of course like to not need to do that sort of work-around instead of working with the mainwindow directly. Had you been able to reproduce my problem i would have been tempted to call it a bug of sorts but since i seem to be the only one affected, maybe it's due to a problem with my Qt installation??

    Quote Originally Posted by fullmetalcoder View Post
    Have you tried commenting out this signal emission? BTW If you issue a "qApp->quit()" your app should actually get closed and not disabled or am I missing something?
    I just tried skipping the signal emission like you suggested but the mainwindow still doesn't get disabled (unless i disable the centralwidget). You're absolutely right about the quit() terminating the application but there is a significant amount of time between the emission of the signal and the actual call to quit() (i do a bunch of stuff in-between that takes time...like closing tcp connections and saving things to file.)

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: enabling/disabling a QMainWindow?

    Could you prepare a minimal compilable example reproducing the problem?

  10. #10
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enabling/disabling a QMainWindow?

    Quote Originally Posted by wysota View Post
    Could you prepare a minimal compilable example reproducing the problem?
    Sorry for the delayed response...here comes the 'minimal' example. When run on my system the main window does not become disabled unless i use the centralwidget-object

    Qt Code:
    1. #include <iostream>
    2. #include <QApplication>
    3. #include <QMainWindow>
    4. #include <QWidget>
    5. #include <QPushButton>
    6. #include <QString>
    7. //#include <QRect>
    8. //#include <QSize>
    9. //
    10. using namespace std;
    11. //
    12. class Gui : public QMainWindow
    13. {
    14. Q_OBJECT
    15. public:
    16. Gui( QWidget * parent = 0 );
    17. private slots:
    18. void disableMe();
    19. private:
    20. QWidget *centralwidget;
    21. QPushButton *pushButton;
    22. };
    23.  
    24. int main(int argc, char ** argv)
    25. {
    26. QApplication app( argc, argv );
    27. Gui gui;
    28. gui.show();
    29. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    30. return app.exec();
    31. }
    32.  
    33. Gui::Gui( QWidget * parent ) : QMainWindow(parent)
    34. {
    35. this->setObjectName(QString::fromUtf8("MainWindow"));
    36. this->setWindowTitle("main window test");
    37. centralwidget = new QWidget(this);
    38. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    39. pushButton = new QPushButton(centralwidget);
    40. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    41. pushButton->setGeometry(QRect(110, 50, 101, 41));
    42. pushButton->setText("disable?");
    43. this->setCentralWidget(centralwidget);
    44.  
    45. QSize size(322, 149);
    46. size = size.expandedTo(this->minimumSizeHint());
    47. this->resize(size);
    48.  
    49. connect(pushButton, SIGNAL(clicked()),
    50. this, SLOT(disableMe()));
    51. }
    52.  
    53. void Gui::disableMe()
    54. {
    55. cout << "trying to disable the mainwindow...";
    56. this->setDisabled(true);
    57. cout << "success?" << endl;
    58. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: enabling/disabling a QMainWindow?

    Seems to work fine for me (Linux+4.2.3+Plastique). Works well on CDE and Cleanlooks as well.

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: enabling/disabling a QMainWindow?

    And works for me on Qt4.2.3/Win with both, winxp and windows styles too.
    J-P Nurmi

Similar Threads

  1. QMainWindow Maximization Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 15th March 2007, 13:30
  2. How do I add a QGridlayout in a QMainwindow?
    By Teuniz in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 10:18
  3. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 01:15
  4. Insert separate QToolBar into QMainWindow
    By YuriyRusinov in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 10:37
  5. Replies: 18
    Last Post: 22nd February 2006, 20:51

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
  •  
Qt is a trademark of The Qt Company.