gpt4 book ai didi

c++ - 如何在小部件上单击 "What' s“此”按钮时得到通知?

转载 作者:行者123 更新时间:2023-11-30 05:13:09 25 4
gpt4 key购买 nike

当我按下 [?]“这是什么?”时标题栏中的按钮,我应该检查哪个信号?

   Qt::WindowFlags flags;

flags |= Qt::CustomizeWindowHint; // Remove all previous windows hints

// Restore previous hints
flags |= Qt::Window;
flags |= Qt::WindowTitleHint;
flags |= Qt::WindowSystemMenuHint;
//flags |= Qt::WindowMinimizeButtonHint; // Hides [?]
//flags |= Qt::WindowMaximizeButtonHint; // Hides [?]
flags |= Qt::WindowCloseButtonHint;

// Add new hints
flags |= Qt::WindowContextHelpButtonHint;

setWindowFlags(flags);

connect(this, ???, this, MainWindow::TestSlot());

最佳答案

I need to [...] call a function (e.g. write a log message) when the [?] button is pressed.

顶层窗口分别在进入和离开 What's This 模式时接收 QEvent::EnterWhatsThisModeQEvent::LeaveWhatsThisMode 事件。

当小部件接收到事件时,您可以将事件转换为信号,例如使用 event signaler安装在该小部件上。

或者,如果您想自动捕获所有 窗口上的事件,而不需要在每个顶级小部件上安装信号器,您可以在QApplication 上安装信号器对象本身。

下面是一个完整的例子:

// https://github.com/KubaO/stackoverflown/tree/master/questions/whatsthis-bypass-44073556
#include <QtWidgets>

// See https://stackoverflow.com/a/32027028/1329652
class WhatsThisSignaler : public QObject {
Q_OBJECT
bool eventFilter(QObject * obj, QEvent * ev) override {
if (!obj->isWidgetType())
return false;
auto widget = static_cast<QWidget*>(obj);
if (!widget->isWindow())
return false;
switch (ev->type()) {
case QEvent::EnterWhatsThisMode:
case QEvent::LeaveWhatsThisMode:
emit whatsThisEvent(widget, ev);
break;
default:
break;
}
return false;
}
public:
Q_SIGNAL void whatsThisEvent(QWidget *, QEvent *);
WhatsThisSignaler(QObject * parent = {}) : QObject(parent) {}
void installOn(QWidget * widget) {
widget->installEventFilter(this);
}
void installOn(QCoreApplication * app) {
app->installEventFilter(this);
}
};

// See https://stackoverflow.com/q/22535469/1329652
template<typename EnumType> QString toName(EnumType enumValue)
{
auto * enumName = qt_getEnumName(enumValue);
auto * metaObject = qt_getEnumMetaObject(enumValue);
QString name;
if (metaObject) {
auto enumIndex = metaObject->indexOfEnumerator(enumName);
name = metaObject->enumerator(enumIndex).valueToKey(enumValue);
}
if (name.isEmpty())
name = QString::number((int)enumValue);
return name;
}

int main(int argc, char ** argv) {
QApplication app{argc, argv};
WhatsThisSignaler sig;
QPlainTextEdit w;
w.setWindowFlags(Qt::WindowContextHelpButtonHint | Qt::WindowCloseButtonHint);
w.setMinimumSize(200, 200);
w.setReadOnly(true);
w.show();
sig.installOn(&w);
QObject::connect(&sig, &WhatsThisSignaler::whatsThisEvent, &w, [&w](QWidget*widget, QEvent*ev){
w.appendPlainText(QStringLiteral("%1(0x%2) \"%3\" QEvent::%4")
.arg(widget->metaObject()->className())
.arg((uintptr_t)widget, 0, 16)
.arg(widget->objectName())
.arg(toName(ev->type())));
});
return app.exec();
}

#include "main.moc"

关于c++ - 如何在小部件上单击 "What' s“此”按钮时得到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44073556/

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