我已经为 L00
(我的第一个 QLineEdit
的名称)完成了一些编码。
我想对其他 99 个小部件(最多 L99
)执行相同的操作。我不想复制粘贴 10000 次。
void MainWindow::on_L00_returnPressed(){
QString c = ui->L00->text();
char d = QString(c).at(0).toLatin1();
if(d>=65&&d<=90||d>=97&&d<=122)
{
square[0]=d;
ui->MessageBox->setText("Alphabet succesfully entered");
ui->L00->setReadOnly(true);
ui->L00->setStyleSheet("QLineEdit { background: rgb(0, 255, 255);}");
}
else
{
ui->MessageBox->setText("Enter Alphabets Only");
ui->L00->setText("");
}
}
最佳答案
如果通过Qt Designer
添加所有这些小部件,这些对象将具有与变量相同的名称,因此我们可以通过findChild
获取它们,然后应用连接信号ReturnPressed
到 lambda 函数,而不必为每个 QLineEdit
创建一个槽。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
int n = 100;
for(int i=0; i<n; i++){
QString name = QString("L%1").arg(i, 2, 10, QChar('0'));
QLineEdit* w = findChild<QLineEdit *>(name);
connect(w, &QLineEdit::returnPressed, [=](){
QString c = w->text();
char d = QString(c).at(0).toLatin1();
if((d>=65&& d<=90)||(d>=97&&d<=122))
{
square[i]=d;
ui->MessageBox->setText("Alphabet succesfully entered");
w->setReadOnly(true);
w->setStyleSheet("QLineEdit { background: rgb(0, 255, 255);}");
}
else
{
ui->MessageBox->setText("Enter Alphabets Only");
w->clear();
}
});
}
}
注意:我认为 square[i]
对应于 QLineEdit
L-i。
另一种解决方案是将所有连接到同一个槽并通过函数sender()获取对象。
*.h
private slots:
void onReturnPressed();
*.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(QLineEdit* w: findChildren<QLineEdit *>()){
connect(w, &QLineEdit::returnPressed, this, &MainWindow::onReturnPressed);
}
}
void MainWindow::onReturnPressed()
{
QLineEdit* w =qobject_cast<QLineEdit *>(sender());
int i = w->objectName().remove(0, 1).toInt();
QString c = w->text();
char d = QString(c).at(0).toLatin1();
if((d>=65&& d<=90)||(d>=97&&d<=122))
{
square[i]=d;
ui->MessageBox->setText("Alphabet succesfully entered");
w->setReadOnly(true);
w->setStyleSheet("QLineEdit { background: rgb(0, 255, 255);}");
}
else
{
ui->MessageBox->setText("Enter Alphabets Only");
w->clear();
}
}
对于旧版本:
*.h
private slots:
void onReturnPressed();
.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
int n = 100;
for(int i=0; i<n; i++){
QString name = QString("L%1").arg(i, 2, 10, QChar('0'));
QLineEdit* w = findChild<QLineEdit *>(name);
if(w)
connect(w, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
}
}
void MainWindow::onReturnPressed()
{
QLineEdit* w =qobject_cast<QLineEdit *>(sender());
int i = w->objectName().remove(0, 1).toInt();
QString c = w->text();
char d = QString(c).at(0).toLatin1();
if((d>=65&& d<=90)||(d>=97&&d<=122))
{
square[i]=d;
ui->MessageBox->setText("Alphabet succesfully entered");
w->setReadOnly(true);
w->setStyleSheet("QLineEdit { background: rgb(0, 255, 255);}");
}
else
{
ui->MessageBox->setText("Enter Alphabets Only");
w->clear();
}
}
关于c++ - 如何一次更改多个qlineedit的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363134/