📄 mainwindow.cpp
字号:
/******************************************************************************** Copyright (C) 2004-2007 Trolltech ASA. All rights reserved.**** This file is part of the demonstration applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "mainwindow.h"#include "colorswatch.h"#include "toolbar.h"#include <QAction>#include <QLayout>#include <QMenu>#include <QMenuBar>#include <QStatusBar>#include <QTextEdit>#include <QFile>#include <QDataStream>#include <QFileDialog>#include <QMessageBox>#include <QSignalMapper>#include <QApplication>#include <QPainter>#include <QMouseEvent>#include <qdebug.h>static const char * const message = "<p><b>Qt Main Window Demo</b></p>" "<p>This is a demonstration of the QMainWindow, QToolBar and " "QDockWidget classes.</p>" "<p>The tool bar and dock widgets can be dragged around and rearranged " "using the mouse or via the menu.</p>" "<p>Each dock widget contains a colored frame and a context " "(right-click) menu.</p>"#ifdef Q_WS_MAC "<p>On Mac OS X, the \"Black\" dock widget has been created as a " "<em>Drawer</em>, which is a special kind of QDockWidget.</p>"#endif ;MainWindow::MainWindow(const QMap<QString, QSize> &customSizeHints, QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags){ setObjectName("MainWindow"); setWindowTitle("Qt Main Window Demo"); setupToolBar(); setupMenuBar(); setupDockWidgets(customSizeHints); QTextEdit *center = new QTextEdit(this); center->setReadOnly(true); center->setHtml(tr(message)); center->setMinimumSize(400, 205); setCentralWidget(center); statusBar()->showMessage(tr("Status Bar"));}void MainWindow::actionTriggered(QAction *action){ qDebug("action '%s' triggered", action->text().toLocal8Bit().data());}void MainWindow::setupToolBar(){ for (int i = 0; i < 3; ++i) { ToolBar *tb = new ToolBar(QString::fromLatin1("Tool Bar %1").arg(i + 1), this); toolBars.append(tb); addToolBar(tb); }}void MainWindow::setupMenuBar(){ QMenu *menu = menuBar()->addMenu(tr("&File")); QAction *action = menu->addAction(tr("Save layout...")); connect(action, SIGNAL(triggered()), this, SLOT(saveLayout())); action = menu->addAction(tr("Load layout...")); connect(action, SIGNAL(triggered()), this, SLOT(loadLayout())); action = menu->addAction(tr("Switch layout direction")); connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection())); menu->addSeparator(); menu->addAction(tr("&Quit"), this, SLOT(close())); mainWindowMenu = menuBar()->addMenu(tr("Main window")); action = mainWindowMenu->addAction(tr("Animated docks")); action->setCheckable(true); action->setChecked(dockOptions() & AnimatedDocks); connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); action = mainWindowMenu->addAction(tr("Allow nested docks")); action->setCheckable(true); action->setChecked(dockOptions() & AllowNestedDocks); connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); action = mainWindowMenu->addAction(tr("Allow tabbed docks")); action->setCheckable(true); action->setChecked(dockOptions() & AllowTabbedDocks); connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); action = mainWindowMenu->addAction(tr("Force tabbed docks")); action->setCheckable(true); action->setChecked(dockOptions() & ForceTabbedDocks); connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); action = mainWindowMenu->addAction(tr("Vertical tabs")); action->setCheckable(true); action->setChecked(dockOptions() & VerticalTabs); connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); QMenu *toolBarMenu = menuBar()->addMenu(tr("Tool bars")); for (int i = 0; i < toolBars.count(); ++i) toolBarMenu->addMenu(toolBars.at(i)->menu); dockWidgetMenu = menuBar()->addMenu(tr("&Dock Widgets"));}void MainWindow::setDockOptions(){ DockOptions opts; QList<QAction*> actions = mainWindowMenu->actions(); if (actions.at(0)->isChecked()) opts |= AnimatedDocks; if (actions.at(1)->isChecked()) opts |= AllowNestedDocks; if (actions.at(2)->isChecked()) opts |= AllowTabbedDocks; if (actions.at(3)->isChecked()) opts |= ForceTabbedDocks; if (actions.at(4)->isChecked()) opts |= VerticalTabs; QMainWindow::setDockOptions(opts);}void MainWindow::saveLayout(){ QString fileName = QFileDialog::getSaveFileName(this, tr("Save layout")); if (fileName.isEmpty()) return; QFile file(fileName); if (!file.open(QFile::WriteOnly)) { QString msg = tr("Failed to open %1\n%2") .arg(fileName) .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; } QByteArray geo_data = saveGeometry(); QByteArray layout_data = saveState(); bool ok = file.putChar((uchar)geo_data.size()); if (ok) ok = file.write(geo_data) == geo_data.size(); if (ok) ok = file.write(layout_data) == layout_data.size(); if (!ok) { QString msg = tr("Error writing to %1\n%2") .arg(fileName) .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; }}void MainWindow::loadLayout(){ QString fileName = QFileDialog::getOpenFileName(this, tr("Load layout")); if (fileName.isEmpty()) return; QFile file(fileName); if (!file.open(QFile::ReadOnly)) { QString msg = tr("Failed to open %1\n%2") .arg(fileName) .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; } uchar geo_size; QByteArray geo_data; QByteArray layout_data; bool ok = file.getChar((char*)&geo_size); if (ok) { geo_data = file.read(geo_size); ok = geo_data.size() == geo_size; } if (ok) { layout_data = file.readAll(); ok = layout_data.size() > 0; } if (ok) ok = restoreGeometry(geo_data);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -