PDA

View Full Version : Help with multiple forms...!



hakermania
18th July 2010, 14:43
Hello all, I am completely new to this forum and quite new to QtCeator... I would like to ask some things about having multiple forms. Ok, let's say that we have a very simple program with only a button "Preferences". When you click this button a new Window (and not QMessageBox) is created with his own features etc... I know how to pop-up a MessageBox, but how do I open a whole new Window? The problem seems huge considering that you have to design its own ui and define its own settings.. Can anyone help me on this?
I give you the files:
mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;

private slots:
void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


mainwindow.cpp


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

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

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

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void MainWindow::on_pushButton_clicked()
{
//Here the other window should open up
}




main.cpp


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

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

Screenshot of Gui:
http://i30.tinypic.com/2v2tjia.png
I have to say that I had the idea an other executable to run each time you press the button preferences but this is quite noob I think :p:p:p

Lykurg
18th July 2010, 15:06
Hi,

please also note our Newbie section. I would suggest you to read the documentation about signal and slot mechanism and have a look at QDialog.

Lykurg

hakermania
18th July 2010, 15:24
Thank you very much for the answer:)

hakermania
18th July 2010, 15:32
Ok, let's see....

void MainWindow::on_pushButton_clicked()
{
QDialog *dial = new QDialog;
dial->setWindowTitle("Hello world");
dial->show();
}
How can I edit this QDialog? Add buttons, labels, etc..?

Lykurg
18th July 2010, 15:39
How can I edit this QDialog? Add buttons, labels, etc..? Like you do with any other QMainWindow or QWidget (which is the base for all other widgets.). And be aware that you delete the pointer somewhere.

Zlatomir
18th July 2010, 15:40
You add just like in any other QWidgets

And don't forget to pas the MainWindow as a parent for your QDialog:


void MainWindow::on_pushButton_clicked()
{
QDialog *dial = new QDialog(this);
dial->setWindowTitle("Hello world");
dial->show();
}

hakermania
18th July 2010, 15:52
Ok thx for any help.....

hakermania
18th July 2010, 18:34
""You add just like in any other QWidgets""
And how do I exactly add these in "any other Widgets"???:confused::confused::confused:

Lykurg
18th July 2010, 18:49
please read the documentation or any book about Qt! Start the documentation on how to use layouts!