gpt4 book ai didi

c++ - QT 编辑器智能按钮

转载 作者:行者123 更新时间:2023-11-30 03:40:08 24 4
gpt4 key购买 nike

我有一个带有按钮的工具栏:撤消、重做、剪切、复制、粘贴,如下所示:

editmenu = menuBar()->addMenu(QObject::tr("&Edit"));
undoact = editmenu->addAction(QIcon(":images/undo.png"), QObject::tr("&Undo"));
QObject::connect(editwin, SIGNAL(undoAvailable(bool)), undoact, SLOT(setEnabled(bool)));
QObject::connect(undoact, SIGNAL(triggered()), editwin, SLOT(undo()));
undoact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Undo));
undoact->setEnabled(false);
redoact = editmenu->addAction(QIcon(":images/redo.png"), QObject::tr("&Redo"));
QObject::connect(editwin, SIGNAL(redoAvailable(bool)), redoact, SLOT(setEnabled(bool)));
QObject::connect(redoact, SIGNAL(triggered()), editwin, SLOT(redo()));
redoact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Redo));
redoact->setEnabled(false);
editmenu->addSeparator();
cutact = editmenu->addAction(QIcon(":images/cut.png"), QObject::tr("Cu&t"));
cutact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Cut));
cutact->setEnabled(false);
copyact = editmenu->addAction(QIcon(":images/copy.png"), QObject::tr("&Copy"));
copyact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Copy));
copyact->setEnabled(false);
pasteact = editmenu->addAction(QIcon(":images/paste.png"), QObject::tr("&Paste"));
pasteact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Paste));

我要:

  1. 粘贴按钮仅在剪贴板包含文本时启用
  2. 出于某种原因,我需要在有限的时间内禁用所有按钮。在此之后我想启用所有按钮。但是当我启用时,每个按钮都会根据他的条件启用/禁用(例如,仅当有要撤消的内容时才启用撤消按钮,仅当有一些可以粘贴的文本时才启用粘贴按钮...... )

你建议我做什么?什么是最聪明/最简单的方法?

最佳答案

http://doc.qt.io/qt-5/qclipboard.html#signals

http://doc.qt.io/qt-5/qclipboard.html#details

创建从 QClipboard 对象到工具栏/主窗口类中的处理程序插槽的连接。然后在处理程序插槽中执行您希望工具栏按钮发生的逻辑。

例如:

http://doc.qt.io/qt-5/qclipboard.html#dataChanged

// in your constructor after creating your buttons:
QObject::connect(qApp->clipboard(), SIGNAL(dataChanged()),
this, SLOT(onClipboardDataChanged()));

// and then make another connection for the other relevant clipboard signal...
// selectionChanged()

然后在你的位置:

void MainWindow::onClipboardDataChanged()
{
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();

pasteact->setEnabled(mimeData->hasText());
// or you could check the length of the text to see if it is non zero.
}

要一次禁用/启用所有按钮,请改为禁用父对象。

editToolbar->setEnabled(false);
//... after some period of time or transactions...
editToolbar->setEnabled(true);

但这可能不会禁用用于复制、剪切和粘贴的内置加速器。要同时取消这些键盘快捷键,您可能需要在您的应用中放置一个事件过滤器。

http://doc.qt.io/qt-5/accelerators.html

http://doc.qt.io/qt-5/qobject.html#eventFilter

http://doc.qt.io/qt-5/qobject.html#installEventFilter

希望对您有所帮助。

附言。我还没有测试过这段代码。阅读文档。祝你好运。

关于c++ - QT 编辑器智能按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38228211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com