使用“connect()”时出现问题在 qt4 的迭代中,使用 clicked() 信号并使用来自相应槽迭代的信息
为此,我使用 qt4 用 c++ 编程来设计我的界面。
在这个程序中,我创建了一个小部件,它根据用户输入生成一个(未知)数量的 QPushButtons,因此我动态地创建它们。在创建时,我想将这些按钮连接到一个函数,该函数需要创建按钮的迭代中存在的参数(例如,指示 a 向量中的位置的 int),但我不能这样做由于信号/槽的性质以及来自 QPushButtons 的 clicked() 信号的性质(它不接受参数)。
代码看起来有点像这样:
vector<int> myVector
for(int i=0; i<user_input_number; i++){
...
QPushButton *testingb4 = new QPushButton("Execute", this);
connect( testingb4, SIGNAL( clicked() ), this, SLOT( customSlot() ) );
...
}
“customSlot”需要“i”来确定我应该访问向量的哪个元素。我该如何解决这个问题?
For this, I'm programming in c++ using qt4 to design my interface.
In this program, I create a widget which spawns a (unknown) number QPushButtons depending on user input, thus I am creating them dynamically. At the moment of creation, I want to connect these buttons to a function that would need parameters that exist within the iteration of creating the buttons (for example, an int that would indicate a position in the a vector), yet I cannot do this due to the nature of signals/slots and the nature of the clicked() signal from the QPushButtons (it does not accept parameters).
The code looks somewhat like this:
vector<int> myVector
for(int i=0; i<user_input_number; i++){
...
QPushButton *testingb4 = new QPushButton("Execute", this);
connect( testingb4, SIGNAL( clicked() ), this, SLOT( customSlot() ) );
...
}
"customSlot" would need "i" to determine what element of my vector I should access. How do I get solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在寻找 QSignalMapper。这可以帮助您识别点击信号的来源。
It seems you are looking for QSignalMapper. This may help you identify the source of click signals.