gpt4 book ai didi

c++ - Qt - QPushButton 快捷方式未触发

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:31 27 4
gpt4 key购买 nike

我在通过 QtCreator 将快捷方式绑定(bind)到 QPushButton 时遇到问题。

我所做的是在 QDialog 中放置一个按钮,并使用自动连接将 clicked() 信号连接到插槽。然后,我在表单编辑器中将属性 QAbstractButton::shortcut 设置为 Ctrl+N

当我点击按钮时,插槽被触发,但是当我按下快捷键时,没有任何反应。

这是 ui 文件的代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

这是我的 Dialog 类的代码(标题和源代码已合并):

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0) :
QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
}

private slots:
void on_pushButton_clicked()
{
qDebug() << "click!";
}

private:
Ui::Dialog *ui;
};

int main(int argc, char* argv[]) {
QApplication app(argc, argv);
Dialog dialog;
dialog.show();
return app.exec();
}

我设法让它与菜单栏中的一个 Action 一起工作,我不明白为什么它看起来不一样。

我正在使用 Qt 5.8.0。

最佳答案

您可能正在使用 macOS,ControlModifier 对应于 Command keysMetaModifier 对应于 控制键documented by Qt :

Note: On macOS, the ControlModifier value corresponds to the Command keys on the Macintosh keyboard, and the MetaModifier value corresponds to the Control keys.

还有 QKeySequence 的文档给出相关信息:

Note: On macOS, references to "Ctrl", Qt::CTRL, Qt::Key_Control and Qt::ControlModifier correspond to the Command keys on the Macintosh keyboard, and references to "Meta", Qt::META, Qt::Key_Meta and Qt::MetaModifier correspond to the Control keys.

因此,您应该按 Command + N 来触发快捷方式。

关于c++ - Qt - QPushButton 快捷方式未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44166911/

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