Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

GUI USING QT

EX NO : 4 DATE : 25.09.2012 AIM:

ALGORITHM:

[28]

Program & Output:


[fosslab@fosslabserver ~]$ echo "GUI Program using QT Toolkit" GUI Program using QT Toolkit [fosslab@fosslabserver ~]$ rpm -qi qt-devel Name : qt-devel Relocations: (not relocatable) Version : 4.7.0 Vendor: Fedora Project Release : 3.fc14 Build Date: Fri 24 Sep 2010 07:57:51 PM IST Install Date: Sun 10 Jun 2012 10:52:19 PM IST Build Host: x86-19.phx2.fedoraproject.org Group : Development/Libraries Source RPM: qt-4.7.0-3.fc14.src.rpm Size : 33374614 License: (LGPLv2 with exceptions or GPLv3 with exceptions) and ASL 2.0 and BSD and FTL and MIT Signature : RSA/SHA256, Fri 24 Sep 2010 10:11:35 PM IST, Key ID 421caddb97a1071f Packager : Fedora Project URL : http://www.qtsoftware.com/ Summary : Development files for the Qt toolkit Description : This package contains the files necessary to develop applications using the Qt toolkit. Includes: Qt Linguist [fosslab@fosslabserver ~]$ rpm -qi qt-demos Name : qt-demos Relocations: (not relocatable) Version : 4.7.0 Vendor: Fedora Project Release : 3.fc14 Build Date: Fri 24 Sep 2010 07:57:51 PM IST Install Date: Sun 10 Jun 2012 09:52:57 PM IST Build Host: x86-19.phx2.fedoraproject.org Group : Documentation Source RPM: qt-4.7.0-3.fc14.src.rpm Size : 9660943 License: (LGPLv2 with exceptions or GPLv3 with exceptions) and ASL 2.0 and BSD and FTL and MIT Signature : RSA/SHA256, Fri 24 Sep 2010 10:07:44 PM IST, Key ID 421caddb97a1071f Packager : Fedora Project URL : http://www.qtsoftware.com/ Summary : Demonstration applications for qt Description : Demonstration applications for qt. [fosslab@fosslabserver ~]$ rpm -qi qt-examples Name : qt-examples Relocations: (not relocatable) Version : 4.7.0 Vendor: Fedora Project Release : 3.fc14 Build Date: Fri 24 Sep 2010 07:57:51 PM IST Install Date: Sun 10 Jun 2012 09:51:56 PM IST Build Host: x86-19.phx2.fedoraproject.org Group : Documentation Source RPM: qt-4.7.0-3.fc14.src.rpm Size : 36024860 License: (LGPLv2 with exceptions or GPLv3 with exceptions) and ASL 2.0 and BSD and FTL and MIT Signature : RSA/SHA256, Fri 24 Sep 2010 10:48:11 PM IST, Key ID 421caddb97a1071f Packager : Fedora Project URL : http://www.qtsoftware.com/ Summary : Programming examples for qt Description : Programming examples for qt.

[29]

[fosslab@fosslabserver ~]$ rpm -qi qt-doc Name : qt-doc Relocations: (not relocatable) Version : 4.7.0 Vendor: Fedora Project Release : 3.fc14 Build Date: Fri 24 Sep 2010 07:56:41 PM IST Install Date: Sun 10 Jun 2012 10:49:21 PM IST Build Host: x86-13.phx2.fedoraproject.org Group : Documentation Source RPM: qt-4.7.0-3.fc14.src.rpm Size : 260488393 License: (LGPLv2 with exceptions or GPLv3 with exceptions) and ASL 2.0 and BSD and FTL and MIT Signature : RSA/SHA256, Fri 24 Sep 2010 10:37:14 PM IST, Key ID 421caddb97a1071f Packager : Fedora Project URL : http://www.qtsoftware.com/ Summary : API documentation for qt Description : API documentation for qt. Includes: Qt Assistant ============ [fosslab@fosslabserver ~]$ pwd /home/fosslab [fosslab@fosslabserver ~]$ ls -l total 48 drwxr-xr-x. 2 fosslab fosslab 4096 Jun 10 23:16 data drwxr-xr-x 5 fosslab fosslab 4096 Sep 21 11:38 Desktop drwxr-xr-x 2 fosslab fosslab 4096 Aug 4 15:32 Documents drwxr-xr-x 3 fosslab fosslab 4096 Sep 15 15:20 Downloads -rw-rw-r-- 1 fosslab fosslab 385 Sep 11 14:25 helloworld.py drwxr-xr-x 2 fosslab fosslab 4096 Jun 10 23:04 Music drwxr-xr-x 2 fosslab fosslab 4096 Jun 10 23:04 Pictures drwxr-xr-x 2 fosslab fosslab 4096 Jun 10 23:04 Public drwxr-xr-x. 3 fosslab fosslab 4096 Jun 10 23:16 public_html drwxr-xr-x. 2 fosslab fosslab 4096 Jun 10 23:16 share drwxr-xr-x 2 fosslab fosslab 4096 Jun 10 23:04 Templates drwxr-xr-x 2 fosslab fosslab 4096 Jun 10 23:04 Videos [fosslab@fosslabserver ~]$ mkdir ex7 [fosslab@fosslabserver ~]$ cd ex7 [fosslab@fosslabserver ex7]$ pwd /home/fosslab/ex7 [fosslab@fosslabserver ex7]$ mkdir qthello [fosslab@fosslabserver ex7]$ cd qthello [fosslab@fosslabserver qthello]$ vi qthello.cpp ~~~~~~~

[30]

//filename:qthello.cpp #include<QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; // resize window to 320*240 window.resize(320,240); window.setWindowzTitle("Hello World!"); // show thw window window.show(); //start the event loop return app.exec(); } :wq [fosslab@fosslabserver qthello]$ vi qthello.cpp [fosslab@fosslabserver qthello]$ qmake-qt4 -project [fosslab@fosslabserver qthello]$ qmake-qt4 [fosslab@fosslabserver qthello]$ make g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o qthello.o qthello.cpp g++ -Wl,-O1 -o qthello qthello.o -lQtGui -lQtCore -lpthread

[fosslab@fosslabserver qtbutton]$ ./qthello

============================== [fosslab@fosslabserver ~]$ mkdir qtbutton [fosslab@fosslabserver ~]$ cd qtbutton [fosslab@fosslabserver qtbutton]$ vi qtbutton.cpp [31]

#include<QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; // resize window to 320*240 window.resize(320,240); window.setWindowTitle("Window with a Button!"); // show the window window.show(); // create a button with the text "press me" as a child of the main window QPushButton *button = new QPushButton("Press me", &window); //move the button into position button->move(100, 100); button->show(); return app.exec(); } ~~~~~~~~~~~~~~ [fosslab@fosslabserver qtbutton]$ qmake-qt4 -project [fosslab@fosslabserver qtbutton]$ qmake-qt4 [fosslab@fosslabserver qtbutton]$ make g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o qtbutton.o qtbutton.cpp g++ -Wl,-O1 -o qtbutton qtbutton.o -lQtGui -lQtCore -lpthread

[fosslab@fosslabserver qtbutton]$ ls -l total 520 -rw-rw-r-- 1 fosslab fosslab 6583 Sep 21 12:02 Makefile -rwxrwxr-x 1 fosslab fosslab 229936 Sep 21 12:02 qtbutton -rw-rw-r-- 1 fosslab fosslab 493 Sep 21 12:02 qtbutton.cpp -rw-rw-r-- 1 fosslab fosslab 282432 Sep 21 12:02 qtbutton.o -rw-rw-r-- 1 fosslab fosslab 302 Sep 21 12:02 qtbutton.pro [fosslab@fosslabserver qtbutton]$ ./qtbutton

[32]

[fosslab@fosslabserver ~]$ cd ex7 [fosslab@fosslabserver ex7]$ mkdir qtlayout [fosslab@fosslabserver ex7]$ cd qtlayout [fosslab@fosslabserver qtlayout]$ vi qtlayout.cpp ~~~~~~ //filename qtlayout.cpp #include<QtGui> #include <QDesktopWidget> #include<QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; //create a label. Add the label and the line edit to it QLabel *label = new QLabel("Name:"); QLineEdit *lineEdit = new QLineEdit(); //create a layout QHBoxLayout *layout= new QHBoxLayout(); layout->addWidget(label); layout->addWidget(lineEdit); //Apply the layout to the main window window.setLayout(layout); window.setWindowTitle("Window Layout"); window.show(); return app.exec(); } ~~~~~~ [fosslab@fosslabserver qtlayout]$ qmake-qt4 -project [fosslab@fosslabserver qtlayout]$ qmake-qt4 [fosslab@fosslabserver qtlayout]$ make g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o qtlayout.o qtlayout.cpp g++ -Wl,-O1 -o qtlayout qtlayout.o -lQtGui -lQtCore -lpthread

[fosslab@fosslabserver qtlayout]$ ls -l total 524 [33]

-rw-rw-r-- 1 fosslab fosslab 6583 Sep 21 12:18 Makefile -rwxrwxr-x 1 fosslab fosslab 231894 Sep 21 12:18 qtlayout -rw-rw-r-- 1 fosslab fosslab 532 Sep 21 12:20 qtlayout.cpp -rw-rw-r-- 1 fosslab fosslab 284176 Sep 21 12:18 qtlayout.o -rw-rw-r-- 1 fosslab fosslab 302 Sep 21 12:17 qtlayout.pro [fosslab@fosslabserver qtlayout]$ ./qtlayout

========== [fosslab@fosslabserver ~]$ cd ex7 [fosslab@fosslabserver ex7]$ mkdir qtsignals [fosslab@fosslabserver ex7]$ pwd /home/fosslab/ex7 [fosslab@fosslabserver ex7]$ ls -l total 12 drwxrwxr-x 2 fosslab fosslab 4096 Sep 21 11:52 qthello drwxrwxr-x 2 fosslab fosslab 4096 Sep 21 12:20 qtlayout drwxrwxr-x 2 fosslab fosslab 4096 Sep 21 12:33 qtsignals [fosslab@fosslabserver ex7]$ echo "example for signals & slots" example for signals & slots [fosslab@fosslabserver ex7]$ echo "interaction between two widgets"interaction between two widgets [fosslab@fosslabserver ex7]$ echo "the entire application consist of 1. communicate.h 2. communicate.cpp 3.main.cpp" [fosslab@fosslabserver ex7]$ vi communicate.h ~~~~~ // filename: communicate.h #include <QWidget> #include<QApplication> #include<QPushButton> #include<QLabel> class Communicate: public QWidget { // the q_object macro_causes the moc tool to initialize code for signals and slots, run time type information and dynamic property system Q_OBJECT public: [34]

Communicate(QWidget *parent=0); //add a slot which allows widget communications private slots: void add(); private: QLabel *label; }; ~~~~~~~~ //filename:communicate.cpp #include "communicate.h" #include <QDesktopWidget> Communicate::Communicate(QWidget *parent) : QWidget(parent) { resize(180,140); QPushButton *plus= new QPushButton("+", this); plus->setGeometry(50,40,50,30); label =new QLabel("0",this); label->setGeometry(120, 40, 20, 30); connect(plus, SIGNAL(clicked()), this, SLOT(add())); } void Communicate::add() { int val=label->text().toInt(); val++; label->setText(QString::number(val)); } ~~~~~~~~ //filename: main.cpp #include "communicate.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Communicate window; window.setWindowTitle("Communicate"); window.show(); return app.exec();} [35]

[fosslab@fosslabserver qtsignals]$ vi communicate.h [fosslab@fosslabserver qtsignals]$ vi communicate.cpp [fosslab@fosslabserver qtsignals]$ vi main.cpp [fosslab@fosslabserver qtsignals]$ qmake-qt4 -project [fosslab@fosslabserver qtsignals]$ qmake-qt4 [fosslab@fosslabserver qtsignals]$ make g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o communicate.o communicate.cpp g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o main.o main.cpp /usr/lib/qt4/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. communicate.h -o moc_communicate.cpp g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o moc_communicate.o moc_communicate.cpp g++ -Wl,-O1 -o qtsignals communicate.o main.o moc_communicate.o [fosslab@fosslabserver qtsignals]$ [fosslab@fosslabserver qtsignals]$ ls -l total 708 -rw-rw-r-- 1 fosslab fosslab 442 Sep 21 13:06 communicate.cpp -rw-rw-r-- 1 fosslab fosslab 425 Sep 21 13:11 communicate.h -rw-rw-r-- 1 fosslab fosslab 147552 Sep 21 13:11 communicate.o -rw-rw-r-- 1 fosslab fosslab 184 Sep 21 13:09 main.cpp -rw-rw-r-- 1 fosslab fosslab 131132 Sep 21 13:11 main.o -rw-rw-r-- 1 fosslab fosslab 7170 Sep 21 13:11 Makefile -rw-rw-r-- 1 fosslab fosslab 2316 Sep 21 13:11 moc_communicate.cpp -rw-rw-r-- 1 fosslab fosslab 148188 Sep 21 13:11 moc_communicate.o -rwxrwxr-x 1 fosslab fosslab 256043 Sep 21 13:11 qtsignals -rw-rw-r-- 1 fosslab fosslab 339 Sep 21 13:11 qtsignals.pro [fosslab@fosslabserver qtsignals]$ ./qtsignals -lQtGui -lQtCore -lpthread

[36]

MY MENU [fosslab@fosslabserver qtmenu]$ vi mymenu.h #include <QMainWindow> class MyMenu : public QMainWindow { public: MyMenu(QWidget *parent=0); }; == [fosslab@fosslabserver qtmenu]$ vi mymenu.cpp #include <QMainWindow> class MyMenu : public QMainWindow { public: MyMenu(QWidget *parent=0); }; == [fosslab@fosslabserver qtmenu]$ vi mymain.cpp #include "mymenu.h" #include <QDesktopWidget> #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); [37]

MyMenu window; window.setWindowTitle("My Menu"); window.show(); return app.exec(); } === [fosslab@fosslabserver qtmenu]$ qmake-qt4 -project [fosslab@fosslabserver qtmenu]$ qmake-qt4 [fosslab@fosslabserver qtmenu]$ make g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o mymain.o mymain.cpp g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o mymenu.o mymenu.cpp g++ -Wl,-O1 -o qtmenu mymain.o mymenu.o [fosslab@fosslabserver qtmenu]$ ./qtmenu -lQtGui -lQtCore -lpthread

Result:

[38]

You might also like