社区
Qt
帖子详情
setShortcut(QKeySequence::New) 什么意思?
liuxiaodongxiao
2010-05-14 02:09:39
详细说一下,我才刚学几天。
...全文
589
2
打赏
收藏
setShortcut(QKeySequence::New) 什么意思?
详细说一下,我才刚学几天。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
liuxiaodongxiao
2010-05-15
打赏
举报
回复
[Quote=引用 1 楼 dext 的回复:]
setShortcut 就是设置 QAction 或者 QAbstractButton 快捷键
你可以设置成 你想要的。
而 QKeySequence::New 是一个常量
他在不同的平台下 可能有不同的值 比如 Windows 下 是 Ctrl + N
这个是由 不同平台的 习惯而导致的。
不过 还好,几乎所有的平台 都是 Ctrl + N 用于 新建。
但是 关闭 就 不同了 ……
[/Quote]
谢了!
dext
2010-05-15
打赏
举报
回复
setShortcut 就是设置 QAction 或者 QAbstractButton 快捷键
你可以设置成 你想要的。
而 QKeySequence::New 是一个常量
他在不同的平台下 可能有不同的值 比如 Windows 下 是 Ctrl + N
这个是由 不同平台的 习惯而导致的。
不过 还好,几乎所有的平台 都是 Ctrl + N 用于 新建。
但是 关闭 就 不同了 Windows 是 Alt + F4 Mac 是 Ctrl + W
如果没有QKeySequence::Close 常量,
可能你要针对不同平台写不同的快捷键 来照顾那个平台下用户的习惯。
Qt笔记本源码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifndef QT_NO_PRINTER #include
#include
#include
#endif #include "textedit.h" #ifdef Q_OS_MAC const QString rsrcPath = ":/images/mac"; #else const QString rsrcPath = ":/images/win"; #endif TextEdit::TextEdit(QWidget *parent) : QMainWindow(parent) { #ifdef Q_OS_OSX
set
UnifiedTitleAndToolBarOnMac(true); #endif
set
ToolButtonStyle(Qt::ToolButtonFollowStyle);
set
upFileActions();
set
upEditActions();
set
upTextActions(); { QMenu *helpMenu =
new
QMenu(tr("Help"), this); menuBar()->addMenu(helpMenu); helpMenu->addAction(tr("About"), this, SLOT(about())); helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt())); } textEdit =
new
QTextEdit(this); connect(textEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(currentCharFormatChanged(QTextCharFormat))); connect(textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChanged()));
set
CentralWidget(textEdit); textEdit->
set
Focus();
set
CurrentFileName(QString()); QFont textFont("Helvetica"); textFont.
set
StyleHint(QFont::SansSerif); textEdit->
set
Font(textFont); fontChanged(textEdit->font()); colorChanged(textEdit->textColor()); alignmentChanged(textEdit->alignment()); connect(textEdit->document(), SIGNAL(modificationChanged(bool)), actionSave, SLOT(
set
Enabled(bool))); connect(textEdit->document(), SIGNAL(modificationChanged(bool)), this, SLOT(
set
WindowModified(bool))); connect(textEdit->document(), SIGNAL(undoAvailable(bool)), actionUndo, SLOT(
set
Enabled(bool))); connect(textEdit->document(), SIGNAL(redoAvailable(bool)), actionRedo, SLOT(
set
Enabled(bool)));
set
WindowModified(textEdit->document()->isModified()); actionSave->
set
Enabled(textEdit->document()->isModified()); actionUndo->
set
Enabled(textEdit->document()->isUndoAvailable()); actionRedo->
set
Enabled(textEdit->document()->isRedoAvailable()); connect(actionUndo, SIGNAL(triggered()), textEdit, SLOT(undo())); connect(actionRedo, SIGNAL(triggered()), textEdit, SLOT(redo())); actionCut->
set
Enabled(false); actionCopy->
set
Enabled(false); connect(actionCut, SIGNAL(triggered()), textEdit, SLOT(cut())); connect(actionCopy, SIGNAL(triggered()), textEdit, SLOT(copy())); connect(actionPaste, SIGNAL(triggered()), textEdit, SLOT(paste())); connect(textEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(
set
Enabled(bool))); connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(
set
Enabled(bool))); #ifndef QT_NO_CLIPBOARD connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged())); #endif QString initialFile = ":/example.html"; const QStringList args = QCoreApplication::arguments(); if (args.count() == 2) initialFile = args.at(1); if (!load(initialFile)) file
New
(); } void TextEdit::closeEvent(QCloseEvent *e) { if (maybeSave()) e->accept(); else e->ignore(); } void TextEdit::
set
upFileActions() { QToolBar *tb =
new
QToolBar(this); tb->
set
WindowTitle(tr("File Actions")); addToolBar(tb); QMenu *menu =
new
QMenu(tr("&File"), this); menuBar()->addMenu(menu); QAction *a; QIcon
new
Icon = QIcon::fromTheme("document-
new
", QIcon(rsrcPath + "/file
new
.png")); a =
new
QAction(
new
Icon, tr("&
New
"), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Q
Key
Sequence
::
New
); connect(a, SIGNAL(triggered()), this, SLOT(file
New
())); tb->addAction(a); menu->addAction(a); a =
new
QAction(QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png")), tr("&Open..."), this); a->
set
Shortcut
(Q
Key
Sequence
::Open); connect(a, SIGNAL(triggered()), this, SLOT(fileOpen())); tb->addAction(a); menu->addAction(a); menu->addSeparator(); actionSave = a =
new
QAction(QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png")), tr("&Save"), this); a->
set
Shortcut
(Q
Key
Sequence
::Save); connect(a, SIGNAL(triggered()), this, SLOT(fileSave())); a->
set
Enabled(false); tb->addAction(a); menu->addAction(a); a =
new
QAction(tr("Save &As..."), this); a->
set
Priority(QAction::LowPriority); connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs())); menu->addAction(a); menu->addSeparator(); #ifndef QT_NO_PRINTER a =
new
QAction(QIcon::fromTheme("document-print", QIcon(rsrcPath + "/fileprint.png")), tr("&Print..."), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Q
Key
Sequence
::Print); connect(a, SIGNAL(triggered()), this, SLOT(filePrint())); tb->addAction(a); menu->addAction(a); a =
new
QAction(QIcon::fromTheme("fileprint", QIcon(rsrcPath + "/fileprint.png")), tr("Print Preview..."), this); connect(a, SIGNAL(triggered()), this, SLOT(filePrintPreview())); menu->addAction(a); a =
new
QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")), tr("&Export PDF..."), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Qt::CTRL + Qt::
Key
_D); connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf())); tb->addAction(a); menu->addAction(a); menu->addSeparator(); #endif a =
new
QAction(tr("&Quit"), this); a->
set
Shortcut
(Qt::CTRL + Qt::
Key
_Q); connect(a, SIGNAL(triggered()), this, SLOT(close())); menu->addAction(a); } void TextEdit::
set
upEditActions() { QToolBar *tb =
new
QToolBar(this); tb->
set
WindowTitle(tr("Edit Actions")); addToolBar(tb); QMenu *menu =
new
QMenu(tr("&Edit"), this); menuBar()->addMenu(menu); QAction *a; a = actionUndo =
new
QAction(QIcon::fromTheme("edit-undo", QIcon(rsrcPath + "/editundo.png")), tr("&Undo"), this); a->
set
Shortcut
(Q
Key
Sequence
::Undo); tb->addAction(a); menu->addAction(a); a = actionRedo =
new
QAction(QIcon::fromTheme("edit-redo", QIcon(rsrcPath + "/editredo.png")), tr("&Redo"), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Q
Key
Sequence
::Redo); tb->addAction(a); menu->addAction(a); menu->addSeparator(); a = actionCut =
new
QAction(QIcon::fromTheme("edit-cut", QIcon(rsrcPath + "/editcut.png")), tr("Cu&t"), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Q
Key
Sequence
::Cut); tb->addAction(a); menu->addAction(a); a = actionCopy =
new
QAction(QIcon::fromTheme("edit-copy", QIcon(rsrcPath + "/editcopy.png")), tr("&Copy"), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Q
Key
Sequence
::Copy); tb->addAction(a); menu->addAction(a); a = actionPaste =
new
QAction(QIcon::fromTheme("edit-paste", QIcon(rsrcPath + "/editpaste.png")), tr("&Paste"), this); a->
set
Priority(QAction::LowPriority); a->
set
Shortcut
(Q
Key
Sequence
::Paste); tb->addAction(a); menu->addAction(a); #ifndef QT_NO_CLIPBOARD if (const QMimeData *md = QApplication::clipboard()->mimeData()) actionPaste->
set
Enabled(md->hasText()); #endif } void TextEdit::
set
upTextActions() { QToolBar *tb =
new
QToolBar(this); tb->
set
WindowTitle(tr("Format Actions")); addToolBar(tb); QMenu *menu =
new
QMenu(tr("F&ormat"), this); menuBar()->addMenu(menu); actionTextBold =
new
QAction(QIcon::fromTheme("format-text-bold", QIcon(rsrcPath + "/textbold.png")), tr("&Bold"), this); actionTextBold->
set
Shortcut
(Qt::CTRL + Qt::
Key
_B); actionTextBold->
set
Priority(QAction::LowPriority); QFont bold; bold.
set
Bold(true); actionTextBold->
set
Font(bold); connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold())); tb->addAction(actionTextBold); menu->addAction(actionTextBold); actionTextBold->
set
Checkable(true); actionTextItalic =
new
QAction(QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png")), tr("&Italic"), this); actionTextItalic->
set
Priority(QAction::LowPriority); actionTextItalic->
set
Shortcut
(Qt::CTRL + Qt::
Key
_I); QFont italic; italic.
set
Italic(true); actionTextItalic->
set
Font(italic); connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic())); tb->addAction(actionTextItalic); menu->addAction(actionTextItalic); actionTextItalic->
set
Checkable(true); actionTextUnderline =
new
QAction(QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png")), tr("&Underline"), this); actionTextUnderline->
set
Shortcut
(Qt::CTRL + Qt::
Key
_U); actionTextUnderline->
set
Priority(QAction::LowPriority); QFont underline; underline.
set
Underline(true); actionTextUnderline->
set
Font(underline); connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline())); tb->addAction(actionTextUnderline); menu->addAction(actionTextUnderline); actionTextUnderline->
set
Checkable(true); menu->addSeparator(); QActionGroup *grp =
new
QActionGroup(this); connect(grp, SIGNAL(triggered(QAction*)), this, SLOT(textAlign(QAction*))); // Make sure the alignLeft is always left of the alignRight if (QApplication::isLeftToRight()) { actionAlignLeft =
new
QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("&Left"), grp); actionAlignCenter =
new
QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp); actionAlignRight =
new
QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp); } else { actionAlignRight =
new
QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp); actionAlignCenter =
new
QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp); actionAlignLeft =
new
QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("&Left"), grp); } actionAlignJustify =
new
QAction(QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png")), tr("&Justify"), grp); actionAlignLeft->
set
Shortcut
(Qt::CTRL + Qt::
Key
_L); actionAlignLeft->
set
Checkable(true); actionAlignLeft->
set
Priority(QAction::LowPriority); actionAlignCenter->
set
Shortcut
(Qt::CTRL + Qt::
Key
_E); actionAlignCenter->
set
Checkable(true); actionAlignCenter->
set
Priority(QAction::LowPriority); actionAlignRight->
set
Shortcut
(Qt::CTRL + Qt::
Key
_R); actionAlignRight->
set
Checkable(true); actionAlignRight->
set
Priority(QAction::LowPriority); actionAlignJustify->
set
Shortcut
(Qt::CTRL + Qt::
Key
_J); actionAlignJustify->
set
Checkable(true); actionAlignJustify->
set
Priority(QAction::LowPriority); tb->addActions(grp->actions()); menu->addActions(grp->actions()); menu->addSeparator(); QPixmap pix(16, 16); pix.fill(Qt::black); actionTextColor =
new
QAction(pix, tr("&Color..."), this); connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor())); tb->addAction(actionTextColor); menu->addAction(actionTextColor); tb =
new
QToolBar(this); tb->
set
AllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); tb->
set
WindowTitle(tr("Format Actions")); addToolBarBreak(Qt::TopToolBarArea); addToolBar(tb); comboStyle =
new
QComboBox(tb); tb->addWidget(comboStyle); comboStyle->addItem("Standard"); comboStyle->addItem("Bullet List (Disc)"); comboStyle->addItem("Bullet List (Circle)"); comboStyle->addItem("Bullet List (Square)"); comboStyle->addItem("Ordered List (Decimal)"); comboStyle->addItem("Ordered List (Alpha lower)"); comboStyle->addItem("Ordered List (Alpha upper)"); comboStyle->addItem("Ordered List (Roman lower)"); comboStyle->addItem("Ordered List (Roman upper)"); connect(comboStyle, SIGNAL(activated(int)), this, SLOT(textStyle(int))); comboFont =
new
QFontComboBox(tb); tb->addWidget(comboFont); connect(comboFont, SIGNAL(activated(QString)), this, SLOT(textFamily(QString))); comboSize =
new
QComboBox(tb); comboSize->
set
ObjectName("comboSize"); tb->addWidget(comboSize); comboSize->
set
Editable(true); QFontDatabase db; foreach(int size, db.standardSizes()) comboSize->addItem(QString::number(size)); connect(comboSize, SIGNAL(activated(QString)), this, SLOT(textSize(QString))); comboSize->
set
CurrentIndex(comboSize->findText(QString::number(QApplication::font() .pointSize()))); } bool TextEdit::load(const QString &f) { if (!QFile::exists(f)) return false; QFile file(f); if (!file.open(QFile::ReadOnly)) return false; QByteArray data = file.readAll(); QTextCodec *codec = Qt::codecForHtml(data); QString str = codec->toUnicode(data); if (Qt::mightBeRichText(str)) { textEdit->
set
Html(str); } else { str = QString::fromLocal8Bit(data); textEdit->
set
PlainText(str); }
set
CurrentFileName(f); return true; } bool TextEdit::maybeSave() { if (!textEdit->document()->isModified()) return true; QMessageBox::StandardButton ret; ret = QMessageBox::warning(this, tr("Application"), tr("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); if (ret == QMessageBox::Save) return fileSave(); else if (ret == QMessageBox::Cancel) return false; return true; } void TextEdit::
set
CurrentFileName(const QString &fileName) { this->fileName = fileName; textEdit->document()->
set
Modified(false); QString shownName; if (fileName.isEmpty()) shownName = "untitled.txt"; else shownName = QFileInfo(fileName).fileName();
set
WindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
set
WindowModified(false); } void TextEdit::file
New
() { if (maybeSave()) { textEdit->clear();
set
CurrentFileName(QString()); } } void TextEdit::fileOpen() { QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."), QString(), tr("HTML-Files (*.htm *.html);;All Files (*)")); if (!fn.isEmpty()) load(fn); } bool TextEdit::fileSave() { if (fileName.isEmpty()) return fileSaveAs(); if (fileName.startsWith(QStringLiteral(":/"))) return fileSaveAs(); QTextDocumentWriter writer(fileName); bool success = writer.write(textEdit->document()); if (success) textEdit->document()->
set
Modified(false); return success; } bool TextEdit::fileSaveAs() { QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("ODF files (*.odt);;HTML-Files " "(*.htm *.html);;All Files (*)")); if (fn.isEmpty()) return false; if (!(fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive))) { fn += ".odt"; // default }
set
CurrentFileName(fn); return fileSave(); } void TextEdit::filePrint() { #if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG) QPrinter printer(QPrinter::HighResolution); QPrintDialog *dlg =
new
QPrintDialog(&printer, this); if (textEdit->textCursor().hasSelection()) dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection); dlg->
set
WindowTitle(tr("Print Document")); if (dlg->exec() == QDialog::Accepted) textEdit->print(&printer); delete dlg; #endif } void TextEdit::filePrintPreview() { #if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG) QPrinter printer(QPrinter::HighResolution); QPrintPreviewDialog preview(&printer, this); connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreview(QPrinter*))); preview.exec(); #endif } void TextEdit::printPreview(QPrinter *printer) { #ifdef QT_NO_PRINTER Q_UNUSED(printer); #else textEdit->print(printer); #endif } void TextEdit::filePrintPdf() { #ifndef QT_NO_PRINTER QString fileName = QFileDialog::getSaveFileName(this, "Export PDF", QString(), "*.pdf"); if (!fileName.isEmpty()) { if (QFileInfo(fileName).suffix().isEmpty()) fileName.append(".pdf"); QPrinter printer(QPrinter::HighResolution); printer.
set
OutputFormat(QPrinter::PdfFormat); printer.
set
OutputFileName(fileName); textEdit->document()->print(&printer); } #endif } void TextEdit::textBold() { QTextCharFormat fmt; fmt.
set
FontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal); mergeFormatOnWordOrSelection(fmt); } void TextEdit::textUnderline() { QTextCharFormat fmt; fmt.
set
FontUnderline(actionTextUnderline->isChecked()); mergeFormatOnWordOrSelection(fmt); } void TextEdit::textItalic() { QTextCharFormat fmt; fmt.
set
FontItalic(actionTextItalic->isChecked()); mergeFormatOnWordOrSelection(fmt); } void TextEdit::textFamily(const QString &f) { QTextCharFormat fmt; fmt.
set
FontFamily(f); mergeFormatOnWordOrSelection(fmt); } void TextEdit::textSize(const QString &p) { qreal pointSize = p.toFloat(); if (p.toFloat() > 0) { QTextCharFormat fmt; fmt.
set
FontPointSize(pointSize); mergeFormatOnWordOrSelection(fmt); } } void TextEdit::textStyle(int styleIndex) { QTextCursor cursor = textEdit->textCursor(); if (styleIndex != 0) { QTextListFormat::Style style = QTextListFormat::ListDisc; switch (styleIndex) { default: case 1: style = QTextListFormat::ListDisc; break; case 2: style = QTextListFormat::ListCircle; break; case 3: style = QTextListFormat::ListSquare; break; case 4: style = QTextListFormat::ListDecimal; break; case 5: style = QTextListFormat::ListLowerAlpha; break; case 6: style = QTextListFormat::ListUpperAlpha; break; case 7: style = QTextListFormat::ListLowerRoman; break; case 8: style = QTextListFormat::ListUpperRoman; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.
set
Indent(blockFmt.indent() + 1); blockFmt.
set
Indent(0); cursor.
set
BlockFormat(blockFmt); } listFmt.
set
Style(style); cursor.createList(listFmt); cursor.endEditBlock(); } else { // #### QTextBlockFormat bfmt; bfmt.
set
ObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } } void TextEdit::textColor() { QColor col = QColorDialog::getColor(textEdit->textColor(), this); if (!col.isValid()) return; QTextCharFormat fmt; fmt.
set
Foreground(col); mergeFormatOnWordOrSelection(fmt); colorChanged(col); } void TextEdit::textAlign(QAction *a) { if (a == actionAlignLeft) textEdit->
set
Alignment(Qt::AlignLeft | Qt::AlignAbsolute); else if (a == actionAlignCenter) textEdit->
set
Alignment(Qt::AlignHCenter); else if (a == actionAlignRight) textEdit->
set
Alignment(Qt::AlignRight | Qt::AlignAbsolute); else if (a == actionAlignJustify) textEdit->
set
Alignment(Qt::AlignJustify); } void TextEdit::currentCharFormatChanged(const QTextCharFormat &format) { fontChanged(format.font()); colorChanged(format.foreground().color()); } void TextEdit::cursorPositionChanged() { alignmentChanged(textEdit->alignment()); } void TextEdit::clipboardDataChanged() { #ifndef QT_NO_CLIPBOARD if (const QMimeData *md = QApplication::clipboard()->mimeData()) actionPaste->
set
Enabled(md->hasText()); #endif } void TextEdit::about() { QMessageBox::about(this, tr("About"), tr("This example demonstrates Qt's " "rich text editing facilities in action, providing an example " "document for you to experiment with.")); } void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format) { QTextCursor cursor = textEdit->textCursor(); if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor); cursor.mergeCharFormat(format); textEdit->mergeCurrentCharFormat(format); } void TextEdit::fontChanged(const QFont &f) { comboFont->
set
CurrentIndex(comboFont->findText(QFontInfo(f).family())); comboSize->
set
CurrentIndex(comboSize->findText(QString::number(f.pointSize()))); actionTextBold->
set
Checked(f.bold()); actionTextItalic->
set
Checked(f.italic()); actionTextUnderline->
set
Checked(f.underline()); } void TextEdit::colorChanged(const QColor &c) { QPixmap pix(16, 16); pix.fill(c); actionTextColor->
set
Icon(pix); } void TextEdit::alignmentChanged(Qt::Alignment a) { if (a & Qt::AlignLeft) actionAlignLeft->
set
Checked(true); else if (a & Qt::AlignHCenter) actionAlignCenter->
set
Checked(true); else if (a & Qt::AlignRight) actionAlignRight->
set
Checked(true); else if (a & Qt::AlignJustify) actionAlignJustify->
set
Checked(true); }
Qt扫雷代码
用Qt写的扫雷,比较简单,可以实现 #include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { this->
set
WindowIcon(QIcon(tr(":/images/ico.png"))); menuBar=
new
QMenuBar(this); gameMenu=
new
QMenu(tr("游戏(&g)"),this); menuBar->addMenu(gameMenu);
new
Action=
new
QAction(tr("新游戏"),this);
new
Action->
set
Shortcut
(tr("F2")); gameMenu->addAction(
new
Action); connect(
new
Action,SIGNAL(triggered()),this,SLOT(
new
Game())); startAction=
new
QAction(tr("开始"),this); startAction->
set
Shortcut
(tr("P")); gameMenu->addAction(startAction); connect(startAction,SIGNAL(triggered()),this,SLOT(gameStart())); startAction->
set
Enabled(false); pauseAction=
new
QAction(tr("暂停"),this); pauseAction->
set
Shortcut
(tr("P")); gameMenu->addAction(pauseAction); connect(pauseAction,SIGNAL(triggered()),this,SLOT(gamePause())); rankingAction=
new
QAction(tr("最高纪录"),this); rankingAction->
set
Shortcut
(tr("Ctrl+R")); gameMenu->addAction(rankingAction); connect(rankingAction,SIGNAL(triggered()),this,SLOT(showRanking())); quitAction=
new
QAction(tr("退出"),this); quitAction->
set
Shortcut
(Q
Key
Sequence
::Quit); gameMenu->addSeparator(); gameMenu->addAction(quitAction); connect(quitAction,SIGNAL(triggered()),this,SLOT(close())); levelMenu=
new
QMenu(tr("难度(&l)"),this); menuBar->addMenu(levelMenu); aLAction=
new
QAction(tr("9x9,10个雷"),this); aLAction->
set
Data("a"); levelMenu->addAction(aLAction); bLAction=
new
QAction(tr("16x16,40个雷"),this); bLAction->
set
Data("b"); levelMenu->addAction(bLAction); cLAction=
new
QAction(tr("30x16,99个雷"),this); cLAction->
set
Data("c"); levelMenu->addAction(cLAction); dLAction=
new
QAction(tr("自定义"),this); dLAction->
set
Data("d"); levelMenu->addAction(dLAction); connect(aLAction,SIGNAL(triggered()),this,SLOT(
set
Level())); connect(bLAction,SIGNAL(triggered()),this,SLOT(
set
Level())); connect(cLAction,SIGNAL(triggered()),
QT5的SQL数据导入exceldemo
在qt5下的把excel的内容导入到sql当中MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(
new
Ui::MainWindow) { ui->
set
upUi(this); qDebug()<
setIcon(icon); openAction->
set
Shortcut
(Q
Key
Sequence
(tr("ctrl+O"))); ui->menu->addAction(openAction) ; oid MainWindow::open_file_func()//从excel中导入到sql中//存入到对应表时,要写相应的表明 { QString filePath =QFileDialog::getOpenFileName(this,"open","../","excel(*.xlsx *.xls)"); if(filePath.isEmpty()==false) { qDebug()<<"路径"
querySubObject("Sheets"); //Sheets也可换用WorkSheets int sheet_count = work_sheets->property("Count").toInt(); //获取工作表数目 if(sheet_count > 0) { QAxObject *work_sheet = work_book->querySubObject("Sheets(int)", 1); QAxObject *used_range = work_sheet->querySubObject("UsedRange"); QAxObject *rows = used_range->querySubObject("Rows"); QAxObject *columns = used_range->querySubObject("Columns"); int row_count = rows->property("Count").toInt(); //获取行
Qt中的
set
Shortcut
()函数
今天在学习《C++ GUI Qt4编程》(第二版)中看到这样一个函数
set
Shortcut
()函数,这个函数是用来设置快捷键的,也就是说可以使用
set
Shortcut
方法给菜单项指定快捷键。 slectAllAction->
set
Shortcut
s(Q
Key
Sequence
::
New
); 那这里的Q
Key
Sequence
::
New
是
什么意思
那? Q
Key
Sequence
类
Qt学习---设置快捷键/
set
Shortcut
在给菜单设置快捷键时:openAction =
new
QAction(tr("&Open"), this); openAction->
set
Shortcut
(Q
Key
Sequence
::Open);Open是一个标准键,在不同的平台下可能代表不同的快捷键,在Windows平台下代表:Ctrl+OQt中有一个标准键的列表,里面还有很多标准键。简单来说,我们完全可以自己定义一个 tr(...
Qt
16,216
社区成员
26,595
社区内容
发帖
与我相关
我的任务
Qt
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
复制链接
扫一扫
分享
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章