PDA

View Full Version : Calling a new form from current form



webgenius
8th April 2007, 14:48
I'm designing front-end for a compression project using QT.

Here is a screen-shot of the forms:
http://img147.imageshack.us/img147/7517/qtinfoib2.jpg

My code starts from the form START. The use then has to select whether he wants to compress or decompress. I then have to invoke the forms compress or decompress accordingly. How can I call the compress form from start, when the COMPRESS BUTTON IS CLICKED?

Someone please help me regarding this.

jacek
8th April 2007, 15:02
http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_designer_2f orms
http://doc.trolltech.com/4.2/designer-using-a-component.html

webgenius
8th April 2007, 18:32
I want to somehow invoke the compression window when the COMPRESS button is clicked in the START form.
Here's the code related to START form genereted by UIC


#ifndef START_H
#define START_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

class Ui_MainWindow
{
public:
QWidget *centralwidget;
QPushButton *pushButton;
QPushButton *pushButton_2;
QMenuBar *menubar;
QStatusBar *statusbar;

void setupUi(QMainWindow *MainWindow)
{
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(30, 50, 75, 23));
pushButton_2 = new QPushButton(centralwidget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
pushButton_2->setGeometry(QRect(150, 50, 75, 23));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 262, 21));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
retranslateUi(MainWindow);
QSize size(262, 154);
size = size.expandedTo(MainWindow->minimumSizeHint());
MainWindow->resize(size);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi

void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("MainWindow", "COMPRESS", 0, QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("MainWindow", "DECOMPRESS", 0, QApplication::UnicodeUTF8));
Q_UNUSED(MainWindow);
} // retranslateUi

};

namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui

#endif // START_H


The main code:


#include <QApplication>
#include <QWidget>
#include <QMainWindow>

#include "start.cpp"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QMainWindow w;
Ui_MainWindow ui;
ui.setupUi(&w);

w.show();
return app.exec();
}



Code generated from COMPRESSION form:


#ifndef COMPRESSION_H
#define COMPRESSION_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QSpinBox>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

class Ui_CompWindow
{
public:
QAction *action_Exit;
QAction *action_Compress;
QAction *action_Decompress;
QWidget *centralwidget;
QLineEdit *lineEdit;
QLineEdit *lineEdit_2;
QLineEdit *lineEdit_3;
QLineEdit *lineEdit_4;
QLineEdit *lineEdit_5;
QLabel *label_2;
QLabel *label_3;
QLabel *label_4;
QLabel *label_5;
QLabel *label_6;
QPushButton *pushButton;
QPushButton *pushButton_2;
QSpinBox *spinBox;
QLabel *label;
QMenuBar *menubar;
QMenu *menu_Menu;
QMenu *menu_About;
QMenu *menu_File;
QStatusBar *statusbar;

void setupUi(QMainWindow *CompWindow)
{
CompWindow->setObjectName(QString::fromUtf8("CompWindow"));
action_Exit = new QAction(CompWindow);
action_Exit->setObjectName(QString::fromUtf8("action_Exit"));
action_Compress = new QAction(CompWindow);
action_Compress->setObjectName(QString::fromUtf8("action_Compress"));
action_Decompress = new QAction(CompWindow);
action_Decompress->setObjectName(QString::fromUtf8("action_Decompress"));
centralwidget = new QWidget(CompWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
lineEdit = new QLineEdit(centralwidget);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(140, 70, 261, 20));
lineEdit_2 = new QLineEdit(centralwidget);
lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
lineEdit_2->setGeometry(QRect(140, 100, 261, 20));
lineEdit_3 = new QLineEdit(centralwidget);
lineEdit_3->setObjectName(QString::fromUtf8("lineEdit_3"));
lineEdit_3->setGeometry(QRect(140, 130, 261, 20));
lineEdit_4 = new QLineEdit(centralwidget);
lineEdit_4->setObjectName(QString::fromUtf8("lineEdit_4"));
lineEdit_4->setGeometry(QRect(140, 160, 261, 20));
lineEdit_5 = new QLineEdit(centralwidget);
lineEdit_5->setObjectName(QString::fromUtf8("lineEdit_5"));
lineEdit_5->setGeometry(QRect(140, 190, 261, 20));
label_2 = new QLabel(centralwidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(90, 70, 41, 16));
label_3 = new QLabel(centralwidget);
label_3->setObjectName(QString::fromUtf8("label_3"));
label_3->setGeometry(QRect(90, 100, 46, 14));
label_4 = new QLabel(centralwidget);
label_4->setObjectName(QString::fromUtf8("label_4"));
label_4->setGeometry(QRect(90, 130, 46, 14));
label_5 = new QLabel(centralwidget);
label_5->setObjectName(QString::fromUtf8("label_5"));
label_5->setGeometry(QRect(90, 160, 46, 14));
label_6 = new QLabel(centralwidget);
label_6->setObjectName(QString::fromUtf8("label_6"));
label_6->setGeometry(QRect(90, 190, 46, 14));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(120, 250, 75, 23));
pushButton_2 = new QPushButton(centralwidget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
pushButton_2->setGeometry(QRect(230, 250, 75, 23));
spinBox = new QSpinBox(centralwidget);
spinBox->setObjectName(QString::fromUtf8("spinBox"));
spinBox->setGeometry(QRect(140, 20, 44, 22));
spinBox->setMaximum(9);
spinBox->setValue(3);
label = new QLabel(centralwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(30, 20, 111, 31));
CompWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(CompWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 438, 21));
menu_Menu = new QMenu(menubar);
menu_Menu->setObjectName(QString::fromUtf8("menu_Menu"));
menu_About = new QMenu(menubar);
menu_About->setObjectName(QString::fromUtf8("menu_About"));
menu_File = new QMenu(menubar);
menu_File->setObjectName(QString::fromUtf8("menu_File"));
CompWindow->setMenuBar(menubar);
statusbar = new QStatusBar(CompWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
CompWindow->setStatusBar(statusbar);

menubar->addAction(menu_File->menuAction());
menubar->addAction(menu_Menu->menuAction());
menubar->addAction(menu_About->menuAction());
menu_Menu->addAction(action_Compress);
menu_Menu->addAction(action_Decompress);
menu_File->addAction(action_Exit);

retranslateUi(CompWindow);

QSize size(438, 325);
size = size.expandedTo(CompWindow->minimumSizeHint());
CompWindow->resize(size);


QMetaObject::connectSlotsByName(CompWindow);
} // setupUi

void retranslateUi(QMainWindow *CompWindow)
{
CompWindow->setWindowTitle(QApplication::translate("CompWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
action_Exit->setText(QApplication::translate("CompWindow", "&Exit", 0, QApplication::UnicodeUTF8));
action_Compress->setText(QApplication::translate("CompWindow", "&Compress", 0, QApplication::UnicodeUTF8));
action_Decompress->setText(QApplication::translate("CompWindow", "&Decompress", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("CompWindow", "File 1", 0, QApplication::UnicodeUTF8));
label_3->setText(QApplication::translate("CompWindow", "File 2", 0, QApplication::UnicodeUTF8));
label_4->setText(QApplication::translate("CompWindow", "File 3", 0, QApplication::UnicodeUTF8));
label_5->setText(QApplication::translate("CompWindow", "File 4", 0, QApplication::UnicodeUTF8));
label_6->setText(QApplication::translate("CompWindow", "File 5", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("CompWindow", "OK", 0, QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("CompWindow", "EXIT", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("CompWindow", "Compression Level", 0, QApplication::UnicodeUTF8));
menu_Menu->setTitle(QApplication::translate("CompWindow", "&Menu", 0, QApplication::UnicodeUTF8));
menu_About->setTitle(QApplication::translate("CompWindow", "&About", 0, QApplication::UnicodeUTF8));
menu_File->setTitle(QApplication::translate("CompWindow", "&File", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class CompWindow: public Ui_CompWindow {};
} // namespace Ui

#endif // COMPRESSION_H

marcel
8th April 2007, 18:45
I never tried to use two main windows in the same app, but if you want it this way, then use QMainWindow::show() to show the compression window.

To do this, create a slot in the class in which you create the compress button.
Let's call this function onCompressClicked(). You can make this slot protected, since it will be used only by your class.

Then, right after creating the compress button, add:

connect( compressButton, SIGNAL( clicked() ), this, SLOT( onCompressClicked() ) );

Then:



void StartClass::onCompressClicked()
{
CompressWindow cWindow; //create an instance of the compress window
cWindow.show();
}
I am almost sure this will work ( creating a main window from a main window). However, you may have problem with event passing and handling from the second main window. Anyway, try this and if it works leave it like this.

Regards

webgenius
8th April 2007, 19:24
For some strange reason, my connect call fails every time.

marcel
8th April 2007, 19:27
You must be doing something wrong. Could you post that part of the code?

jacek
8th April 2007, 19:44
void StartClass::onCompressClicked()
{
CompressWindow cWindow; //create an instance of the compress window
cWindow.show();
}
This won't work, because show() is a non-blocking call --- it only posts an event and returns, so onCompressClicked() returns immediately and cWindow gets destroyed before user has a chance to see it.

This should work:
void StartClass::onCompressClicked()
{
CompressWindow * cWindow = new CompressWindow();
cWindow->setAttribute( Qt::WA_DeleteOnClose );
cWindow->show();
}

jacek
8th April 2007, 19:50
QMainWindow w;
Ui_MainWindow ui;
ui.setupUi(&w);
Try the single inheritance approach.

http://doc.trolltech.com/4.2/designer-using-a-component.html#the-single-inheritance-approach


For some strange reason, my connect call fails every time.
You have to declare onCompressClicked() as a slot to be able to connect a signal to it. This means also that onCompressClicked() has to be a method of a class derived from QObject (for example a widget).

Don't forget to add Q_OBJECT macro to your class definition.

http://www.qtcentre.org/forum/faq.php?faq=qt_signalslot#faq_qt_signalslot_nodebu g