Results 1 to 9 of 9

Thread: [SOLVED] How to use Ui forms as a simple Dialog?

  1. #1
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Question [SOLVED] How to use Ui forms as a simple Dialog?

    In the Qt Creator I have two options to add a form:
    New->Qt-> Qt Designer Form Class
    New->Qt-> Qt Designer Form

    I want to use Qt Designer to design an "About this software" Dialog and run it from MainWindow menu. I know how to do this via Qt Designer Form Class, but I think it doesn't need to add about.ui, about.h and about.cpp to my application for doing this.
    I believe it should be possible to do such an easy job only by using

    New->Qt-> Qt Designer Form

    but I don't know how use ui form itself as a Dialog. Please give me some directions.
    Last edited by tofighi; 15th July 2011 at 23:12.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Ui forms as a simple Dialog?

    The best way would be to create the files about.cpp and about.h to manage to form and then when you want to show the dialog, create an instance of the about class and request it to show the dialog.

    If you are using Qt Designer, then the header file will contain everything necessary to build the dialog and configure all appropriate signals. A bit of glue code in the CPP will use that.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to use Ui forms as a simple Dialog?

    You cannot use a ui file by itself, you need a QWidget on to which you load the ui file.

    Qt Code:
    1. class Widget : public QWidget
    2. {
    3. Widget(QWidget * parent = 0);
    4. private:
    5. Ui::form * ui;
    6. }
    7.  
    8. Widget::Widget(QWidget * parent) : QWidget(parent), ui(new Ui::form)
    9. {
    10. ui->setupUi(this); //this loads the ui on this widget
    11. }
    12.  
    13. Widget::~Widget()
    14. {
    15. delete ui;
    16. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use Ui forms as a simple Dialog?

    No, you don't need to write custom class, .ui form is enough, but writing class is more reusable. If this is simple window that displays some text, you can show the dialog this way:
    Qt Code:
    1. #include "ui_AboutDialog.h"
    2. //...
    3. void Main::about(){
    4. Ui::AboutDialog ui; // depends on what name you give to the dialog in Designer
    5. ui.setupUi(&d);
    6. d.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to stampede for this useful post:

    tofighi (15th July 2011)

  6. #5
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to use Ui forms as a simple Dialog?

    Quote Originally Posted by stampede View Post
    No, you don't need to write custom class, .ui form is enough, but writing class is more reusable. If this is simple window that displays some text, you can show the dialog this way:
    Qt Code:
    1. #include "ui_AboutDialog.h"
    2. //...
    3. void Main::about(){
    4. Ui::AboutDialog ui; // depends on what name you give to the dialog in Designer
    5. ui.setupUi(&d);
    6. d.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    I tried to do it in this way. I've created test.ui using this path:

    New->Qt-> Qt Designer Form > Dialog with buttons buttom
    and name it test.ui instead of dialog.ui

    It adds automatically ui_test.h in the build directory :

    Qt Code:
    1. #ifndef UI_TEST_H
    2. #define UI_TEST_H
    3. #include <QtCore/QVariant>
    4. #include <QtGui/QAction>
    5. #include <QtGui/QApplication>
    6. #include <QtGui/QButtonGroup>
    7. #include <QtGui/QDialog>
    8. #include <QtGui/QDialogButtonBox>
    9. #include <QtGui/QHeaderView>
    10.  
    11. QT_BEGIN_NAMESPACE
    12.  
    13. class Ui_Dialog
    14. {
    15. public:
    16. QDialogButtonBox *buttonBox;
    17.  
    18. void setupUi(QDialog *Dialog)
    19. {
    20. if (Dialog->objectName().isEmpty())
    21. Dialog->setObjectName(QString::fromUtf8("Dialog"));
    22. Dialog->resize(400, 300);
    23. buttonBox = new QDialogButtonBox(Dialog);
    24. buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    25. buttonBox->setGeometry(QRect(30, 240, 341, 32));
    26. buttonBox->setOrientation(Qt::Horizontal);
    27. buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    28.  
    29. retranslateUi(Dialog);
    30. QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));
    31. QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));
    32.  
    33. QMetaObject::connectSlotsByName(Dialog);
    34. } // setupUi
    35.  
    36. void retranslateUi(QDialog *Dialog)
    37. {
    38. Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
    39. } // retranslateUi
    40.  
    41. };
    42.  
    43. namespace Ui {
    44. class Dialog: public Ui_Dialog {};
    45. } // namespace Ui
    46.  
    47. QT_END_NAMESPACE
    48.  
    49. #endif // UI_TEST_H
    To copy to clipboard, switch view to plain text mode 

    and I add it to my MainWindow.h

    Qt Code:
    1. #include "ui_test.h"
    To copy to clipboard, switch view to plain text mode 

    As you can see in ui_test.h file, I cannot use the code like below:

    Qt Code:
    1. Ui::TestDialog ui;
    To copy to clipboard, switch view to plain text mode 

    because there is not such class. The class is named automatically Ui_Dialog, and I don't know why it gives it this name. I also tried to compile it with this name but it generates an error:
    Qt Code:
    1. 'Ui_Dialog' is not a member of 'Ui'
    To copy to clipboard, switch view to plain text mode 

    There is another side effect. If I add another form test2.ui in this way, it generates the same ui_test2.h, with the same class name ( Ui_Dialog).
    I'm really confused by the way Qt 4.7 acts in this part.

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use Ui forms as a simple Dialog?

    Because this name is automatically generated, its based on the type (class) of object you create in designer. To change the name of the generated class, use objectName field in PropertyEditor (click somewhere on main widget form to edit its properties).
    Btw. why do you think the class should be named TestDialog ? Class name is always as objectName property of "main widget" in the form (the one that holds other widgets, in your case you should use Ui:: Dialog).
    Last edited by stampede; 15th July 2011 at 22:28.

  8. #7
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to use Ui forms as a simple Dialog?

    Quote Originally Posted by stampede View Post
    Because this name is automatically generated, its based on the type (class) of object you create in designer. To change the name of the generated class, use objectName field in PropertyEditor (click somewhere on main widget form to edit its properties).
    Btw. why do you think the class should be named TestDialog ? Class name is always as objectName property of "main widget" in the form (the one that holds other widgets, in your case you should use Ui:: Dialog).
    Ok, I got this part. But it still generates an error. My code is:

    Qt Code:
    1. void MainWindow::openAbout()
    2. {
    3. Ui::Ui_Test uiTestDlg;
    4. uiTestDlg.setupUi(&d);
    5. d.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    and I also included ui_test.h in the MainWindow.h
    But it generates this error:

    Qt Code:
    1. 'Ui_Test' is not a member of 'Ui'
    To copy to clipboard, switch view to plain text mode 

    When I'm typing Ui:: it shows me Ui_Test is a member of Ui in the context menu, but whe I compile it, it says me it is not its member?!!

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use Ui forms as a simple Dialog?

    Not Ui:: Ui_Test - Ui:: Test or Ui_Test, check the generated header for the class names if you are not sure. Do not rely blindly on the auto completer, its not perfect.

  10. The following user says thank you to stampede for this useful post:

    tofighi (15th July 2011)

  11. #9
    Join Date
    May 2011
    Posts
    16
    Thanks
    4
    Qt products
    Qt4

    Default Re: How to use Ui forms as a simple Dialog?

    Quote Originally Posted by stampede View Post
    Not Ui:: Ui_Test - Ui:: Test or Ui_Test, check the generated header for the class names if you are not sure. Do not rely blindly on the auto completer, its not perfect.
    Your help is so great and straightforward. Thank you very much.

Similar Threads

  1. How to access objects of parent Dialog from Child Dialog .
    By ranjit.kadam in forum Qt Programming
    Replies: 4
    Last Post: 18th April 2011, 06:39
  2. Multiple Forms and vertical layout in forms
    By eva2002 in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 05:05
  3. Simple Dialog
    By hesitation in forum Newbie
    Replies: 6
    Last Post: 10th June 2009, 10:50
  4. Layouting "Windows.Forms" like Qt forms
    By Boron in forum Qt Tools
    Replies: 1
    Last Post: 29th April 2008, 22:01
  5. can't get a simple dialog working
    By pthomas in forum Newbie
    Replies: 8
    Last Post: 13th January 2006, 14:52

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.