Results 1 to 6 of 6

Thread: Multiple UI files

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple UI files

    Hi, I'm new to Qt and I am using Qt Creator to build my project.
    I created a simple project to get familiar with the signal/slot mechanism of Qt.
    In created a mainwindow with one pushbutton in the middle.
    I created a dialog using a separate UI form with its corrsponding header.

    Im trying to get the program to open the dialog window whenever I press
    the pushbutton in the main window but I cant seem to get it to work. Where
    should I declar the signals, etc to accomplish this? I have tried using
    the connect mechanism but even when I modify the mainwindow.cpp
    file and try using the pushButton object it says that it is not defined
    in this scope. Any help is greatly appreciated!

    Here are the 4 files Qt created (UI files are not shown):

    mainwindow.cpp is as follows:

    //////////////////////////////////////////////////////////////////

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    /*QLabel *headIcon = new QLabel(this); // This is how to display a pic
    headIcon->setPixmap(QPixmap("hangmanv10.jpg"));
    headIcon->move(100, 200);
    headIcon->adjustSize();
    headIcon->show();
    headIcon->setAttribute(Qt::WA_DeleteOnClose);*/
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    //////////////////////////////////////////////////////

    mainwindow.h is as follows:

    //////////////////////////////////////////////////////

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QMainWindow>
    #include "dialog.h"

    namespace Ui
    {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    };

    #endif // MAINWINDOW_H

    //////////////////////////////////////////////////////

    dialog.cpp is as follows:

    //////////////////////////////////////////////////////

    #include "dialog.h"
    #include "ui_dialog.h"

    Dialog:ialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui:ialog)
    {
    m_ui->setupUi(this);
    }

    Dialog::~Dialog()
    {
    delete m_ui;
    }

    void Dialog::changeEvent(QEvent *e)
    {
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
    m_ui->retranslateUi(this);
    break;
    default:
    break;
    }
    }

    //////////////////////////////////////////////////////

    dialog.h is as follows:

    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QtGui/QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog {
    Q_OBJECT
    public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

    protected:
    void changeEvent(QEvent *e);

    private:
    Ui:ialog *m_ui;
    };

    #endif // DIALOG_H

    //////////////////////////////////////////////////////

    and main.cpp is as follows:

    //////////////////////////////////////////////////////

    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include "dialog.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    }

  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: Multiple UI files

    Wheres the code that you are using to open the dialog on button press? Even if it doesn't work, you should still post it so we can tell you where you are going wrong. Please post it in [code] tags rather than just pasting, as it makes it a lot easier to read.

    Also, you may be interested in the automatic connection of signals to slots, rather than using the 'connect' call for simple things like buttons. All you have to do is name the method appropriately, and it'll automatically be linked to the ui elements.

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple UI files

    last night I got it to work and this is what I had to do, but Im wondering if theres a way to do it from within the Designer form? Linking objects from different UI forms within the designer? To get it work I just created an Oject from the Dialog class within the mainwindow.h file and then called that object, a function to .show the object, and used the connec to connect the button from the mainwindow to the fucntion.

    First I added this to the mainwindow.cpp file:

    Qt Code:
    1. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showdialog()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::showdialog()
    2. {
    3. mydialog.show();
    4. }
    To copy to clipboard, switch view to plain text mode 


    added this to the mainwindow.h file:

    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. Dialog mydialog;
    4.  
    5. private slots:
    6. void showdialog();
    To copy to clipboard, switch view to plain text mode 

  4. The following 2 users say thank you to Diblye for this useful post:

    antonisberkous (29th June 2012), giacomelli.fabio (29th December 2009)

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

    Default Re: Multiple UI files

    You might find [qt]connectSlotsByName[/qt] to be easier. It's normally implemented in the generated code by UIC when your ui file is parsed. Assuming your button is called pushButton, then just adding the line:

    Qt Code:
    1. void on_pushButton_pressed();
    To copy to clipboard, switch view to plain text mode 

    To the "slots" part of your header file will generate an automatic connect call, rather than doing it yourself.

  6. The following user says thank you to squidge for this useful post:

    Diblye (2nd November 2009)

  7. #5
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple UI files

    Awesome, that worked too!! That makes it easy as long as long as you know what function to call and the object name! Thanks alot fatjuicymole!

  8. #6
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    1
    Platforms
    Unix/X11

    Default Re: Multiple UI files

    hello!

    it worked like a charm! Thanks!

    The problem that i have is that when i am on the second ui ( dialog ) when i press the return button on the simulator the apps exits.
    I would like when i press "return " to go back to the first one ui.

    Any help on this?

    Thanks!

Similar Threads

  1. example project with multiple .ui files
    By navid in forum Newbie
    Replies: 1
    Last Post: 31st October 2009, 18:25
  2. loging to multiple files using Log4Qt
    By zhongzhu in forum Qt Programming
    Replies: 0
    Last Post: 17th June 2009, 10:07
  3. Open multiple files with one program instance
    By Ginsengelf in forum Qt Programming
    Replies: 6
    Last Post: 16th March 2009, 14:08
  4. Multiple project files in a single directory
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th July 2008, 07:03
  5. Error: Using Multiple files
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 09:23

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.