Qt的新手。你能帮我理解如何从两个不同的线编辑连接两个QStrings吗?
我有两行编辑gui。前缀文本和数字文本。我想将前缀和数字值组合成一个字符串。
//connect the line edit slots to signals
QObject::connect(ui->prefixLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(prefixChanged()));
QObject::connect(ui->startNumberLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(startNumChanged()));
//functions to run when line edit is changed
void MainWindow::prefixChanged(){
QString prefixText = ui->prefixLineEdit->text();
}
void MainWindow::startNumChanged(){
QString startNumberText = ui->startNumberLineEdit->text();
//combine prefix and start number??
QString combined = (prefixText + startNumberText);
ui->statusbar->showMessage(combined);
}
我想一般的问题是如何在插槽之间共享变量?我已经阅读了有关QSignalMapper的信息,可能会创建一个代理槽来传递参数,并查看了QString的.args()函数。但现在这一切都超越了我。
我是否必须公开这些变量才能从插槽功能中访问它们?我觉得那有点不受欢迎?
非常感谢任何建议。
答案 0 :(得分:3)
You can do like that, just accessing ui
where you need:
void MainWindow::startNumChanged(){
QString startNumberText = ui->startNumberLineEdit->text();
QString prefixText = ui->prefixLineEdit->text();
//combine prefix and start number
QString combined = prefixText + startNumberText;
ui->statusbar->showMessage(combined);
}
You can move this code to some function and call it from each slot if you need.
In more complex cases, you would need private variables of class in header:
QString startNumberText;
QString prefixText;
答案 1 :(得分:0)
(代表OP发布)。
根据demonplus的回答,我精心设计了这个功能:
void MainWindow::combineOutputName(){
QString prefixText = ui->prefixLineEdit->text();
int incrementValue = ui->incrementSpinBox->value();
QString startNumberValue = ui->startNumberLineEdit->text();
int newValue = startNumberValue.toInt() + incrementValue;
ui->exampleLabel->setText(QString::number(newValue));
}
完成将来自不同UI元素的输入汇总到一个变量中。