QT – Windows, menus, and such C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3.

Slides:



Advertisements
Similar presentations
Introduction to Microsoft Office 2007 with focus on MS Word
Advertisements

QT GUI Programming CS340 – Software Design © 2009 – Jason Leigh University of Illinois at Chicago.
Using Macros and Visual Basic for Applications (VBA) with Excel
110-H1 More VB Tools Creating a Menu: What is a menu? a group of (related) commands displayed at at the top of an application Top level menu Disabled command.
© by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Introduction to the Visual C# 2005 Express Edition IDE.
Using the Visual Basic Editor Visual Basic for Applications 1.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Creating Menu. Objectives Create a menu system for a form –Create a menu –Create a menu titles –Create a menu items –Create a submenu –Modify menu –Edit.
Chapter 4: Working with Windows Types of Windows –Program Windows –Message Boxes –Dialog Boxes Elements of a Window –Window Panes –Scroll Bars –Menus –Tool.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
PYP002 Intro.to Computer Science Microsoft Word1 Lab 07 Creating Documents with Efficiency and Consistency.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Chapter 3 Working with Symbols and Interactivity.
Qt – Introduction C++ GUI Programming with Qt 3
Blanchette and Summerfield, Ch. 2
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
Multiple Forms, Standard Modules, And Menus
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
© 2011 Delmar, Cengage Learning Chapter 3 Working with Symbols and Interactivity.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
XP New Perspectives on Windows XP Tutorial 1 Exploring the Basics.
Rapid GUI Programming with Python and Qt Main Window By Raed S. Rasheed Main Window By Raed S. Rasheed 1.
Productivity Programs Common Features and Commands.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Copyright © 2010 Wolters Kluwer Health | Lippincott Williams & Wilkins Introduction to Windows Chapter 2.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Adding a New Option to the Framework. Introduction This is intended as a step by step guide to adding a new action to the menu or toolbar. The order of.
Key Applications Module Lesson 21 — Access Essentials
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
XP New Perspectives on Microsoft Windows 2000 Professional Windows 2000 Tutorial 1 1 Microsoft Windows 2000 Professional Tutorial 1 – Exploring the Basics.
Basic Editing Lesson 2.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
 2006 Pearson Education, Inc. All rights reserved Introduction to the Visual C# 2005 Express Edition IDE.
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
Chapter 2 – Introduction to the Visual Studio .NET IDE
© 2010 Pearson Education, Inc. | Publishing as Prentice Hall1 Computer Literacy for IC 3 Unit 2: Using Productivity Software Chapter 1: Starting with Microsoft.
QT – Dialog C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3.
Creating a Qt Widget Based Application From Qt Creator Manual.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
Confidential © 2008 Teleca AB Creating Custom Widgets Author: Patricia Jiang Date: July 29, 2010 Version: 1.0 Teleca Chengdu.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
QT – Introduction C++ GUI Programming with Qt 4
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
Chapter 7 Controls. List box control 3 List Box Control(1/8) Listbox control: –Display lists of text strings called items –Optionally sort the items.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
LCG AA Meeting 30 June 2004 Ilka Antcheva Graphics User Interface in ROOT.
XP New Perspectives on Microsoft Windows XP Tutorial 1 1 Microsoft Windows XP Creating a Web Site Tutorial 1.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
 2007 Pearson Education, Inc. All rights reserved Introduction to the Visual Basic Express 2005 IDE.
Adobe ® Photoshop ® CS6 Chapter 1 Editing a Photo.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
Chapter 8 Using Document Collaboration, Integration, and Charting Tools Microsoft Word 2013.
Excel Tutorial 8 Developing an Excel Application
Chapter 2 – Introduction to the Visual Studio .NET IDE
Introduction to the Visual C# 2005 Express Edition IDE
Understand Windows Forms Applications and Console-based Applications
Predefined Dialog Boxes
Chapter 2 – Introduction to the Visual Studio .NET IDE
Custom Widgets & Events
Presentation transcript:

QT – Windows, menus, and such C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3

Overview Another example of essential Qt concepts – Addressbook tutorial Qt – “main window” Subclassing – many “actions” / methods Menus, tool bars –Selection of item creates action, using signals and slots Implementing functionality – in actions “Context menus” – right button Modal vs. modeless dialogs

Main Windows (quick look – overview) For Qt (and other systems) “main window” refers to a kind of “executive” control and coordination of elements –Dialogs, menus, tool bars, status bars After this, final essential that remains is to look carefully at implementation of application functionality Blanchette and Summerfield build on spreadsheet dialogs Will take a quick look at example

Creating Application’s Main Window Subclassing QMainWindow // mainwindow.h - Create main window as subclass of QMainWindow #include class QAction; class QLabel; class FindDialog; class Spreadsheet; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0, const char *name = 0); protected: void closeEvent(QCloseEvent *event);// reimplement to modify void contextMenuEvent(QContextMenuEvent *event);// “ right-click menu

mainwindow.h, 2 Subclassing QMainWindow – Menus & slots for menu options // Most menu options are implemented as private slots (methods, functions) // in MainWindow // Below and on next page defines slots for this menu structure // (will see more of slot definitions next week) private slots: void newFile(); void open(); bool save(); bool saveAs(); void find(); void goToCell(); void sort(); void about();

mainwindow.h, 3 Subclassing QMainWindow – More slots // … and so on, … void updateCellIndicators(); void spreadsheetModified(); void openRecentFile(int param); private: void createActions(); void createMenus(); void createToolBars(); void createStatusBar(); void readSettings(); void writeSettings(); bool maybeSave(); void loadFile(const QString &fileName); void saveFile(const QString &fileName); void setCurrentFile(const QString &fileName); void updateRecentFileItems(); QString strippedName(const QString &fullFileName);

mainwindow.h, 4 Subclassing QMainWindow – More slots – to support UI // … and so forth (more later) Spreadsheet *spreadsheet; FindDialog *findDialog; QLabel *locationLabel; QLabel *formulaLabel; QLabel *modLabel; QStringList recentFiles; QString curFile; QString fileFilters; bool modified; enum { MaxRecentFiles = 5 }; int recentFileIds[MaxRecentFiles]; QPopupMenu *fileMenu; QPopupMenu *editMenu; QPopupMenu *selectSubMenu; QPopupMenu *toolsMenu; QPopupMenu *optionsMenu; QPopupMenu *helpMenu; QToolBar *fileToolBar; QToolBar *editToolBar; QAction *newAct; QAction *openAct; QAction *saveAct; ··· QAction *aboutAct; QAction *aboutQtAct; };

And that’s “all” there is to mainwindow.h! … and mainwindow.cpp holds the implementation

mainwindow.cpp “implementation” – should be familiar structure MainWindow::MainWindow(QWidget *parent, const char *name) : QMainWindow(parent, name) { // Below creates spreadsheet widget and it’s constituent widgets spreadsheet = new Spreadsheet(this); // class to be defined later setCentralWidget(spreadsheet); createActions(); // next slide createMenus(); createToolBars(); createStatusBar(); readSettings(); setCaption(tr("Spreadsheet")); setIcon(QPixmap::fromMimeSource("icon.png")); findDialog = 0; fileFilters = tr("Spreadsheet files (*.sp)"); modified = false; }

mainwindow.cpp creating menu and toolbar elements, and actions A Qt “action” is item that can be added to menu or toolbar To create menus and toolbars in Qt: –Create actions –Add actions to menus –Add actions to toolbars Below is Qt-ese for the menu element “New”: void MainWindow::createActions() { newAct = new QAction(tr("&New"), tr("Ctrl+N"), this); newAct->setIconSet(QPixmap::fromMimeSource("new.png")); newAct->setStatusTip(tr("Create a new spreadsheet file")); connect(newAct, SIGNAL(activated()), this, SLOT(newFile()));

mainwindow.cpp creating menu and toolbar elements, and actions, 2 // Show Grid is toggle (Boolean), rendered with check mark showGridAct = new QAction(tr("&Show Grid"), 0, this); showGridAct->setToggleAction(true); showGridAct->setOn(spreadsheet->showGrid()); showGridAct->setStatusTip(tr("Show or hide spreadsheet "grid")); connect(showGridAct, SIGNAL(toggled(bool)), spreadsheet, SLOT(setShowGrid(bool))); // For “About”: aboutQtAct = new QAction(tr("About &Qt"), 0, this); aboutQtAct->setStatusTip(tr("Show the Qt library’s About box")); connect(aboutQtAct, SIGNAL(activated()), qApp, SLOT(aboutQt())); } // Have now created all actions

mainwindow.cpp Building the menu system, “File” Actions are invoked through menu system –All menus are instances of QPopupMenu void MainWindow::createMenus() { fileMenu = new QPopupMenu(this);// Create file menu newAct->addTo(fileMenu);// Add “actions” to it openAct->addTo(fileMenu); saveAct->addTo(fileMenu); saveAsAct->addTo(fileMenu); fileMenu->insertSeparator(); exitAct->addTo(fileMenu); for (int i = 0; i < MaxRecentFiles; ++i) recentFileIds[i] = -1;

mainwindow.cpp Building the menu system, “Edit” Edit menu includes a submenu, otherwise, “same song, 2 nd …” –Submenu simply has parent and inserted where it is to appear editMenu = new QPopupMenu(this); cutAct->addTo(editMenu); copyAct->addTo(editMenu); pasteAct->addTo(editMenu); deleteAct->addTo(editMenu); selectSubMenu = new QPopupMenu(this); selectRowAct->addTo(selectSubMenu); selectColumnAct->addTo(selectSubMenu); selectAllAct->addTo(selectSubMenu); editMenu->insertItem(tr("&Select"), selectSubMenu);// submenu editMenu->insertSeparator(); findAct->addTo(editMenu); goToCellAct->addTo(editMenu);

mainwindow.cpp toolbars Creating toolbars is very similar to creating menus: void MainWindow::createToolBars() { fileToolBar = new QToolBar(tr("File"), this); newAct->addTo(fileToolBar); openAct->addTo(fileToolBar); saveAct->addTo(fileToolBar); editToolBar = new QToolBar(tr("Edit"), this); cutAct->addTo(editToolBar); copyAct->addTo(editToolBar); pasteAct->addTo(editToolBar); editToolBar->addSeparator(); findAct->addTo(editToolBar); goToCellAct->addTo(editToolBar); }

mainwindow.cpp “ context menu” – invoke w/rt button, creating User right mouse button Reimplement QWidget :: contextMenuEvent void MainWindow::contextMenuEvent(QContextMenuEvent *event) { QPopupMenu contextMenu(this); cutAct->addTo(&contextMenu); copyAct->addTo(&contextMenu); pasteAct->addTo(&contextMenu); // “exec” causes to be shown at loc contextMenu.exec(event->globalPos()); }

mainwindow.cpp context menu, event handling “Events are generated by Qt’s kernel to report mouse clicks, key presses, resize requests, and similar occurrences.” –As noted, will reimplement QWidget :: contextMenuEvent to handle event QPopupMenu *contextMenu = new QPopupMenu(this); cutAct->addTo(contextMenu); copyAct->addTo(contextMenu); pasteAct->addTo(contextMenu); contextMenu->exec(event->globalPos()); delete contextMenu;

Implementing Functionality What happens when menu item selected? Recall, basic control structure: - when user selects a menu item, slot/member-function is called Connect slot to “New” menu item: newAct = new QAction(tr("&New"), tr("Ctrl+N"), this); newAct->setIconSet(QPixmap::fromMimeSource("new.png")); newAct->setStatusTip(tr("Create a new spreadsheet file")); connect(newAct, SIGNAL(activated()), this, SLOT(newFile())); Connect slot to “About” menu item: aboutQtAct = new QAction(tr("About &Qt"), 0, this); aboutQtAct->setStatusTip(tr("Show the Qt library’s About box")); connect(aboutQtAct, SIGNAL(activated()), qApp, SLOT(aboutQt()));

Implementing Functionality What happens when menu item selected? Functionality can be simple or complex –In fact, here, newFile implementation is … “complex and rich” –Will leave it to student to master such topics, as interest dictates –Today, getting feel for program elements, structure, and interactions is goal Functionality “built in”, e.g., “About” –connect(aboutQtAct, SIGNAL(activated()), qApp, SLOT(aboutQt())); –QMessageBox::about() - a “convenience function” void MainWindow::about() { QMessageBox::about(this, tr("About Spreadsheet"), tr(" Spreadsheet 1.0 " " Copyright © 2003 Software Inc." " Spreadsheet is a small application that " "demonstrates QAction, QMainWindow, " " QMenuBar, QStatusBar, " " QToolBar, and many other Qt classes.")); }

Using Dialogs almost done … How to create, initialize, execute, and respond to choices Dialog (window) “modes” –Terminology not specific to Qt –Modeless: executes independently of other windows –Modal: executes (pops up) when invoked and nothing else executes until closed Qt dialogs and typical user action handling –Modeless Have their signals connected to slots that respond to user action Invoked using show() –Modal Handle user action within dialog, no signals and slots Invoked using exec()

Recall, findDialog

Define finddialog slots, or functions // Called when user clicks Find button, emits signal findPrevious() or findNext void FindDialog ::findClicked() { QString text = lineEdit->text(); Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; if (backwardCheckBox->isChecked()) { emit findPrevious(text, cs); } else { emit findNext(text, cs);// emit keyword (macro) specific to Qt } // Called whenever user changes text in line editor, enables button if there is text in line editor void FindDialog::enableFindButton(const QString &text) { findButton->setEnabled (!text.isEmpty ()); }

A Modeless Dialog (box, window) findDialog - window (dialog) that enables user to search for text –Invoked when user clicks Edit|Find to “pop up” (execute) Find dialog (box) –Recall functionality implemented in last chapter –Basically, “just run it” void MainWindow::find() { if (!findDialog) { // if 1 st execution findDialog = new FindDialog(this); connect(findDialog, SIGNAL(findNext(const QString &, bool)), spreadsheet, SLOT(findNext(const QString &, bool))); connect(findDialog, SIGNAL(findPrev(const QString &, bool)), spreadsheet, SLOT(findPrev(const QString &, bool))); } findDialog->show();// make not hidden (if so) findDialog->raise();// may need to bring from “underneath” other wins findDialog->setActiveWindow();// make active (color title bar, give focus, etc.) }

A Modal Dialog When invoke modal dialogs using exec(), typically don’t need to set up any signal-slot connection Qdialog::exec() returns true, if dialog accepted, false otherwise –GoToCellDialog - Created with Designer in Ch. 2 –OK was connected to accept(), Cancel to reject() –Will need to essentially implement all functionality void MainWindow::goToCell() { GoToCellDialog dialog(this); if (dialog.exec()) { QString str = dialog.lineEdit->text();// get text from input widget spreadsheet->setCurrentCell(str.mid(1).toInt() - 1,// set… defined in program str[0].upper().unicode() - ’A’); }

Other Dialogs for Spreadsheet Many examples provided in chapter Some straightforward Some arcane When in doubt … keep it simple

End.