OpieProgrammingStepByStep2

= Table Of Contents =

Chapter II

Next OpieProgrammingStepByStep3

Previous OpieProgrammingStepByStep

= Application should have dialog =

Simple yes no as response to action
diff -urNd step4/mainwindow.cpp step5/mainwindow.cpp --- step4/mainwindow.cpp	2005-05-02 22:01:29.000000000 +0300 +++ step5/mainwindow.cpp	2005-05-04 12:00:52.773454708 +0300 @@ -2,6 +2,7 @@ +#include  @@ -21,6 +22,10 @@ } void MainWindow::createActions { +	newAct = new QAction(tr("&New"), QString::null, 0, this, 0); +	newAct->setIconSet(Opie::Core::OResource::loadPixmap( "new_icon", Opie::Core::OResource::SmallIcon )); +	connect( newAct, SIGNAL( activated ), this, SLOT(newFile) ); + 	exitAct = new QAction(tr("&Quit"), QString::null, 0, this, 0); exitAct->setIconSet(Opie::Core::OResource::loadPixmap( "quit_icon", Opie::Core::OResource::SmallIcon )); connect( exitAct, SIGNAL( activated ), qApp, SLOT(quit) ); @@ -28,6 +33,19 @@ void MainWindow::createMenus { fileMenu = new QPopupMenu(this); + +	newAct->addTo(fileMenu); +	fileMenu->insertSeparator; exitAct->addTo(fileMenu); + 	menuBar->insertItem(tr("&File"), fileMenu); } + +void MainWindow::newFile { +	int ret = QMessageBox::warning(this, tr("Opie-rdt"), +		tr("The document has been modified. \n" +		  "Do you want to save changes?"), +		QMessageBox::Yes || QMessageBox::Default, +		QMessageBox::No, +		QMessageBox::Cancel | QMessageBox::Escape); +} diff -urNd step4/mainwindow.h step5/mainwindow.h --- step4/mainwindow.h	2005-05-02 22:01:29.000000000 +0300 +++ step5/mainwindow.h	2005-05-04 12:00:52.773454708 +0300 @@ -14,12 +14,16 @@ 	MainWindow(QWidget *parent = 0, const char *name =0, WFlags fl); ~MainWindow; +private slots: +	void newFile; + private: void createActions; void createMenus; QTextBrowser *sourcewin; +	QAction *newAct; QAction *exitAct; QPopupMenu *fileMenu; };
 * 1) include 
 * 2) include 
 * 3) include 
 * 1) include 
 * 1) include 	// For loading icons

Select file dialog
diff -urNd step5/mainwindow.cpp step6/mainwindow.cpp --- step5/mainwindow.cpp	2005-05-04 12:00:52.773454708 +0300 +++ step6/mainwindow.cpp	2005-05-04 12:50:18.084264406 +0300 @@ -3,12 +3,16 @@ +#include  +#include  +using namespace Opie::Ui; + MainWindow::MainWindow(QWidget *parent, const char* name, WFlags fl = 0) : QMainWindow( parent, name, fl ) { sourcewin = new QTextBrowser( this ); @@ -26,6 +30,11 @@ 	newAct->setIconSet(Opie::Core::OResource::loadPixmap( "new_icon", Opie::Core::OResource::SmallIcon )); connect( newAct, SIGNAL( activated ), this, SLOT(newFile) ); +	openAct = new QAction(tr("&Open"), QString::null, 0, this, 0); +	openAct->setIconSet(Opie::Core::OResource::loadPixmap( "open_icon", Opie::Core::OResource::SmallIcon )); +	connect( openAct, SIGNAL( activated ), this, SLOT(open) ); + +	 	exitAct = new QAction(tr("&Quit"), QString::null, 0, this, 0); exitAct->setIconSet(Opie::Core::OResource::loadPixmap( "quit_icon", Opie::Core::OResource::SmallIcon )); connect( exitAct, SIGNAL( activated ), qApp, SLOT(quit) ); @@ -35,6 +44,7 @@ 	fileMenu = new QPopupMenu(this); newAct->addTo(fileMenu); +-	openAct->addTo(fileMenu); fileMenu->insertSeparator; exitAct->addTo(fileMenu); @@ -49,3 +59,14 @@ 		QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape); } + +void MainWindow::open { +	QMap mimeTypes; +	QStringList types; +	types << "text/* "; +	mimeTypes.insert( tr("Text"), types ); +	mimeTypes.insert( tr("All"), "*/*" ); +	QString fileName= OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL, +                                                "/home", QString::null, mimeTypes); +	sourcewin->setText(fileName); +} diff -urNd step5/mainwindow.h step6/mainwindow.h --- step5/mainwindow.h	2005-05-04 12:00:52.773454708 +0300 +++ step6/mainwindow.h	2005-05-04 12:50:18.084264406 +0300 @@ -16,6 +16,7 @@ private slots: 	void newFile; +	void open; private: 	void createActions; @@ -24,6 +25,7 @@ 	QTextBrowser *sourcewin; 	QAction *newAct; +	QAction *openAct; 	QAction *exitAct; 	QPopupMenu *fileMenu; }; diff -urNd step5/opie-rdt.pro step6/opie-rdt.pro --- step5/opie-rdt.pro	2005-05-04 12:05:46.546176998 +0300 +++ step6/opie-rdt.pro	2005-05-04 12:50:18.084264406 +0300 @@ -9,5 +9,6 @@ TARGET  = opie-rdt INCLUDEPATH += $(OPIEDIR)/include DEPENDPATH += $(OPIEDIR)/include +LIBS  += -lopieui2 include ( $(OPIEDIR)/include.pro )
 * 1) include 
 * 2) include 
 * 3) include 
 * 1) include 
 * 1) include 	// For loading icons
 * 1) include "mainwindow.h"

QT Designer
Creating dialogs for QT is possible dinamicaly from programm. For making dialogs Trolltech has developed special tool named QT Designer. It has next advantages against dinamicaly created dialogs:


 * it is more easy to understund and maintain gui elements visualy...
 * seperating gui from logic is easer to convert in case of upgrades

I would recomend to read Qt Designers Tutorial if you are not familiar with creating dialogs with QT.

FIXME: add find dialog layout picture

So to have our own dialog we should make dialog in QTDesigner and save result as .ui file. Start designer choose menu File,New and choose Dialog. Resize your dialog and put label and textedit controls on it. Select both controls and layout them horizontaly. Then put two check box controls and selecting them and joined text and label filds layout all this verticaly.

Second dialog part will be buttons put two of them on form and also vertical spacer. Layout thease three controls verticaly. Next select from so choosing form layout and push horisontal layout button. As last correct all forms properties.

Also we should change some properties of our form like text for buttons and most important dialog form Name field change to - "FindDialog". This will be used to refer our dialog in code.

Now our form is ready and we can save it as .ui file in project directory. You can find resulting file attached to this page.

Include dialog in code
Let change our application to call new created dialog.

diff -uNrd step6/opie-rdt.pro step7/opie-rdt.pro --- step6/opie-rdt.pro	2005-05-04 12:50:18.000000000 +0300 +++ step7/opie-rdt.pro	2005-05-07 14:30:35.347047062 +0300 @@ -3,6 +3,7 @@ INCLUDEPATH +=. +INTERFACES += finddialog.ui HEADERS += mainwindow.h SOURCES += main.cpp mainwindow.cpp diff -uNrd step6/mainwindow.h step7/mainwindow.h --- step6/mainwindow.h	2005-05-04 12:50:18.000000000 +0300 +++ step7/mainwindow.h	2005-05-07 14:30:35.345047398 +0300 @@ -17,6 +17,7 @@ private slots: void newFile; void open; +	void findEdit; private: void createActions; @@ -27,7 +28,9 @@ 	QAction *newAct; QAction *openAct; QAction *exitAct; +	QAction *findAct; QPopupMenu *fileMenu; +	QPopupMenu *editMenu; }; diff -uNrd step6/mainwindow.cpp step7/mainwindow.cpp --- step6/mainwindow.cpp	2005-05-04 12:50:18.000000000 +0300 +++ step7/mainwindow.cpp	2005-05-07 14:30:35.345047398 +0300 @@ -10,6 +10,7 @@ +#include "finddialog.h" using namespace Opie::Ui; @@ -38,17 +39,25 @@ 	exitAct = new QAction(tr("&Quit"), QString::null, 0, this, 0); exitAct->setIconSet(Opie::Core::OResource::loadPixmap( "quit_icon", Opie::Core::OResource::SmallIcon )); connect( exitAct, SIGNAL( activated ), qApp, SLOT(quit) ); -} + +	+	findAct = new QAction(tr("&Find"), QString::null, 0, this, 0); +	findAct->setIconSet(Opie::Core::OResource::loadPixmap( "find_icon", Opie::Core::OResource::SmallIcon )); +	connect( findAct, SIGNAL( activated ), this, SLOT(findEdit) );} void MainWindow::createMenus { fileMenu = new QPopupMenu(this); +	editMenu = new QPopupMenu(this); newAct->addTo(fileMenu); -	openAct->addTo(fileMenu); fileMenu->insertSeparator; exitAct->addTo(fileMenu); +	findAct->addTo(editMenu); + 	menuBar->insertItem(tr("&File"), fileMenu); +	menuBar->insertItem(tr("&Edit"), editMenu); } void MainWindow::newFile { @@ -70,3 +79,9 @@                                                 "/home", QString::null, mimeTypes); 	sourcewin->setText(fileName); } + +void MainWindow::findEdit { +	FindDialog *dialog = new FindDialog; +	dialog->exec; +} +
 * 1) Input
 * 1) endif
 * 1) include 	// For loading icons
 * 1) include "mainwindow.h"

Dialog implementation
diff -uNrd step7/finddialogimpl.h step8/finddialogimpl.h --- step7/finddialogimpl.h     1970-01-01 03:00:00.000000000 +0300 +++ step8/finddialogimpl.h     2005-05-18 23:33:51.174332470 +0300 @@ -0,0 +1,17 @@ +#ifndef FINDDIALOGIMPL_H +#define FINDDIALOGIMPL_H + +#include "finddialog.h" + +class FindDialogImpl : public FindDialog +{ +      Q_OBJECT + +public: +      FindDialogImpl( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 ); + +private: +      void createConnections; +}; + +#endif

diff -uNrd step7/finddialogimpl.cpp step8/finddialogimpl.cpp --- step7/finddialogimpl.cpp   1970-01-01 03:00:00.000000000 +0300 +++ step8/finddialogimpl.cpp   2005-05-18 23:33:51.174332470 +0300 @@ -0,0 +1,14 @@ +#include  + +#include "finddialogimpl.h" + +FindDialogImpl::FindDialogImpl( QWidget *parent, const char* name, bool modal, WFlags fl) +              : FindDialog( parent, name, modal, fl) +{ +      createConnections; +} + +void FindDialogImpl::createConnections { +      connect( PushButton2, SIGNAL( clicked ),  this, SLOT(close) ); +} +

diff -uNrd step7/mainwindow.cpp step8/mainwindow.cpp --- step7/mainwindow.cpp       2005-05-07 14:30:35.000000000 +0300 +++ step8/mainwindow.cpp       2005-05-18 23:33:51.153337713 +0300 @@ -10,7 +10,7 @@
 * 1) include <opie2/oresource.h>  // For loading icons

-#include "finddialog.h" +#include "finddialogimpl.h"
 * 1) include "mainwindow.h"

using namespace Opie::Ui;

@@ -50,7 +50,7 @@       editMenu = new QPopupMenu(this);

newAct->addTo(fileMenu); --     openAct->addTo(fileMenu); +      openAct->addTo(fileMenu); fileMenu->insertSeparator; exitAct->addTo(fileMenu);

@@ -81,7 +81,7 @@ }

void MainWindow::findEdit { -      FindDialog *dialog = new FindDialog; +      FindDialogImpl *dialog = new FindDialogImpl; dialog->exec; }

diff -uNrd step7/opie-rdt.pro step8/opie-rdt.pro --- step7/opie-rdt.pro 2005-05-07 14:30:35.000000000 +0300 +++ step8/opie-rdt.pro 2005-05-18 23:33:51.174332470 +0300 @@ -4,8 +4,8 @@

INTERFACES += finddialog.ui -HEADERS += mainwindow.h -SOURCES += main.cpp mainwindow.cpp +HEADERS += mainwindow.h finddialogimpl.h +SOURCES += main.cpp mainwindow.cpp finddialogimpl.cpp
 * 1) Input

TARGET = opie-rdt INCLUDEPATH += $(OPIEDIR)/include

= Let's get our application compiled with Opie =

May be you already found that it is possible to start your compiled opie as full enviroment with commands like: qvfb& qpe

There you have all opie applications. Question is how to get our made opie application into that interface? Let's do it. We will do this folowing instructions given here - AddingYourAppToOpie.

So first we need to move our source tree to $(OPIEDIR)/noncore/apps/ with command mv our_app_dir $OPIEDIR/noncore/apps/opie-rdt