[英]QT - connecting QPushButtons with QCheckBoxes
I am new to QT. 我是QT新手。 I started creating a TODO app and I want to somehow connect my PushButtons that are placed in vector with CheckBoxes that are also placed in a different vector.
我开始创建TODO应用,我想以某种方式将放置在矢量中的按钮与也放置在不同矢量中的CheckBox连接起来。
std::vector <QPushButton*> buttons;
std::vector <QCheckBox*> checks;
I thought that the best way to do that will be to make a for loop connecting every element of mentioned vectors 我认为最好的方法是建立一个for循环,连接提到的向量的每个元素
Something like: 就像是:
for(int i=0; i<buttons.size(); ++i){
connect(buttons[i], SIGNAL(???), checks[i], SLOT(???));
}
But idea is the only thing that I have. 但是想法是我唯一的东西。 I tried putting different things into SIGNAL() and SLOT() but none of them worked.
我尝试将不同的东西放到SIGNAL()和SLOT()中,但是它们都不起作用。 By "none of them worked" I mean the fact that when button is clicked nothing happens.
“没有一个起作用”是指当单击按钮时什么也没有发生的事实。 Program is normally compiled without any error.
程序通常已编译,没有任何错误。
What about just clicked(bool)
for SIGNAL
and toggle()
for SLOT
? 刚刚
clicked(bool)
的SIGNAL
和toggle()
的SLOT
怎么办?
Something like that: 像这样:
connect(pushButton, SIGNAL(clicked(bool)), checkBox, SLOT(toggle()));
Works for me - and you can store the widgets directly in a std::list
: that avoids the need to mess with manual memory management. 适用于我-您可以将小部件直接存储在
std::list
:这样就避免了手动存储管理的麻烦。 Let the libraries do it for you. 让图书馆为您做。
#include <QtWidgets>
#include <list>
int main(int argc, char **argv) {
QApplication app{argc, argv};
QWidget win;
QGridLayout layout{&win};
std::list<QPushButton> buttons;
std::list<QCheckBox> checkboxes;
QPushButton addButton{"Add"};
layout.addWidget(&addButton, 0, 0, 2, 1);
auto const clicked = &QAbstractButton::clicked;
auto const toggle = &QAbstractButton::toggle;
auto const add = [&,clicked,toggle]{
int const col = layout.columnCount();
auto const text = QString::number(col);
auto *button = &(buttons.emplace_back(text), buttons.back()); //C++11, not 14
auto *checkbox = &(checkboxes.emplace_back(text), checkboxes.back());
layout.addWidget(button, 0, col);
layout.addWidget(checkbox, 1, col);
QObject::connect(button, clicked, checkbox, toggle);
};
add();
QObject::connect(&addButton, clicked, add);
win.show();
return app.exec();
}
With Qt-5 you can now use lambda functions as slots (see connect version 5 ) 使用Qt-5,您现在可以将lambda函数用作插槽(请参阅连接版本5 )
You can also do away with the need for the SIGNAL
macro, and instead use member function pointers. 您也可以消除对
SIGNAL
宏的需要,而使用成员函数指针。
QObject::connect(buttons[i], &QPushButton::clicked, [=]
{
// toggle the check state
checks[i]->setChecked(!checks[i]->isChecked());
});
The first two parameters are a pointer to an object, and a member function pointer 前两个参数是指向对象的指针和成员函数指针
buttons[i]
is of type QPushButton*
buttons[i]
的类型为QPushButton*
&QPushButton::clicked
is a member function pointer of the signal you want to connect to &QPushButton::clicked
是您要连接的信号的成员函数指针 The second parameter is a C++11 lambda, which captures checked
and i
by value, and then sets the QCheckBox
checked state to the inverse of its previous value 第二个参数是C ++ 11 lambda,它按值捕获
checked
和i
,然后将QCheckBox
选中的状态设置为其先前值的倒数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.