Law*_*aw. 3 c++ qt keyboard-shortcuts
我的键盘快捷键似乎无法工作。这是代码:
class Interface : public QObject
{
Q_OBJECT
private:
QMainWindow myWindow;
QWidget mainWidget;
QShortcut shortcut;
public:
Interface();
~Interface();
void show(void);
public slots:
void haha(void);
};
Interface::Interface() :
QObject(),
shortcut(QKeySequence(Qt::Key_Enter), &mainWidget)
{
myWindow.setFixedSize(1200, 600);
myWindow.setCentralWidget(&mainWidget);
QObject::connect(&shortcut, SIGNAL(activated()), this, SLOT(haha()));
}
void Interface::show(void)
{
myWindow.show();
}
void Interface::haha(void)
{
std::cout << "foo" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我的主要功能是:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
Interface myInterface;
myInterface.Show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
它编译时没有任何警告/错误,但是当我执行它时,按 Enter 键时不会打印“foo”。我已经在互联网上查找过,但没有找到足够接近的东西。如果我错过了一些相关的内容,我很抱歉。
Qt::Key_Enter
指位于键盘上的键。Qt::Key_Return
如果您希望在按下键盘上的主 Enter 键时调用该插槽,则应该使用:
Interface::Interface() :
QObject(),
shortcut(QKeySequence(Qt::Key_Return), &mainWidget)
{
myWindow.setFixedSize(1200, 600);
myWindow.setCentralWidget(&mainWidget);
QObject::connect(&shortcut, SIGNAL(activated()), this, SLOT(haha()));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2283 次 |
最近记录: |