PDA

View Full Version : Multiple UI files



Diblye
31st October 2009, 23:42
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::Dialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::Dialog)
{
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::Dialog *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();
}

squidge
1st November 2009, 09:36
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.

Diblye
1st November 2009, 20:31
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:


connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showdialog( )));


void MainWindow::showdialog()
{
mydialog.show();
}


added this to the mainwindow.h file:


private:
Ui::MainWindow *ui;
Dialog mydialog;

private slots:
void showdialog();

squidge
1st November 2009, 22:21
You might find connectSlotsByName 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:



void on_pushButton_pressed();


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

Diblye
1st November 2009, 23:52
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!

antonisberkous
29th June 2012, 00:24
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!