+ Reply to Thread
Results 1 to 9 of 9

Thread: How to initiate a form based on QUiLoader

  1. #1
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default How to initiate a form based on QUiLoader

    Hi all

    I'm relatively new to both C++ and QT and I am trying to create an application which dynamically loads a form based on what is returned from a query to a database. Below is the code I am using to try and 'run' the form:

    Qt Code:
    1. ItemAdd item;
    2. item.show();
    To copy to clipboard, switch view to plain text mode 

    This however doesn't seem to do anything. The contents of ItemAdd.cpp are as follows:
    Qt Code:
    1. #include "itemadd.h"
    2.  
    3. ItemAdd::ItemAdd(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. QUiLoader builder;
    7. QFile file("form.ui");
    8. file.open(QFile::ReadOnly);
    9. QWidget *myWidget = builder.load(&file, this);
    10. file.close();
    11.  
    12. QPushButton *tmpButton = new QPushButton;
    13. tmpButton = qFindChild<QPushButton*>(this,"addNewItemButton");
    14. QVBoxLayout *layout = new QVBoxLayout;
    15. layout->addWidget(myWidget);
    16. setLayout(layout);
    17. // connect(tmpButton,SIGNAL(clicked()),this,SLOT(slotAddItem()));
    18. }
    19. void ItemAdd::slotAddItem() {
    20. // Code to be added later;
    21. }
    22.  
    23. ItemAdd::~ItemAdd() {
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    I have based both my header and cpp file on the defaults for a QDialog form created by Qt Creator. Below is my header file:

    Qt Code:
    1. #ifndef ITEMADD_H
    2. #define ITEMADD_H
    3. #include <QtUiTools>
    4. #include <QtGui>
    5. #include <QWidget>
    6.  
    7. namespace Ui {
    8. class ItemAdd;
    9. }
    10.  
    11. class ItemAdd : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit ItemAdd(QWidget *parent = 0);
    17. ~ItemAdd();
    18. void slotAddItem();
    19. QString uifile;
    20.  
    21. private:
    22. Ui::ItemAdd *ui;
    23. };
    24.  
    25. #endif // ITEMADD_H
    To copy to clipboard, switch view to plain text mode 

    Many thanks for any help you can offer, I've looked through some documentation but can't seem to find what I need.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to initiate a form based on QUiLoader

    what is defined (which widgets) in your ui form?
    If its an empty widget, you will only see and empty widget on your empty widget.
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,939
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to initiate a form based on QUiLoader

    And the widget is created on the stack. You might have deleted it already before it was able to show.

  4. #4
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to initiate a form based on QUiLoader

    At the moment I have a single QPushButton on the form for use in testing.


    Added after 6 minutes:


    Quote Originally Posted by tbscope View Post
    And the widget is created on the stack. You might have deleted it already before it was able to show.
    Hi, I don't quite understand what created on the stack means, how would I make sure it isn't deleted?
    Last edited by peterjb31; 21st December 2010 at 17:44.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,368
    Thanks
    19
    Thanked 992 Times in 903 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to initiate a form based on QUiLoader

    As I see it, all needed widgets are created on the heap, only the builder is created on the stack. Which is fine.
    But are you sure the path to the ui file is correct? I don't think so. Debug what
    Qt Code:
    1. file.open(QFile::ReadOnly);
    To copy to clipboard, switch view to plain text mode 
    says. Remember the relative path is relativ to the executable directory.
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  6. #6
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to initiate a form based on QUiLoader

    Hi Lykurg, thanks for your advice. qDebug returned true for that line though I looked at the code more closely around there afterwards. The examples all show the use of:
    Qt Code:
    1. QFile file(":form.ui");
    To copy to clipboard, switch view to plain text mode 
    where as I had:
    Qt Code:
    1. QFile file("form.ui");
    To copy to clipboard, switch view to plain text mode 

    Anyway, I changed my code to match the format of :form.ui and I am now getting the following error:
    Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
    QLayout: Cannot add null widget to QVBoxLayout/
    I have now tried several different .ui files but all provide the same error. Do you have any suggestions?

    Many thanks.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,368
    Thanks
    19
    Thanked 992 Times in 903 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to initiate a form based on QUiLoader

    Hi,

    the ":" only indicates that it uses the resource system of Qt. You don't need that. Also your two line about tmpButton are nonsense. Anyway, I can't see a real problem in your code. Although you are mixing two approaches on how to use ui files when you also declare a ui pointer in the header file. (See the Qt Designer manual for a detailed description on how to use ui files)

    But to the problem: what does
    Qt Code:
    1. qWarning() << file.readAll();
    To copy to clipboard, switch view to plain text mode 
    say? (after opening it off course) It must be something with the file. It isn't read correctly...
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  8. #8
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to initiate a form based on QUiLoader

    Hi, I tried to use the qWarning() function but just had:
    ""
    printed each time on the console.

    Could my problem be caused because I am trying to call the UI from a button click within an existing QWindow. I have got the code to load the form in the end by changing the following:

    Qt Code:
    1. QUiLoader builder;
    2. QFile file("form.ui");
    3. file.open(QFile::ReadOnly);
    4. QWidget *myWidget = builder.load(&file, this);
    5. QVBoxLayout *layout = new QVBoxLayout;
    6. layout->addWidget(myWidget);
    7. setLayout(layout);
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. QUiLoader builder;
    2. QFile file("../form.ui");
    3. file.open(QFile::ReadOnly);
    4. QWidget *myWidget = builder.load(&file);
    5. myWidget->show();
    6. file.close();
    To copy to clipboard, switch view to plain text mode 

    Is there anything wrong with the new code or can I settle on it?

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,368
    Thanks
    19
    Thanked 992 Times in 903 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to initiate a form based on QUiLoader

    Quote Originally Posted by peterjb31 View Post
    Hi, I tried to use the qWarning() function but just had:
    Yes, that is what I said in post #5: Your file is not loaded. Why: because you are using a relative path.
    So: use the resource system or use an absolute path or use a correct relative path. (See also Current working directory)
    Artistic Style Plugin (1.3.0) for Qt Creator 3.0.0: http://astyleplugin.sourceforge.net.

  10. The following 2 users say thank you to Lykurg for this useful post:

    Niamita (4th October 2011), peterjb31 (7th April 2011)

+ Reply to Thread

Similar Threads

  1. undefined reference to `QUiLoader::QUiLoader(QObject*)'
    By ashukla in forum Qt Programming
    Replies: 7
    Last Post: 7th October 2011, 19:05
  2. Replies: 0
    Last Post: 31st July 2010, 18:27
  3. Advanced use of QUiLoader
    By doberkofler in forum Qt Programming
    Replies: 2
    Last Post: 28th October 2009, 16:47
  4. Advanced use of QUiLoader
    By doberkofler in forum Newbie
    Replies: 0
    Last Post: 27th October 2009, 22:02
  5. Initiate file download on drag from between models
    By psih128 in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2009, 22:03

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