스핀 박스의 이름은 어떻게 가져올 수 있습니까? 그러나 많은 설명서를 살펴 보았지만 아이 스핀 박스의 이름이 표시되는 것을 찾을 수 없었습니다. 결과를 문자열로 변경하려고했습니다. 그러나, 나는 단지 상상할 수있는 주소의 16 진수 또는 긴 정수를 얻습니다. 대신에 반환되었습니다. 내가 이름을 얻을 수있는 직접적인 방법이없는 spinboxesQt에서 스핀 박스의 이름을 얻고 싶습니다.
답변
검색하려는 이름이 objectName
재산이다 QObject
및 QObject
의 모든 유급 클래스가 있습니다. 이 값을 검색하려면 objectName()
으로 전화하십시오.
QObject::findChild()
기능과 함께 사용할 수도 있습니다.
이것은 당신이 원하는 것을 얻을해야합니다
그리고 출력 :
"norm_spinBox_10"SpinBoxWrite을
주
이 줄은 위험하다 :
QSpinBox* sp= (QSpinBox*)wSp;
C 스타일의 캐스트 대신 qobject_cast
을 사용하십시오.
@ tisaconundrum C 스타일의 캐스트에 대한 편집 된 경고에 유의하십시오. –
또한''sender' (http://doc.qt.io/qt-5/qobject.html#sender) 함수와 함께'qobject_cast'를 사용하면'QSignalMapper'를 제거 할 수 있습니다. –
에 대한 정보를 얻는 데 문제가있는 곳이기 때문에
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() < < 전자을 유의하시기 바랍니다 변수의 문자열로.
그러나 QMap<QSpinBox*, QString>
을 사용하여 각 스핀 상자를 해당 이름에 매핑 할 수 있습니다.
map[ui->spinBox] = "spinBox";
map[ui->spinBoxWithStrangeName] = "spinBoxWithStrangeName";
단순히 사용하여 문자열을 얻을 수 있습니다 : 생성자에서
당신은 수동을 할당해야 이QString name = map[ui->spinBox];
디자이너 파일에 이름을 지정한 다음 해당 이름을 사용하여 C++ 코드에서 이름을 검색하면됩니다.
QSpinBox* mySpinner = findChild<QSpinBox*>("myGivenName");
회원 변수로 저장할 수없는 이유가 있습니까? – GraphicsMuncher
그 (것)들에게 이름을주고 참조하기 위하여 그것을 사용하는 것이 쉬운가요? – RvdK
아니, 그 이름을 어떻게 든 찾아야하기 때문이다. 'findChild',지도, 해시 등은 아마도 포인터를 멤버 변수로 기억하는 것보다 런타임 비용이 훨씬 더 크다. – peppe