首页 文章

如何将lineEdits分配给某个变量?

提问于
浏览
2

我是Qt的新手 . 我被困在这件事上了 . 我的应用程序中有很多lineEdits,其中的值可能会在应用程序运行时随时更改 . lineEdits中的值只是整数 . 在某些阶段,我需要检查lineEdits中的值并与整数数组进行比较 . 如果它们相等,则完成用户的任务 .

在这里,我想将lineEdits的所有值存储到整数数组,以便我可以运行 for 循环来检查两个数组是否相等而不是为每个 lineEdit 制作 if 条件 . 每当用户更改lineEdit中的值时,数组中的值都应更新,并且当数组中的相应值更改时,lineEdit中的值也应更改 .

我尝试了一个qvector,将lineEdits的值附加到其中 . 此向量现在具有lineEdits的值,但在向量中更改其对应值时不会更新值 .

任何人都可以帮忙,如何做到这一点?

3 回答

  • 0

    你可以有一个指向 QLineEdit 的指针列表作为类成员:

    QList<QLineEdit *> lineEdits;
    

    并在实例化时将指针添加到列表中:

    QLineEdit * lineEdit = new QLineEdit(this);
    lineEdits->append(lineEdit);
    

    然后 QSignalMapper 用于在更新行编辑时更新值数组 . QSignalMapper class收集一组无参数信号,并使用与发送信号的对象相对应的整数,字符串或窗口小部件参数重新发出它们 . 所以你可以有一个像:

    QSignalMapper * mapper = new QSignalMapper(this);
    QObject::connect(mapper,SIGNAL(mapped(int)),this,SLOT(OntextChanged(int)));
    

    对于每个行编辑,您可以将 textChanged() 信号连接到 QSignalMappermap() 插槽,并使用 setMapping 添加映射,以便在从行编辑发出 textChanged() 信号时,会发出信号 mapped(int)

    for(int i=0; i<lineEdits.count(); i++)
    {
        QObject::connect(lineEdits[i], SIGNAL(textChanged()),mapper,SLOT(map()));
        mapper->setMapping(lineEdits[i], i+1);
    }
    

    这样,无论何时更改行编辑,都会发出映射器的 mapped(int) 信号,其中包含行编辑的索引作为参数 .

    可以在 OntextChanged 插槽中更新数组的值,如:

    void MyClass::OntextChanged(int index)
    {
       values[index-1] = lineEdits[index-1].text().toInt();
    }
    
  • 2

    我建议使用QDataWidgetMapper,因为它会使用模型/视图设计模式,它有几个优点 . 下面的示例清楚地显示了所有QLineEdit中的数据可以很容易地进行汇总 . 该示例包含两个文件main.cpp和mainwin.h:

    mainwin.h:

    #ifndef MAINWIN_H
    #define MAINWIN_H
    #include <QtGui>
    static const int lineEditCount=4;
    class MainWin : public QMainWindow
    {
          Q_OBJECT
          QStandardItemModel& model;
       public:
          explicit MainWin(QStandardItemModel& m)
          : model(m)
          {
             setWindowTitle("QDataWidgetMapper Example");
             resize(400,400);
             QDataWidgetMapper* mapper=new QDataWidgetMapper(this);
             mapper->setModel(&m);
             QWidget *w = new QWidget;
             QVBoxLayout *layout = new QVBoxLayout;
             w->setLayout(layout);
            for(int i=0;i<lineEditCount;++i)
            {
               QLineEdit* e=new QLineEdit;
               layout->addWidget(e);
               mapper->addMapping(e,i);
            }
            layout->addWidget(new QTextEdit);
            setCentralWidget(w);
            mapper->setCurrentIndex(0); // the only existing row is activated
            reactToChange(0);
            connect(&m,SIGNAL(itemChanged(QStandardItem*)), 
                     this,SLOT(reactToChange(QStandardItem*)));
          }
       private slots:
          void reactToChange(QStandardItem*)
          {
             QTextEdit* t=findChild<QTextEdit*>();
             t->append("=========================");
             for(int i=0;i<lineEditCount;++i)
                t->append(model.item(0,i)->text());
          }
    };
    #endif
    

    main.cpp:

    #include <QtGui>
    #include "mainwin.h"
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        /* Create the data model. It has only one row, but as many columns as there are
           QLineEdit. */
        QStandardItemModel model(1,lineEditCount);
        for(int column = 0; column < 4; ++column)
        {
          QStandardItem *item = new QStandardItem(QString("QLineEdit %0 value").arg(column));
          model.setItem(0,column, item);
        }
        MainWin mainWin(model);
        mainWin.show();
        return app.exec();
    }
    

    每次 QLineEdit 中的任何一个更改时,都会显示所有 QLineEdit 中值的新摘要,请参见下文 .

    enter image description here

  • 0

    Qt是关于信号和插槽的 . 在您的情况下,您需要将 lineEdit 的文本编辑信号连接到要修改的变量 . 在你拥有 lineEdit 的对话框中,你应该在构造函数中有一个连接,例如:

    connect(ui->lineEdit, &QLineEdit::textEdited, this, &YourDialog::textEdited);
    

    这会将 lineEdittextEdited 信号连接到 YourDialog 的插槽,我也将此命名为 textEdited .

    这个插槽应该采用一个 const QString& 的单个参数,因为这是 QLineEdit::textEdited 根据Qt的文档发出的 .

    然后,您可以在此插槽函数中执行任何操作,例如将输入转换为带有 lineEdit->text().toInt()int 并将其值分配给数组元素等 .

    此外,如果您确定唯一的条目应该是 int 到此 lineEdit ,则在构造函数中,您可以使用带有 lineEdit->setValidatorQIntValidator 对象来确保用户将 lineEdit 值编辑为您指定的范围内的有效整数 .

相关问题