⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qmainwindow.3qt

📁 Trolltech公司发布的基于C++图形开发环境
💻 3QT
📖 第 1 页 / 共 3 页
字号:
.nf.br        QPopupMenu * help = new QPopupMenu( this );.br        menuBar()->insertItem( "&Help", help );.br.br        help->insertItem( "&About", this, SLOT(about()), Key_F1 );.fi.PPHere we've added a new menu with one menu item. The menu has been inserted into the menu bar that QMainWindow provides by default and which is accessible through the menuBar() function. The slot will be called when the menu item is clicked..PP.nf.br        QToolBar * fileTools = new QToolBar( this, "file operations" );.br        fileTools->setLabel( "File Operations" );.fi.PP.nf.br        QToolButton * fileOpen.br            = new QToolButton( openIcon, "Open File", QString::null,.br                               this, SLOT(choose()), fileTools, "open file" );.fi.PPThis extract shows the creation of a toolbar with one toolbar button. QMainWindow supplies four dock areas for toolbars. When a toolbar is created as a child of a QMainWindow (or derived class) instance it will be placed in a dock area (the Top dock area by default). The slot will be called when the toolbar button is clicked. Any dock window can be added to a dock area either using addDockWindow(), or by creating a dock window with the QMainWindow as the parent..PP.nf.br        e = new QTextEdit( this, "editor" );.br        e->setFocus();.br        setCentralWidget( e );.br        statusBar()->message( "Ready", 2000 );.fi.PPHaving created the menus and toolbar we create an instance of the large central widget, give it the focus and set it as the main window's central widget. In the example we've also set the status bar, accessed via the statusBar() function, to an initial message which will be displayed for two seconds. Note that you can add additional widgets to the status bar, for example labels, to show further status information. See the QStatusBar documentation for details, particularly the addWidget() function..PPOften we want to synchronize a toolbar button with a menu item. For example, if the user clicks a 'bold' toolbar button we want the 'bold' menu item to be checked. This synchronization can be achieved automatically by creating actions and adding the actions to the toolbar and menu..PP.nf.br        QAction * fileOpenAction;.fi.PP.nf.br        fileOpenAction = new QAction( QPixmap( fileopen ), "&Open...",.br                                      CTRL+Key_O, this, "open" );.br        connect( fileOpenAction, SIGNAL( activated() ) , this, SLOT( choose() ) );.fi.PPHere we create an action with an icon which will be used in any menu and toolbar that the action is added to. We've also given the action a menu name, '&Open', and a keyboard shortcut. The connection that we have made will be used when the user clicks either the menu item \fIor\fR the toolbar button..PP.nf.br        QPopupMenu * file = new QPopupMenu( this );.br        menuBar()->insertItem( "&File", file );.fi.PP.nf.br        fileOpenAction->addTo( file );.fi.PPThe extract above shows the creation of a popup menu. We add the menu to the QMainWindow's menu bar and add our action..PP.nf.br        QToolBar * fileTools = new QToolBar( this, "file operations" );.br        fileTools->setLabel( "File Operations" );.br        fileOpenAction->addTo( fileTools );.fi.PPHere we create a new toolbar as a child of the QMainWindow and add our action to the toolbar..PPWe'll now explore the functionality offered by QMainWindow..PPThe main window will take care of the dock areas, and the geometry of the central widget, but all other aspects of the central widget are left to you. QMainWindow automatically detects the creation of a menu bar or status bar if you specify the QMainWindow as parent, or you can use the provided menuBar() and statusBar() functions. The functions menuBar() and statusBar() create a suitable widget if one doesn't exist, and update the window's layout to make space..PPQMainWindow provides a QToolTipGroup connected to the status bar. The function toolTipGroup() provides access to the default QToolTipGroup. It isn't possible to set a different tool tip group..PPNew dock windows and toolbars can be added to a QMainWindow using addDockWindow(). Dock windows can be moved using moveDockWindow() and removed with removeDockWindow(). QMainWindow allows default dock window (toolbar) docking in all its dock areas (Top, Left, Right, Bottom). You can use setDockEnabled() to enable and disable docking areas for dock windows. When adding or moving dock windows you can specify their 'edge' (dock area). The currently available edges are: Top, Left, Right, Bottom, Minimized (effectively a 'hidden' dock area) and TornOff (floating). See Qt::Dock for an explanation of these areas. Note that the *ToolBar functions are included for backward compatibility; all new code should use the *DockWindow functions. QToolbar is a subclass of QDockWindow so all functions that work with dock windows work on toolbars in the same way..PPIf the user clicks the close button, then the dock window is hidden. A dock window can be hidden or unhidden by the user by right clicking a dock area and clicking the name of the relevant dock window on the pop up dock window menu. This menu lists the names of every dock window; visible dock windows have a tick beside their names. The dock window menu is created automatically as required by createDockWindowMenu(). Since it may not always be appropriate for a dock window to appear on this menu the setAppropriate() function is used to inform the main window whether or not the dock window menu should include a particular dock window. Double clicking a dock window handle (usually on the left-hand side of the dock window) undocks (floats) the dock window. Double clicking a floating dock window's titlebar will dock the floating dock window. (See also QMainWindow::DockWindows.).PPSome functions change the appearance of a QMainWindow globally:.TPQDockWindow::setHorizontalStretchable() and QDockWindow::setVerticalStretchable() are used to make specific dock windows or toolbars stretchable..TPsetUsesBigPixmaps() is used to set whether tool buttons should draw small or large pixmaps (see QIconSet for more information)..TPsetUsesTextLabel() is used to set whether tool buttons should display a textual label in addition to pixmaps (see QToolButton for more information)..PPThe user can drag dock windows into any enabled docking area. Dock windows can also be dragged \fIwithin\fR a docking area, for example to rearrange the order of some toolbars. Dock windows can also be dragged outside any docking area (undocked or 'floated'). Being able to drag dock windows can be enabled (the default) and disabled using setDockWindowsMovable()..PPThe Minimized edge is a hidden dock area. If this dock area is enabled the user can hide (minimize) a dock window or show (restore) a minimized dock window by clicking the dock window handle. If the user hovers the mouse cursor over one of the handles, the caption of the dock window is displayed in a tool tip (see QDockWindow::caption() or QToolBar::label()), so if you enable the Minimized dock area, it is best to specify a meaningful caption or label for each dock window. To minimize a dock window programmatically use moveDockWindow() with an edge of Minimized..PPDock windows are moved transparently by default, i.e. during the drag an outline rectangle is drawn on the screen representing the position of the dock window as it moves. If you want the dock window to be shown normally whilst it is moved use setOpaqueMoving()..PPThe location of a dock window, i.e. its dock area and position within the dock area, can be determined by calling getLocation(). Movable dock windows can be lined up to minimize wasted space with lineUpDockWindows(). Pointers to the dock areas are available from topDock(), leftDock(), rightDock() and bottomDock(). A customize menu item is added to the pop up dock window menu if isCustomizable() returns TRUE; it returns FALSE by default. Reimplement isCustomizable() and customize() if you want to offer this extra menu item, for example, to allow the user to change settings relating to the main window and its toolbars and dock windows..PPThe main window's menu bar is fixed (at the top) by default. If you want a movable menu bar, create a QMenuBar as a stretchable widget inside its own movable dock window and restrict this dock window to only live within the Top or Bottom dock:.PP.nf.br    QToolBar *tb = new QToolBar( this );.br    addDockWindow( tb, tr( "Menubar" ), Top, FALSE );.br    QMenuBar *mb = new QMenuBar( tb );.br    mb->setFrameStyle( QFrame::NoFrame );.br    tb->setStretchableWidget( mb );.br    setDockEnabled( tb, Left, FALSE );.br    setDockEnabled( tb, Right, FALSE );.br.fi.PPAn application with multiple dock windows can choose to save the current dock window layout in order to restore it later, e.g. in the next session. You can do this by using the streaming operators for QMainWindow..PPTo save the layout and positions of all the dock windows do this:.PP.nf.br    QFile file( filename );.br    if ( file.open( IO_WriteOnly ) ) {.br        QTextStream stream( &file );.br        stream << *mainWindow;.br        file.close();.br    }.br.fi.PPTo restore the dock window positions and sizes (normally when the application is next started), do following:.PP.nf.br    QFile file( filename );.br    if ( file.open( IO_ReadOnly ) ) {.br        QTextStream stream( &file );.br        stream >> *mainWindow;.br        file.close();.br    }.br.fi.PPThe QSettings class can be used in conjunction with the streaming operators to store the application's settings..PPQMainWindow's management of dock windows and toolbars is done transparently behind-the-scenes by QDockArea..PPFor multi-document interfaces (MDI), use a QWorkspace as the central widget..PPAdding dock windows, e.g. toolbars, to QMainWindow's dock areas is straightforward. If the supplied dock areas are not sufficient for your application we suggest that you create a QWidget subclass and add your own dock areas (see QDockArea) to the subclass since QMainWindow provides functionality specific to the standard dock areas it provides..PP.ce 1.B "[Image Omitted]".PP.ce 1.B "[Image Omitted]".PPSee also QToolBar, QDockWindow, QStatusBar, QAction, QMenuBar, QPopupMenu, QToolTipGroup, QDialog, and Main Window and Related Classes..SS "Member Type Documentation".SH "QMainWindow::DockWindows"Right-clicking a dock area will pop-up the dock window menu (createDockWindowMenu() is called automatically). When called in code you can specify what items should appear on the menu with this enum..TP\fCQMainWindow::OnlyToolBars\fR - The menu will list all the toolbars, but not any other dock windows..TP\fCQMainWindow::NoToolBars\fR - The menu will list dock windows but not toolbars..TP\fCQMainWindow::AllDockWindows\fR - The menu will list all toolbars and other dock windows. (This is the default.).SH MEMBER FUNCTION DOCUMENTATION.SH "QMainWindow::QMainWindow ( QWidget * parent = 0, const char * name = 0, WFlags f = WType_TopLevel )"Constructs an empty main window. The \fIparent\fR, \fIname\fR and widget flags \fIf\fR, are passed on to the QWidget constructor..PPBy default, the widget flags are set to WType_TopLevel rather than 0 as they are with QWidget. If you don't want your QMainWindow to be a top level widget then you will need to set \fIf\fR to 0..SH "QMainWindow::~QMainWindow ()"Destroys the object and frees any allocated resources..SH "void QMainWindow::addDockWindow ( QDockWindow * dockWindow, Dock edge = DockTop, bool newLine = FALSE )\fC [virtual]\fR"Adds \fIdockWindow\fR to the \fIedge\fR dock area..PPIf \fInewLine\fR is FALSE (the default) then the \fIdockWindow\fR is added at the end of the \fIedge\fR. For vertical edges the end is at the bottom, for horizontal edges (including Minimized) the end is at the right. If \fInewLine\fR is TRUE a new line of dock windows is started with \fIdockWindow\fR as the first (left-most and top-most) dock window..PPIf \fIdockWindow\fR is managed by another main window, it is first removed from that window..SH "void QMainWindow::addDockWindow ( QDockWindow * dockWindow, const QString & label, Dock edge = DockTop, bool newLine = FALSE )\fC [virtual]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAdds \fIdockWindow\fR to the dock area with label \fIlabel\fR..PPIf \fInewLine\fR is FALSE (the default) the \fIdockWindow\fR is added at the end of the \fIedge\fR. For vertical edges the end is at the bottom, for horizontal edges (including Minimized) the end is at the right. If \fInewLine\fR is TRUE a new line of dock windows is started with \fIdockWindow\fR as the first (left-most and top-most) dock window..PPIf \fIdockWindow\fR is managed by another main window, it is first removed from that window..SH "void QMainWindow::addToolBar ( QDockWindow *, Dock = DockTop, bool newLine = FALSE )"\fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code..SH "void QMainWindow::addToolBar ( QDockWindow *, const QString & label, Dock = DockTop, bool newLine = FALSE )"\fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code..PPThis is an overloaded member function, provided for convenience. It behaves essentially like the above function..SH "bool QMainWindow::appropriate ( QDockWindow * dw ) const"Returns TRUE if it is appropriate to include a menu item for the \fIdw\fR dock window in the dock window menu; otherwise returns FALSE..PPThe user is able to change the state (show or hide) a dock window that has a menu item by clicking the item..PPCall setAppropriate() to indicate whether or not a particular dock window should appear on the popup menu..PPSee also setAppropriate()..SH "QDockArea * QMainWindow::bottomDock () const"Returns a pointer the Bottom dock area.PPSee also topDock(), leftDock(), and rightDock()..SH "QWidget * QMainWindow::centralWidget () const"Returns a pointer to the main window's central widget..PPThe central widget is surrounded by the left, top, right and bottom dock areas. The menu bar is above the top dock area..PPSee also setCentralWidget()..PPExample: qfd/qfd.cpp..SH "void QMainWindow::childEvent ( QChildEvent * e )\fC [virtual protected]\fR"Monitors events, recieved in \fIe\fR, to ensure the layout is updated..PPReimplemented from QObject..SH "QPopupMenu * QMainWindow::createDockWindowMenu ( DockWindows dockWindows = AllDockWindows ) const"Creates the dock window menu which contains all toolbars (if \fIdockWindows\fR is OnlyToolBars ), all dock windows (if \fIdockWindows\fR is NoToolBars) or all toolbars and dock windows (if \fIdockWindows\fR is AllDockWindows - the default)..PPThis function is called internally when necessary, e.g. when the user right clicks a dock area (providing isDockMenuEnabled() returns TRUE)..PPThe menu items representing the toolbars and dock windows are checkable. The visible dock windows are checked and the hidden dock windows are unchecked. The user can click a menu item to change its state (show or hide the dock window)..PPThe list and the state are always kept up-to-date..PPToolbars and dock windows which are not appropriate in the current context (see setAppropriate()) are not listed in the menu..PPThe menu also has a menu item for lining up the dock windows..PPIf isCustomizable() returns TRUE, a Customize menu item is added to the menu, which if clicked will call customize(). The isCustomizable() function we provide returns FALSE and customize() does nothing, so they must be reimplemented in a subclass to be useful..SH "void QMainWindow::customize ()\fC [virtual slot]\fR"This function is called when the user clicks the Customize menu item on the dock window menu..PPThe customize menu item will only appear if isCustomizable() returns TRUE (it returns FALSE by default)..PPThe function is intended, for example, to provide the user with a means of telling the application that they wish to customize the main window, dock windows or dock areas..PPThe default implementation does nothing and the Customize menu item is not shown on the right-click menu by default. If you want the item to appear then reimplement isCustomizable() to return TRUE, and reimplement this function to do whatever you want..PPSee also isCustomizable()..SH "void QMainWindow::dockWindowPositionChanged ( QDockWindow * dockWindow )\fC [signal]\fR"This signal is emitted when the \fIdockWindow\fR has changed its position. A change in position occurs when a dock window is moved within its dock area or moved to another dock area (including the Minimized and \fCTearOff\fR dock areas)..PPSee also getLocation()..SH "QPtrList<QDockWindow> QMainWindow::dockWindows ( Dock dock ) const"Returns a list of all the dock windows which are in the \fIdock\fR dock area, regardless of their state..PPFor example, the DockTornOff dock area may contain closed dock windows but these are returned along with the visible dock windows..SH "QPtrList<QDockWindow> QMainWindow::dockWindows () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns the list of dock windows which belong to this main window, regardless of which dock area they are in or what their state is, (e.g. irrespective of whether they are visible or not).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -