转帖|使用教程|编辑:鲍佳佳|2021-08-09 11:25:00.007|阅读 130 次
概述:该示例展示了如何将特殊小部件嵌入到应用程序 - BackstageView 中。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
QtitanRibbon组件实现了Microsoft Ribbon for Qt的概念。该组件设计用于使用Qt开发的任何类型的商业应用。与Qt一起,该组件为获得应用程序提供了独特的机会,该应用程序对于全球存在的三个主要平台(Windows,Linux和Mac OSX)均可打造现代化外观。
该示例展示了如何将特殊小部件嵌入到应用程序 - BackstageView 中。
BackstageView 是一个具有最大 z 顺序并与整个窗口重叠的小部件。它位于按应用程序中经常使用的菜单主题分组的按钮。
功能区/BackstageView/BackstageView.pro
TEMPLATE = app TARGET = "Ribbon_"$$member(TARGET, 0) QTITANDIR = $$quote($$(QTITANDIR)) isEmpty(QTITANDIR):QTITANDIR = $$quote($$PWD/../../../) include($$QTITANDIR/src/shared/qtitanribbon.pri) DESTDIR = $$QTITANDIR/bin DESTDIR = $$member(DESTDIR, 0)$$QTITAN_LIB_PREFIX DESTDIR = "$$DESTDIR" !debug_and_release|build_pass { CONFIG(debug, debug|release) { TARGET = $$member(TARGET, 0)d } } HEADERS = mainwindow.h \ backstagepages.h SOURCES = main.cpp \ mainwindow.cpp \ backstagepages.cpp FORMS = infopage.ui \ savepage.ui \ helppage.ui \ saveemailpage.ui \ savewebpage.ui \ RESOURCES = BackstageView.qrc include($$PWD/../../shared/aboutdialog.pri) include($$PWD/../../ribbon/shared/DemoRibbonWindow.pri) macx { CONFIG-=app_bundle } !lessThan(QT_VER_MAJ, 5) { QT += printsupport }BackstageView.py
import sys, os sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../shared") from PySide2 import QtCore from PySide2.QtCore import Qt, QPoint, QSettings from PySide2.QtGui import QIcon, QPixmap, QKeySequence from PySide2.QtWidgets import (QWidget, QApplication, QAction, QMenu, QListWidget, QCheckBox, QDockWidget, QGroupBox, QVBoxLayout, QMessageBox, QListWidgetItem) from DemoRibbonWindow import DemoRibbonWindow from backstagepages import (RibbonBackstageInfoPage, RibbonBackstageSaveEmailPage, RibbonBackstageSaveWebPage, RibbonBackstageSavePage, RibbonBackstageAboutPage) from DevMachines import QtitanBase from DevMachines.QtitanBase import Qtitan, CommonStyle from DevMachines.QtitanRibbon import RibbonWorkspace, RibbonBackstageView import BackstageView_rc class MainWindow(DemoRibbonWindow): def __init__(self): DemoRibbonWindow.__init__(self) self.setWindowTitle(self.tr("PyQtitanRibbon Backstage Sample")) self.setCentralWidget(RibbonWorkspace()) self.createBackstage() self.createQuickAccessBar() self.createRibbon() self.createDockWindows() self.statusBar() self.ribbonBar().setTitleBackground(QPixmap(":/res/background.png")) self.move(QPoint(200, 200)) geom = QApplication.desktop().availableGeometry() self.resize(2 * geom.width() / 3, 2 * geom.height() / 3) settings = QSettings(QSettings.UserScope, "Developer Machines") files = settings.value("recentFileList") settings.setValue("recentFileList", files) #emit updateRecentFileActions(files) self.ribbonBar().systemButton().setToolButtonStyle(Qt.ToolButtonTextOnly) def createBackstage(self): iconLogo = QIcon() iconLogo.addPixmap(QPixmap(":/res/file.png")) iconLogo.addPixmap(QPixmap(":/res/qtitanlogo32x32.png")) actionFile = self.ribbonBar().addSystemButton(iconLogo, self.tr("&File")) actionFile.setToolTip(self.tr("Click here to see everything<br />you can do with your<br />document")) backstage = RibbonBackstageView(self.ribbonBar()) #connect(backstage, SIGNAL(aboutToShow()), this, SLOT(showBackstage())); #connect(backstage, SIGNAL(aboutToHide()), this, SLOT(hideBackstage())); saveFile = backstage.addAction(DemoRibbonWindow.createIcon(DemoRibbonWindow.Image_Save), self.tr("&Save")) saveFile.setShortcut(self.tr("Ctrl+S")) saveFile.setToolTip(self.tr("Save")) saveFile.setStatusTip(self.tr("Save the active document")) #connect(saveFile, SIGNAL(triggered()), this, SLOT(save())); saveAsFile = backstage.addAction(DemoRibbonWindow.createIcon(DemoRibbonWindow.Image_SaveAs), self.tr("Save &As")) saveAsFile.setToolTip(self.tr("Save As")) saveAsFile.setStatusTip(self.tr("Save the active document with a new name")) #connect(saveAsFile, SIGNAL(triggered()), this, SLOT(save())) openFile = backstage.addAction(DemoRibbonWindow.createIcon(DemoRibbonWindow.Image_Open), self.tr("&Open")) openFile.setShortcut(self.tr("Ctrl+O")) openFile.setToolTip(self.tr("Open")) openFile.setStatusTip(self.tr("Open an existing document")) #connect(openFile, SIGNAL(triggered()), this, SLOT(open())); infoPage = RibbonBackstageInfoPage(self) backstage.addPage(infoPage) savePage = RibbonBackstageSavePage(self) backstage.addPage(savePage) aboutPage = RibbonBackstageAboutPage(self) backstage.addPage(aboutPage) actOptions = backstage.addAction(QIcon(":/res/smallOptions.png"), self.tr("Options")) #connect(actOptions, SIGNAL(triggered()), this, SLOT(pressButton())); backstage.addSeparator() actExit = backstage.addAction(QIcon(":/res/smallClose.png"), self.tr("Exit")) # connect(actExit, SIGNAL(triggered()), this, SLOT(close())); sysButton = self.ribbonBar().systemButton() if sysButton != None: sysButton.setBackstage(backstage) backstage.open() def createQuickAccessBar(self): quickAccessBar = self.ribbonBar().quickAccessBar() if quickAccessBar != None: action = quickAccessBar.actionCustomizeButton() action.setToolTip(self.tr("Customize Quick Access Bar")) smallButton = quickAccessBar.addAction(DemoRibbonWindow.createIcon(DemoRibbonWindow.Image_New),self.tr("New")) smallButton.setToolTip(self.tr("Create a new document")) self.connect(smallButton, QtCore.SIGNAL("triggered()"), self.pressButton) quickAccessBar.setActionVisible(smallButton, False) smallButton = quickAccessBar.addAction(DemoRibbonWindow.createIcon(DemoRibbonWindow.Image_Open), self.tr("Open")) smallButton.setToolTip(self.tr("Open an existing document")) self.connect(smallButton, QtCore.SIGNAL("triggered()"), self.pressButton) quickAccessBar.setActionVisible(smallButton, False) smallButton = quickAccessBar.addAction(DemoRibbonWindow.createIcon(DemoRibbonWindow.Image_Save), self.tr("Save")) smallButton.setToolTip(self.tr("Save the active document")) self.connect(smallButton, QtCore.SIGNAL("triggered()"), self.pressButton) smallButton = quickAccessBar.addAction(QIcon(":/res/smallUndo.png"), self.tr("&Undo")) smallButton.setShortcut(QKeySequence.Undo) smallButton.setEnabled(False) smallButton = quickAccessBar.addAction(QIcon(":/res/smallRedo.png"), self.tr("&Redo")) smallButton.setShortcut(QKeySequence.Redo) smallButton.setEnabled(False) quickAccessBar.setVisible(True) def createRibbon(self): pageButtons = self.ribbonBar().addPage(self.tr("&Buttons")) if pageButtons != None: splitPopup = QMenu(self) splitPopup.addAction(self.tr("Option 1")) splitPopup.addAction(self.tr("Option 2")) splitPopup.addAction(self.tr("Option 3")) groupClipboard = pageButtons.addGroup(self.tr("Clipboard")) if groupClipboard != None: editPaste = QMenu(self) actionPaste1 = editPaste.addAction(QIcon(":/res/smallClipboard.png"), self.tr("Paste")) actionPaste1.setPriority(QAction.LowPriority) actionPaste1.setShortcut(QKeySequence.Paste) actionPaste2 = groupClipboard.addAction(QIcon(":/res/clipboard.png"), self.tr("&Paste"), Qt.ToolButtonTextUnderIcon, editPaste) actionPaste2.setPriority(QAction.LowPriority) actionPaste2.setShortcut(QKeySequence.Paste) actionCut = groupClipboard.addAction(QIcon(":/res/smallCut.png"), self.tr("&Cut"), Qt.ToolButtonTextBesideIcon) actionCut.setShortcut(QKeySequence.Cut) actionCopy = groupClipboard.addAction(QIcon(":/res/smallCopy.png"), self.tr("&Copy"), Qt.ToolButtonTextBesideIcon) actionCopy.setShortcut(QKeySequence.Copy) groupClipboard.addAction(QIcon(":/res/smallFormatPainter.png"), self.tr("F&ormat Pointer"), Qt.ToolButtonTextBesideIcon) groupClipboard.setEnabled(False) groupView = pageButtons.addGroup(self.tr("View")) if groupView != None: checkStatusBar = QCheckBox(self.tr("Status Bar")) checkStatusBar.setToolTip(self.tr("CheckBox 1")) checkStatusBar.setCheckState(Qt.Checked) groupView.addWidget(checkStatusBar) #connect(checkStatusBar, SIGNAL(stateChanged(int)), this, SLOT(hideStatusBar(int))); pageWindow = self.ribbonBar().addPage(self.tr("&Window")) pageContext = self.ribbonBar().addPage(self.tr("&Context Tab")) if pageContext != None: pageContext.setContextColor(Qtitan.ContextColorGreen) self.ribbonBar().setFrameThemeEnabled() def createDockWindows(self): dock = QDockWidget(self.tr("Styles"), self) dock.setAllowedAreas(Qt.AllDockWidgetAreas) styleWidget = QWidget() styleGroup = QWidget() listLayout = QVBoxLayout() listLayout.setMargin(0) customerList = QListWidget() for i in range(0, 10): customerList.insertItem(i, QListWidgetItem(self.tr("Item ") + str(i+1))) listLayout.addWidget(customerList) styleGroup.setLayout(listLayout) optionsGroup = QGroupBox(self.tr("Preview")) optionsLayout = QVBoxLayout() showPreviewBox = QCheckBox(self.tr("Show Preview")) showPreviewBox.setEnabled(False) linkedStylesBox = QCheckBox(self.tr("Disable Linked Styles")) optionsLayout.addWidget(showPreviewBox) optionsLayout.addWidget(linkedStylesBox) optionsGroup.setLayout(optionsLayout) mainLayout = QVBoxLayout() mainLayout.addWidget(styleGroup) mainLayout.addWidget(optionsGroup) styleWidget.setLayout(mainLayout) dock.setWidget(styleWidget) self.addDockWidget(Qt.LeftDockWidgetArea, dock) # QList<QDockWidget*> docks; docks << dock; # QList<int> sizes; sizes << 120; # resizeDocks(docks, sizes, Qt::Vertical); def open(self): fileName = QFileDialog.getOpenFileName(self, self.tr("Open")) if not fileName: self.setCurrentFile(fileName) self.statusBar().showMessage(self.tr("File loaded"), 2000) def save(self): fileName = QFileDialog.getSaveFileName(self, self.tr("Save As"), self.tr("Document"), "*.txt")[0] if fileName == "": return False self.setCurrentFile(fileName) self.statusBar().showMessage(self.tr("File saved"), 2000) return True def openRecentFile(self, file): QMessageBox.information(self, self.windowTitle(), "Open Recent File. " + file, QMessageBox.Ok) def print(self): printer = QPrinter() dialog = QPrintDialog(printer, self) return dialog.exec() == QDialog.Accepted def printSetup(self): self = self def hideStatusBar(self, state): self.statusBar().setVisible(state = Qt.Checked) def pressButton(self): QMessageBox.information(self, self.windowTitle(), "Button pressed.", QMessageBox.Ok) def showBackstage(self): quickAccessBar = self.ribbonBar().quickAccessBar() if quickAccessBar != None: quickAccessBar.setEnabled(False) self.actionRibbonMinimize.setEnabled(False) self.menuOptions.setEnabled(False) def hideBackstage(self): quickAccessBar = self.ribbonBar().quickAccessBar() if quickAccessBar != None: quickAccessBar.setEnabled(True) self.actionRibbonMinimize.setEnabled(True) self.menuOptions.setEnabled(True) def setDPIToggled(self, on_): #self.ribbonStyle.setDPIAware(on_) on_ = on_ def setCurrentFile(self, fileName): self.setWindowFilePath(fileName) settings = QSettings(QSettings.UserScope, "Developer Machines") files = settings.value("recentFileList").toStringList() files.removeAll(fileName) files.prepend(fileName) while files.size() > 9: files.removeLast() settings.setValue("recentFileList", files) if __name__ == "__main__": app = QApplication(sys.argv) app.setApplicationName("PyQtitanRibbon Backstage Sample"); app.setOrganizationName("Developer Machines"); w = MainWindow() w.show() sys.exit(app.exec_())
<RCC> <qresource prefix="/"> <file>res/clipboard.png</file> <file>res/smallClipboard.png</file> <file>res/smallCopy.png</file> <file>res/smallCut.png</file> <file>res/smallFormatPainter.png</file> <file>res/ChangeFileType.png</file> <file>res/CreateDocument.png</file> <file>res/smallRedo.png</file> <file>res/smallUndo.png</file> <file>res/smallOptions.png</file> <file>res/smallClose.png</file> <file>res/ScreenPreview.png</file> <file>res/ProtectDocument.png</file> <file>res/CheckIssues.png</file> <file>res/MngVersions.png</file> <file>res/SendUsing.png</file> <file>res/SaveWeb.png</file> <file>res/SaveSharePoint.png</file> <file>res/Publish.png</file> <file>res/devmachines.png</file> <file>res/help.png</file> <file>res/background.png</file> <file>../../shared/res/about.png</file> <file>../../shared/res/ribbonMaximize.png</file> <file>../../shared/res/ribbonMinimize.png</file> </qresource> </RCC>
#include <QApplication> #include <QMenu> #include <QtnRibbonDef.h> #include "backstagepages.h" /* BackstagePageInfo */ BackstagePageInfo::BackstagePageInfo(QWidget* parent) : RibbonBackstagePage(parent) { ui.setupUi(this); createActions(); createMenus(); int maxWidth = ui.protectDocumentBut->sizeHint().width(); maxWidth = qMax(maxWidth, ui.checkIssuesBut->sizeHint().width()); maxWidth = qMax(maxWidth, ui.mngVersionsBut->sizeHint().width()); ui.protectDocumentBut->setMinimumWidth(maxWidth); ui.protectDocumentBut->setMaximumWidth(maxWidth); ui.checkIssuesBut->setMinimumWidth(maxWidth); ui.checkIssuesBut->setMaximumWidth(maxWidth); ui.mngVersionsBut->setMinimumWidth(maxWidth); ui.mngVersionsBut->setMaximumWidth(maxWidth); } BackstagePageInfo::~BackstagePageInfo() { } void BackstagePageInfo::createActions() { m_actMark = new QAction(tr("Mark as &Final"), this); m_actMark->setStatusTip(tr("Let readers know the document is final and make it read-only.")); m_actPassword = new QAction(QIcon(QStringLiteral(":/res/ProtectDocument.png")), tr("Encrypt with Password"), this); m_actMark->setStatusTip(tr("Requare a password to open this document")); m_actInspect = new QAction(tr("&Inspect Document"), this); m_actMark->setStatusTip(tr("Check the document for hidden properties or personal information.")); m_actAccessibility = new QAction(tr("Check Accessibility"), this); m_actMark->setStatusTip(tr("Check the document for content that people with disabilities might find difficult to read.")); } void BackstagePageInfo::createMenus() { QMenu* menu = new QMenu(this); menu->addAction(m_actMark); menu->addAction(m_actPassword); ui.protectDocumentBut->setMenu(menu); menu = new QMenu(this); menu->addAction(m_actInspect); menu->addAction(m_actAccessibility); ui.checkIssuesBut->setMenu(menu); } /* BackstagePageSaveEmail */ BackstagePageSaveEmail::BackstagePageSaveEmail(QWidget* parent) : RibbonBackstagePage(parent) { ui.setupUi(this); } BackstagePageSaveEmail::~BackstagePageSaveEmail() { } /* BackstagePageSaveEmail */ BackstagePageSaveWeb::BackstagePageSaveWeb(QWidget* parent) : RibbonBackstagePage(parent) { ui.setupUi(this); } BackstagePageSaveWeb::~BackstagePageSaveWeb() { } /* BackstagePageSave */ BackstagePageSave::BackstagePageSave(QWidget* parent) : RibbonBackstagePage(parent) { m_separator = new RibbonBackstageSeparator(this); m_separator->setOrientation(Qt::Vertical); ui.setupUi(this); ui.pageWrapper->setText(QString()); m_pageSaveEmail = new BackstagePageSaveEmail(ui.pageWrapper); m_pageSaveWeb = new BackstagePageSaveWeb(ui.pageWrapper); m_pageSaveWeb->hide(); QActionGroup* actGroup = new QActionGroup(this); m_actSendEmailButton = actGroup->addAction(ui.sendEmailButton->icon(), ui.sendEmailButton->text()); m_actSendEmailButton->setCheckable(true); m_actSaveWebButton = actGroup->addAction(ui.saveWebButton->icon(), ui.saveWebButton->text()); m_actSaveWebButton->setCheckable(true); m_actSharePointButton = actGroup->addAction(ui.sharePointButton->icon(), ui.sharePointButton->text()); m_actSharePointButton->setCheckable(true); m_actBlogPostButton = actGroup->addAction(ui.blogPostButton->icon(), ui.blogPostButton->text()); m_actBlogPostButton->setCheckable(true); m_actFileTypeButton = actGroup->addAction(ui.fileTypeButton->icon(), ui.fileTypeButton->text()); m_actFileTypeButton->setCheckable(true); m_actCreateDocButton = actGroup->addAction(ui.createDocButton->icon(), ui.createDocButton->text()); m_actCreateDocButton->setCheckable(true); ui.sendEmailButton->setDefaultAction(m_actSendEmailButton); ui.saveWebButton->setDefaultAction(m_actSaveWebButton); ui.sharePointButton->setDefaultAction(m_actSharePointButton); ui.blogPostButton->setDefaultAction(m_actBlogPostButton); ui.fileTypeButton->setDefaultAction(m_actFileTypeButton); ui.createDocButton->setDefaultAction(m_actCreateDocButton); m_actSendEmailButton->setChecked(true); connect(actGroup, SIGNAL(triggered(QAction*)), this, SLOT(triggeredPage(QAction*))); } BackstagePageSave::~BackstagePageSave() { } void BackstagePageSave::triggeredPage(QAction* act) { if (m_actSendEmailButton == act) { m_pageSaveWeb->hide(); m_pageSaveEmail->show(); } else if (m_actSaveWebButton == act) { m_pageSaveWeb->show(); m_pageSaveEmail->hide(); } else { m_pageSaveWeb->hide(); m_pageSaveEmail->hide(); } } void BackstagePageSave::resizeEvent(QResizeEvent* event) { QWidget::resizeEvent(event); m_pageSaveEmail->setGeometry(QRect(QPoint(0, 0), ui.pageWrapper->size())); m_pageSaveWeb->setGeometry(QRect(QPoint(0, 0), ui.pageWrapper->size())); QRect rc = ui.verticalLayoutTabs->geometry(); m_separator->setGeometry(QRect(QPoint(rc.right() - 1, 0), QSize(3, size().height()))); } /* BackstagePageSave */ BackstagePageAbout::BackstagePageAbout(QWidget* parent) : RibbonBackstagePage(parent) { ui.setupUi(this); QString str = ui.labelVersion->text(); if (!str.isEmpty()) { QString strVersion(tr("Version:")); int index = str.indexOf(strVersion); if (index != -1) ui.labelVersion->setText(str.insert(index + strVersion.length(), QLatin1Char(' ') + QStringLiteral(QTN_VERSION_RIBBON_STR))); } } BackstagePageAbout::~BackstagePageAbout() { }
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn
文章转载自: