我想在 Qt 中获取我的旋转框的名称
I want to get the names of my spinboxes in Qt
如何提取我的旋转框的名称?我试着查看了很多文档,但是,我找不到任何可以显示每个子 spinBox 的名称的内容。我试过将结果更改为字符串。但是,我只是得到了我想象中的地址的 Hex 或 Long Int,而不是返回。
QList<QSpinBox*> spinBoxes= findChildren<QSpinBox*>();
//create the QSignalMapper object
QSignalMapper* signalMapper= new QSignalMapper(this);
//loop through your spinboxes list
QSpinBox* spinBox;
foreach(spinBox, spinBoxes){
//setup mapping for each spin box
connect(spinBox, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
signalMapper->setMapping(spinBox, spinBox);
}
//connect the unified mapped(QWidget*) signal to your spinboxWrite slot
connect(signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(spinboxWrite(QWidget*)));
.
.
.
void GuiTest::SpinBoxChanged(QWidget* wSp){
QSpinBox* sp= (QSpinBox*)wSp; //now sp is a pointer to the QSpinBox that emitted the valueChanged signal
int value = sp->value(); //and value is its value after the change
//do whatever you want to do with them here. . .
qDebug() << value << "SpinBoxChanged";
}
void GuiTest::spinboxWrite(QWidget* e){
SpinBoxChanged(e);
QString* value = (QString*)e;
qDebug() << e << value << " SpinBoxWrite";
}
请注意 qDebug() << e 因为这是我在获取有关旋转框的一些信息时遇到的问题
没有直接的方法来获取字符串形式的变量名。
但是,您可以使用 QMap<QSpinBox*, QString>
将每个旋转框映射到它的名称。
在构造函数中,您必须手动分配这些:
map[ui->spinBox] = "spinBox";
map[ui->spinBoxWithStrangeName] = "spinBoxWithStrangeName";
然后您可以使用以下方法简单地获取字符串:
QString name = map[ui->spinBox];
您要检索的名称是 objectName
属性,每个 QObject
和 QObject
派生的 class 都有。调用 objectName()
以检索此值。
您也可以将其与 QObject::findChild()
函数一起使用。
这应该得到你想要的:
void GuiTest::spinboxWrite(QWidget* e){
SpinBoxChanged(e);
qDebug() << e->objectName() << " SpinBoxWrite";
并将输出:
"norm_spinBox_10" SpinBoxWrite
备注
这行是危险的:
QSpinBox* sp= (QSpinBox*)wSp;
使用 qobject_cast
而不是 C 风格的转换。
只需在设计器文件中给它们命名,然后使用该名称在 C++ 代码中检索它们。
QSpinBox* mySpinner = findChild<QSpinBox*>("myGivenName");
如何提取我的旋转框的名称?我试着查看了很多文档,但是,我找不到任何可以显示每个子 spinBox 的名称的内容。我试过将结果更改为字符串。但是,我只是得到了我想象中的地址的 Hex 或 Long Int,而不是返回。
QList<QSpinBox*> spinBoxes= findChildren<QSpinBox*>();
//create the QSignalMapper object
QSignalMapper* signalMapper= new QSignalMapper(this);
//loop through your spinboxes list
QSpinBox* spinBox;
foreach(spinBox, spinBoxes){
//setup mapping for each spin box
connect(spinBox, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
signalMapper->setMapping(spinBox, spinBox);
}
//connect the unified mapped(QWidget*) signal to your spinboxWrite slot
connect(signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(spinboxWrite(QWidget*)));
.
.
.
void GuiTest::SpinBoxChanged(QWidget* wSp){
QSpinBox* sp= (QSpinBox*)wSp; //now sp is a pointer to the QSpinBox that emitted the valueChanged signal
int value = sp->value(); //and value is its value after the change
//do whatever you want to do with them here. . .
qDebug() << value << "SpinBoxChanged";
}
void GuiTest::spinboxWrite(QWidget* e){
SpinBoxChanged(e);
QString* value = (QString*)e;
qDebug() << e << value << " SpinBoxWrite";
}
请注意 qDebug() << e 因为这是我在获取有关旋转框的一些信息时遇到的问题
没有直接的方法来获取字符串形式的变量名。
但是,您可以使用 QMap<QSpinBox*, QString>
将每个旋转框映射到它的名称。
在构造函数中,您必须手动分配这些:
map[ui->spinBox] = "spinBox";
map[ui->spinBoxWithStrangeName] = "spinBoxWithStrangeName";
然后您可以使用以下方法简单地获取字符串:
QString name = map[ui->spinBox];
您要检索的名称是 objectName
属性,每个 QObject
和 QObject
派生的 class 都有。调用 objectName()
以检索此值。
您也可以将其与 QObject::findChild()
函数一起使用。
这应该得到你想要的:
void GuiTest::spinboxWrite(QWidget* e){
SpinBoxChanged(e);
qDebug() << e->objectName() << " SpinBoxWrite";
并将输出:
"norm_spinBox_10" SpinBoxWrite
备注
这行是危险的:
QSpinBox* sp= (QSpinBox*)wSp;
使用 qobject_cast
而不是 C 风格的转换。
只需在设计器文件中给它们命名,然后使用该名称在 C++ 代码中检索它们。
QSpinBox* mySpinner = findChild<QSpinBox*>("myGivenName");