Results 1 to 20 of 20

Thread: Using 2 ui files in a project

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Using 2 ui files in a project

    I have a QDialog with a pushbutton on it, and when I click the pushbutton, I want another QDialog window in a separate ui file to open. I have a dialog class (with the pushbutton in), but I can't figure out how to use the other ui file to create the window. I've included settings.h (the file that's automatically generated when I compile the program) and added it to the Ui namespace, then tried to create a settings object with "Ui::settings foo;", but I keep getting the same compiler error:

    "c:\programs\qt\sim\dialog.h:67: error: C2079: 'Dialog::foo' uses undefined class 'Ui::settings'"

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

    Default Re: Using 2 ui files in a project

    You may not have included the required ui file. See if this sample helps

    Qt Code:
    1. #include "ui_OtherDialog.h"
    2.  
    3. void Dialog::on_pushButton_Clicked()
    4. {
    5. QDialog * other_parent = new QDialog(this);
    6. Ui::OtherDialog other_ui;
    7.  
    8. other_ui.setupUi(other_parent);
    9.  
    10. other_parent.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    Quote Originally Posted by Santosh Reddy View Post
    You may not have included the required ui file. See if this sample helps

    Qt Code:
    1. #include "ui_OtherDialog.h"
    2.  
    3. void Dialog::on_pushButton_Clicked()
    4. {
    5. QDialog * other_parent = new QDialog(this);
    6. Ui::OtherDialog other_ui;
    7.  
    8. other_ui.setupUi(other_parent);
    9.  
    10. other_parent.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Hmm... Just tried that, but now I'm getting an error whenever I access a member of ui (in the dialog class):

    dialog.cpp:

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent):
    5. QDialog(parent),
    6. ui(new Ui::Dialog),
    7. {
    8. srand(time(0));
    9. ui->setupUi(this);
    10. ui->label->setVisible(false);
    11. }
    12.  
    13. void Dialog::on_pushButton_clicked(){ //settings
    14. QDialog *parent = new QDialog(this);
    15. Ui::settings foo;
    16.  
    17. foo.setupUi(parent);
    18. parent->exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h:

    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtCore>
    6. #include <QtGui>
    7. #include "settings.h"
    8. #include "ui_settings.h"
    9.  
    10. namespace Ui {
    11. class Dialog;
    12. class settings;
    13. }
    14.  
    15. class Dialog : public QDialog
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. Ui::Dialog *ui;
    21. explicit Dialog(QWidget *parent = 0);
    22. ~Dialog();
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26. };
    27.  
    28. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Now, I get errors whenever I have "ui->" in my code.
    Last edited by ben1996123; 3rd January 2013 at 16:51.

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

    Default Re: Using 2 ui files in a project

    Can you post the error?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    Quote Originally Posted by Santosh Reddy View Post
    Can you post the error?
    C:\programs\Qt\sim-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Release\..\sim\dialog.cpp:21: error: C2027: use of undefined type 'Ui::Dialog'
    C:\programs\Qt\sim-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Release\..\sim\dialog.cpp:21: error: C2227: left of '->setupUi' must point to class/struct/union/generic type

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    @ben: I think you are totally confused about how to go about what you want to do. You should not need to access the ui pointer in your other dialog at all. Ui pointers are meant to be hidden inside the widget classes that define them, so that they can access the signals, slots, and properties of the widgets defined in the ui file.

    To invoke a second dialog as a response to a button click in the first dialog, all you need to do is something like this:

    Qt Code:
    1. // Dialog1.cpp:
    2. #include "Dialog1.h"
    3. #include "Dialog2.h"
    4.  
    5. Dialog1::Dialog1( QWidget * parent )
    6. : QDialog( parent )
    7. , ui( new Ui::Dialog1 )
    8. {
    9. ui->setupUi( this );
    10.  
    11. connect( ui->someButton, SIGNAL( clicked() ), this, SLOT( showDialog2() ) );
    12. }
    13.  
    14. void Dialog1::showDialog2()
    15. {
    16. Dialog2 dlg2( this );
    17. dlg2->exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    where of course, you have defined the showDialog2 slot correctly in the Dialog1.h header. The "ui" pointers in the Dialog1 and Dialog2 classes should be declared as private, since there is absolutely no reason to access them from outside of the class the uses them.

    If there are any signals or slots in Dialog2 that need to be "watched" from inside of Dialog1, then set up the connections in showDialog2(). These will automatically be disconnected when dlg2 goes out of scope at the end of showDialog2().

  7. #7
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    Quote Originally Posted by d_stranz View Post
    @ben: I think you are totally confused about how to go about what you want to do. You should not need to access the ui pointer in your other dialog at all. Ui pointers are meant to be hidden inside the widget classes that define them, so that they can access the signals, slots, and properties of the widgets defined in the ui file.

    To invoke a second dialog as a response to a button click in the first dialog, all you need to do is something like this:

    Qt Code:
    1. // Dialog1.cpp:
    2. #include "Dialog1.h"
    3. #include "Dialog2.h"
    4.  
    5. Dialog1::Dialog1( QWidget * parent )
    6. : QDialog( parent )
    7. , ui( new Ui::Dialog1 )
    8. {
    9. ui->setupUi( this );
    10.  
    11. connect( ui->someButton, SIGNAL( clicked() ), this, SLOT( showDialog2() ) );
    12. }
    13.  
    14. void Dialog1::showDialog2()
    15. {
    16. Dialog2 dlg2( this );
    17. dlg2->exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    where of course, you have defined the showDialog2 slot correctly in the Dialog1.h header. The "ui" pointers in the Dialog1 and Dialog2 classes should be declared as private, since there is absolutely no reason to access them from outside of the class the uses them.

    If there are any signals or slots in Dialog2 that need to be "watched" from inside of Dialog1, then set up the connections in showDialog2(). These will automatically be disconnected when dlg2 goes out of scope at the end of showDialog2().
    That's similar to what I originally tried, but I was getting linker errors...

    dialog.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __thiscall settings::~settings(void)" (??1settings@@UAE@XZ) referenced in function "public: void __thiscall Dialog::showSettings(void)" (?showSettings@Dialog@@QAEXXZ)

    dialog.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall settings::settings(class QWidget *)" (??0settings@@QAE@PAVQWidget@@@Z) referenced in function "public: void __thiscall Dialog::showSettings(void)" (?showSettings@Dialog@@QAEXXZ)

    release\sim.exe:-1: error: LNK1120: 2 unresolved externals

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    That's because you are trying to access some "Ui::settings" object. I have no clue what that is, and it is probably a bad design to try to access it in the way that you are doing, whatever it is.

    What is it that you really want to do?

  9. #9
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    Quote Originally Posted by d_stranz View Post
    That's because you are trying to access some "Ui::settings" object. I have no clue what that is, and it is probably a bad design to try to access it in the way that you are doing, whatever it is.

    What is it that you really want to do?
    Click a pushbutton and have it create a window from the ui file.

    I basically copied the code that you posted, and I got the errors I posted before:

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

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "settings.h"
    4.  
    5. Dialog::Dialog(QWidget *parent):
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. void Dialog::on_pushButton_clicked(){ //settings
    13. settings a(this);
    14. a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    create a window from the ui file
    I don't know what that means. The moc compiler converts the XML definition in the .ui file into C++ code, which is then compiled and linked into your executable. At run-time the Qt meta-object system executes that compiled code and instantiates QWidgets and layouts, sets their properties, and establishes connections between signals and slots. That's what the "ui->setupUi( this )" line does behind the scenes.

    As far as I know, the only way to replicate this functionality at run-time (e.g. go from uncompiled XML code in a .ui file to running QWidget instances) is to use the QUiLoader or QFormBuilder classes.

    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. { //settings
    3. settings a(this);
    4. a.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    What is this "settings" class you are trying to create and exec()? Is that the name of your second dialog class? If it is, there is probably something wrong with the way it has been declared or implemented, but since you don't show the code for "settings.h" or "settings.cpp", there's no way to tell.

    What happens if you use QFileDialog instead of "settings"?

    Qt Code:
    1. #include <QFileDialog>
    2.  
    3. void Dialog::on_pushButton_clicked()
    4. { //settings
    5. QFileDialog a(this);
    6. a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 4th January 2013 at 17:19.

  11. #11
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using 2 ui files in a project

    Quote Originally Posted by d_stranz View Post
    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. { //settings
    3. settings a(this);
    4. a.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    What is this "settings" class you are trying to create and exec()? Is that the name of your second dialog class? If it is, there is probably something wrong with the way it has been declared or implemented, but since you don't show the code for "settings.h" or "settings.cpp", there's no way to tell.

    What happens if you use QFileDialog instead of "settings"?

    Qt Code:
    1. #include <QFileDialog>
    2.  
    3. void Dialog::on_pushButton_clicked()
    4. { //settings
    5. QFileDialog a(this);
    6. a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    The settings class is the second dialog class. I added the settings.h and settings.cpp files and straight away it wouldn't compile, even with the default code. Using QFileDialog compiles and works fine.

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

    settings.cpp:
    Qt Code:
    1. #include "settings.h"
    2. #include "ui_settings.h"
    3.  
    4. settings::settings(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::settings) //error: C2512: 'Ui::settings' : no appropriate default constructor available
    7. {
    8. }
    9.  
    10. settings::~settings(){
    11. delete ui;
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 15th August 2011, 23:26
  2. Replies: 1
    Last Post: 3rd December 2009, 23:34
  3. example project with multiple .ui files
    By navid in forum Newbie
    Replies: 1
    Last Post: 31st October 2009, 18:25
  4. QMake Project Files....
    By TemporalBeing in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2009, 16:15
  5. visual studio project files - adding extra files
    By luf in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 21:05

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