- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我很抱歉这个看起来很夸张的问题,但事实并非如此。我正在阅读 Foundation of qt development 一书,在阅读第四章时,作者通过展示这个例子讲述了 MDI 窗口的基础知识:
MdiWindow::MdiWindow( QWidget *parent ) : QMainWindow( parent ) {
setWindowTitle( tr( "MDI" ) );
QWorkspace* workspace = new QWorkspace;
setCentralWidget( workspace );
connect( workspace, SIGNAL(windowActivated(QWidget *)), this, SLOT(enableActions()));
QSignalMapper* mapper = new QSignalMapper( this );
//my problem is in this line
connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
createActions();
createMenus();
createToolbars();
statusBar()->showMessage( tr("Done") );
enableActions();
}
他的这一段解释完全让我看不懂(是我还是其他人也有理解问题?):
Next, a signal mapping object called QSignalMapper is created and connected. A signal mapper is used to tie the source of the signal to an argument of another signal. In this example, the action of the menu item corresponding to each window in the Window menu is tied to the actual document window. The actions are in turn connected to mapper. When the triggered signal is emitted by the action, the sending action has been associated with the QWidget* of the corresponding document window. This pointer is used as the argument in the mapped(QWidget*) signal emitted by the signal mapping object.
我的问题:在上面的示例中,我仍然不明白什么是信号映射器类、它是如何使用的以及它的功能是什么?。谁能用简单的术语解释上面的段落?如果你能用简单的例子教我 mapper 类的基础知识,那就太棒了?可能是外行人的说法?
P.S :一个困惑是当我们有 MDI 窗口时,进行菜单更改(尽管操作被禁用/启用)例如假设对于一个特定的文档我们有菜单“文件/关闭”和其他文档我们有“文件/重映射器”?
最佳答案
QSignalMapper
用于重新发射带有可选参数的信号。换句话说(来自 documentation ):
This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.
一个很好的例子(也来自文档 - 看看它)设置如下:
Suppose we want to create a custom widget that contains a group of buttons (like a tool palette). One approach is to connect each button's clicked() signal to its own custom slot; but in this example we want to connect all the buttons to a single slot and parameterize the slot by the button that was clicked.
假设您将许多按钮封装在一个类中,比如 ButtonWidget
,带有自定义信号 void clicked(const QString &text)
。这是定义:
class ButtonWidget : public QWidget {
Q_OBJECT
public:
ButtonWidget(QStringList texts, QWidget *parent = 0);
signals:
void clicked(const QString &text);
private:
QSignalMapper *signalMapper;
};
然后可以像下面这样定义构造函数:
ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
: QWidget(parent)
{
signalMapper = new QSignalMapper(this);
QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));
setLayout(gridLayout);
}
那么这里发生了什么?我们构建了一个网格布局和类型为 QPushButton
的按钮。其中每一个的 clicked()
信号都连接到信号映射器。
使用 QSignalMapper
的力量之一是您可以将参数传递给重新发射的信号。在我们的示例中,每个按钮都应发出不同的文本(由于信号的定义),因此我们使用 setMapping()
方法进行设置。
现在剩下要做的就是将信号映射器映射到我们类的信号:
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));
假设我们有一个名为 TestClass
的测试类,那么可以这样使用 ButtonWidget
:
TestClass::TestClass() {
widget = new ButtonWidget(QStringList() << "Foo" << "Bar");
connect(widget, SIGNAL(clicked(const QString &)),
this, SLOT(onButtonClicked(const QString &)));
}
void TestClass::onButtonClicked(const QString &btnText) {
if (btnText == "Foo") {
// Do stuff.
}
else {
// Or something else.
}
}
通过以这种方式使用信号映射器,您不必声明和管理所有按钮及其点击信号,只需一个信号 pr。 ButtonWidget
.
最重要的是,信号映射器非常适合捆绑多个信号,您甚至可以在它重新发射时设置参数。我希望这能让您对 QSignalMapper
的用法有一些直觉。
您的示例代码
解释(您的“para”)指出所有操作都单独映射到特定的 QWidget*
。当触发一个 Action 时,其各自的 QWidget*
将被传递到 workspace
的插槽 QWorkspace::setActiveWindow(QWidget*)
中,这反过来会激活小部件。
另请注意,从操作到小部件的映射必须发生在代码中的某个位置。我假设它可能是在 createActions()
或 enableActions()
中完成的。
关于c++ - MDI 窗口和 QSignalMapper 基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12652356/
积累和总结,是长期持续的过程 01 最近,很多朋友微信私聊关于「 butte-java-note 」仓库的话题; 这个「 Git仓库 」每年都会
我即将参加挑战测试,所以我不必参加数据库处理类(class)。尽管在过去的 5 年里我一直在使用数据库,但我还是忍不住对测试感到紧张。这是 50 个问题,有 2 部分:真/假部分和我实际编写 SQL
我的 groovy 代码将 Rabbit Native Plugin 用于 grails: def handleMessage(def body, MessageContext context) {
我想看看是否有人可以就我在 .NET 环境中的进一步知识提供任何建议... 这里有一点背景。我上了一所大学并获得了计算机科学学士学位(主要从事 C、Java 和 C++ 方面的工作)。大学毕业后在一家
我在 postgres 数据库中有一个表,该表用于测试环境,我们需要一次添加和删除多个列。问题是 postgres 最多有 1600 列,并且这个计数包括丢弃的列。我的表永远不会有 1600 个“未丢
作为业余程序员 3 年(主要是 Python 和 C)并且从未编写过超过 500 行代码的应用程序,我发现自己面临两个选择: (1) 学习数据结构和算法设计的基本知识,使我成为一名 l33t 计算机科
有人能告诉我为什么 Android 工作需要 Linux 知识吗?许多 Android 工作都以 Linux 作为先决条件。我可以很好地从 Windows 机器开发 Android 应用程序吗? 最佳
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
是否可以在 Drools 中保持知识 session ?如果是这样,如何? 我将事实存储在数据库中,并且每次添加新事实时,我都希望避免在新 session 中重新加载所有事实。 目前,当有新事实时,该
我对 C++ 有很好的了解,但从未深入研究 STL。我必须学习 STL 的哪一部分才能提高工作效率并减少工作中的缺陷? 谢谢。 最佳答案 I have good knowledge of C++ 恕我
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在我从 SO answers here 和许多 BackBoneJs 示例中选择的示例之一中,我看到初始化函数知道模型将使用哪个 View 进行渲染。我不知道我现在有点偏见,这是一个好的做法还是取决于
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 12 年前。 Improve thi
我在我的网站上使用 C# 和 ASP.NET MVC 3 实现 OpenID 和 OAuth。我基于 DotNetOpenAuth用于后端和openid-selector对于前端。 我喜欢 openi
很长一段时间以来,我都在思考和研究C语言编译器以汇编形式的输出,以及CPU架构。我知道这对你来说可能很愚蠢,但在我看来有些东西是非常无效的。如果我错了,请不要生气,我不明白所有这些原则是有原因的。如果
我有一些 Python 知识,但我从来不认为自己对这门语言特别流利。我正在开发一个潜在的机器视觉项目,该项目将从 SimpleCV 中受益匪浅,但从时间的角度来看,我宁愿不必非常流利地使用 pytho
我是一名优秀的程序员,十分优秀!