Results 1 to 11 of 11

Thread: Dynamically Loading a QMainWindow?

  1. #1
    Join Date
    Jul 2006
    Location
    Seattle
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Dynamically Loading a QMainWindow?

    First, to describe my setup, I'm using Qt 4.1 patched in with VS2005 on Windows XP.

    I've been using Qt for a couple day now, and have managed to run into troubles early.
    I'm trying to dynamically load my Qt Designer-created QMainWindow, much like the Calculator Builder example does with a QWidget. I can compile and use the calculator perfectly.
    I figure changing the parent class and a couple ctor items should work for a QMainWindow...right?

    Here's essentially what I'm working with:

    Qt Code:
    1. class MainForm : public QMainWindow
    2. {
    3. public:
    4. MainForm(QMainWindow *parent = 0);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainForm::MainForm(QMainWindow *parent) : QMainWindow(parent)
    2. {
    3. QUiLoader loader;
    4. QFile file(":/forms/mainwindowgui.ui");
    5. file.open(QFile::ReadOnly);
    6. QWidget *formWidget = loader.load(&file, this);
    7. file.close();
    8.  
    9. QLayout *layout = new QVBoxLayout;
    10. layout->addWidget(formWidget);
    11. setLayout(layout);
    12. }
    To copy to clipboard, switch view to plain text mode 

    And the main.cpp remains the same as the calculatorbuilder example.

    This seems simple, but clearly I'm missing something crucial. Any input is appreciated. Thanks.

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

    Default Re: Dynamically Loading a QMainWindow?

    Are you sure you added the /forms/mainwindow.ui file to your resources system (and that it is accessible under that name)?

  3. #3
    Join Date
    Jul 2006
    Location
    Seattle
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically Loading a QMainWindow?

    Yes on both.

    Here's what my mainwindowbuilder.qrc file looks like
    Qt Code:
    1. <!DOCTYPE RCC><RCC version="1.0">
    2. <qresource prefix="/forms">
    3. <file>mainwindowgui.ui</file>
    4. </qresource>
    5. </RCC>
    To copy to clipboard, switch view to plain text mode 

    and I might as well reveal my main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. Q_INIT_RESOURCE(mainwindowbuilder);
    5. MainForm winMain;
    6. winMain.show();
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    I've verified the file being loaded by simply adding a
    formwidget->show()
    in my class file, and the form does work. However, not actually being associated with the class yet, it doesn't have any use.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically Loading a QMainWindow?

    Quote Originally Posted by natron
    Qt Code:
    1. MainForm::MainForm(QMainWindow *parent) : QMainWindow(parent)
    2. {
    3. QUiLoader loader;
    4. QFile file(":/forms/mainwindowgui.ui");
    5. file.open(QFile::ReadOnly);
    6. QWidget *formWidget = loader.load(&file, this);
    7. file.close();
    8.  
    9. QLayout *layout = new QVBoxLayout;
    10. layout->addWidget(formWidget);
    11. setLayout(layout);
    12. }
    To copy to clipboard, switch view to plain text mode 
    You should use QMainWindow::setCentralWidget() instead the layout, since main windows have their own layout.

    What widget is mainwindowgui.ui based on? QMainWindow or QWidget?

  5. #5
    Join Date
    Jul 2006
    Location
    Seattle
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically Loading a QMainWindow?

    I changed the layout jargon to setCentralWidget. Unfortunately, that didn't make a difference.

    I created the form by choosing "Main Window" from the New Form dialog in Qt Designer. If that decides what its "based on", then QMainWindow is it.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically Loading a QMainWindow?

    Quote Originally Posted by natron
    I created the form by choosing "Main Window" from the New Form dialog in Qt Designer. If that decides what its "based on", then QMainWindow is it.
    This might be the problem, you are trying to put a QMainWindow inside QMainWindow.

  7. #7
    Join Date
    Jul 2006
    Location
    Seattle
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically Loading a QMainWindow?

    Good point.

    I changed the parent class to QWidget, but like my story goes, it still doesn't work.

    Since this is no longer a QMainWindow subclass, the setCentralWidget is no longer available. With or without the original layout code, my GUI isn't showing up. One noted difference though, the blank form that shows up with the layout code is small, slightly larger than the window decroations. Removing the layout code creates a huge window that feels close to a main window...I swear the solution must be near.

    As a side, how do you experts start your Qt applications? This seems like a natural way to do it; it's similar to how I've done this type of thing in Gtk.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically Loading a QMainWindow?

    Quote Originally Posted by natron
    Since this is no longer a QMainWindow subclass, the setCentralWidget is no longer available.
    This time you try to put QMainWindow on QWidget.

    As you can see:
    Qt Code:
    1. QMainWindow::QMainWindow(QWidget *parent, Qt::WFlags flags)
    2. : QWidget(*(new QMainWindowPrivate()), parent, flags | Qt::Window /* <- note this flag */ )
    3. {
    4. d_func()->init();
    5. }
    To copy to clipboard, switch view to plain text mode 
    QMainWindow is always a top level window. You have to either change your .ui file or use a different approach.

    As a side, how do you experts start your Qt applications?
    I prefer to use uic instead of QUiLoader. If you store .ui files in resources it doesn't make much difference, except that the code generated by uic is smaller and runs faster.

  9. The following user says thank you to jacek for this useful post:

    natron (20th July 2006)

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

    Default Re: Dynamically Loading a QMainWindow?

    Quote Originally Posted by natron
    Since this is no longer a QMainWindow subclass, the setCentralWidget is no longer available.
    Maybe you should create an instance of QMainWindow and then use setCentralWidget to set your widget as its main part? I mean something like:

    Qt Code:
    1. int main(int argc, char **argv){
    2. QApplication app(argc, argv);
    3. // ...
    4. mw.setCentralWidget(createYourWidgetHere);
    5. //...
    6. mw.show();
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 


    As a side, how do you experts start your Qt applications?
    I don't exactly understand what you mean... How do we execute applications or how do we implement them?

    If the former then either from the command line, KDevelop or by clicking on an icon in Konqueror. I think all people do it in one of these ways...

    If the latter, then I start by writing main.cpp with contents such as the one above, then implement my main ui, subclass it to create the main window class and then I check if it works fine. Afterwards I begin to implement the functionality.

    And I don't use dynamically loaded forms

  11. The following user says thank you to wysota for this useful post:

    natron (20th July 2006)

  12. #10
    Join Date
    Jul 2006
    Location
    Seattle
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically Loading a QMainWindow?

    Hey, thanks you two for your help. I got it working so far.

    I realized my blunder - I basically had the right code, just in the wrong place.

    My main now loads the .ui file, followed by
    Qt Code:
    1. MainForm mfMain;
    2. mfMain.setCentralWidget(formWidget);
    To copy to clipboard, switch view to plain text mode 

    where the ctor for MainForm goes like so
    Qt Code:
    1. MainForm::MainForm(QWidget *parent) : QMainWindow(parent)
    2. {
    3. //...
    4. }
    To copy to clipboard, switch view to plain text mode 

    This makes a lot of sense too; it works like wysota's post above.

    Any idea why Gtk's (using Glade) method of interface design explicitly promotes dynamic interface loading, while in Qt land the paradigm focuses on the static method? I can see advantages and restrictions in both, but neither seems clearly better.

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

    Default Re: Dynamically Loading a QMainWindow?

    Quote Originally Posted by natron
    Any idea why Gtk's (using Glade) method of interface design explicitly promotes dynamic interface loading, while in Qt land the paradigm focuses on the static method? I can see advantages and restrictions in both, but neither seems clearly better.
    <flame mode>
    Maybe because Qt is better?
    </flame mode>

    Qt is object oriented and focuses on using classes, also for things like the GUI, whereas Gtk is not object oriented and doesn't care about classes, so it doesn't matter where it creates its bunch of variables responsible for controlling the user interface. Of course that's my personal opinion

Similar Threads

  1. Switching static to dynamic ui loading
    By s_a_white in forum Newbie
    Replies: 4
    Last Post: 26th June 2006, 15:57
  2. Insert separate QToolBar into QMainWindow
    By YuriyRusinov in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 10:37
  3. Replies: 18
    Last Post: 22nd February 2006, 20:51
  4. QSA: loading scripts at runtime
    By seneca in forum Qt Programming
    Replies: 0
    Last Post: 15th February 2006, 15:19

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.