Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "QT – Windows, menus, and such C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3."— Presentation transcript:

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

2 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

3 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

4 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

5 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();

6 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);

7 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; };

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

9 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; }

10 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()));

11 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

12 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;

13 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);

14 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); }

15 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()); }

16 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;

17 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()));

18 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.")); }

19 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()

20 Recall, findDialog

21 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 ()); }

22 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.) }

23 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’); }

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

25 End.


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

Similar presentations


Ads by Google