How do I get values from multiple QLineEdit into one matrix?

336 views Asked by At

I'm quite new to QT, and I wanted to do the following:

Have a layout which initially consist of 1 QLineEdit (text) for input and 1 button for confirming it (button).

Then I wanted to send signal SetN(N) when the button is clicked and this signal should activate ArrayEdit(N) slot, which will change the layot to have N inputs (QLineEdit), and 1 button to send what's in them (as one array) to further processing .

I managed to do the first part, but... it didn't work and I don't know how to deal with this no matching member function error Here's code of my class:

#include "textlayout.h"  

TextLayout::TextLayout()
{ 
    setWindowTitle("Choosing N value");
    resize(200, 200);
    auto layout = new QGridLayout(this);
    auto text = new QLineEdit;
    text->resize(10, 30);

    auto button = new QPushButton();
    button->setText("set");
    layout->addWidget(text, 0, 0);
    layout->addWidget(button, 0, 1);
    
    QObject::connect(&button, SIGNAL(SetN())„ this, SLOT(ArrayEdit())); //no matching member function for call to 'connect' 
    int N = text->text().toInt();

    if(N > 0)
    {
        emit SetN(N); 
    }
}

And its header file:

#ifndef TEXTLAYOUT_H
#define TEXTLAYOUT_H 
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton> 
#include <QGridLayout> 
#include <QObject> 

class TextLayout : public QWidget
{ 
     Q_OBJECT 
public: 
     TextLayout(); 
public slots: 
     void ArrayEdit(int N);I 
signals:
    void SetN(int N); 
};
#endif // TEXTLAYOUT_H 

I know that actually now I don't activate this signal on click, but... well previously I didn't realize that and I don't know how to have sent parameter with onclick signal... How can i work around it and how to fix this class?

2

There are 2 answers

3
Macintron On

If you handle numeric values use QSpinBox instead of QLineEdit. Then you can also set limits and the user can't input invalid data. Use QLineEdit for text.

Widget.hpp

#pragma once

#include <QWidget>


class QGridLayout;
class QLineEdit;
class QPushButton;
class QSpinBox;


class Widget : public QWidget
{
  Q_OBJECT

public:
  Widget(QWidget *parent = nullptr);
  ~Widget();

private slots:
  void adjustLayout(int n);

private:
  QGridLayout* _layout {};
  QPushButton* _button {};
  QSpinBox* _spinBox {};
  QVector<QLineEdit*> _lineEditVector;
};

Widget.cpp

#include "Widget.hpp"
#include <QGridLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
  _layout = new QGridLayout(this);
  _spinBox = new QSpinBox;
  _button = new QPushButton("set");

  _layout->addWidget(_spinBox, 0, 0);
  _layout->addWidget(_button, 0, 1);

  connect(_button, &QPushButton::clicked,
          this, [this](bool) {
            emit adjustLayout(_spinBox->value());
          });
}


Widget::~Widget() = default;


void Widget::adjustLayout(int n)
{
  for (int i=0; i<n; ++i)
  {
    QLineEdit* lineEdit = new QLineEdit(QString::number(i+1));
    // for later access, keep a reference in the vector:
    _lineEditVector.push_back(lineEdit);
    _layout->addWidget(lineEdit, i + 1, 0);
  }
}
0
ΦXocę 웃 Пepeúpa ツ On

you are not far away from doing it.. let me help you a little :)

TextLayout::TextLayout()
{ 
    setWindowTitle("Choosing N value");
    resize(200, 200);
    /*auto*/ layout = new QGridLayout(this);
    //declare layout in header, cos you will need it later in the slot

    auto text = new QLineEdit;
    text->resize(10, 30);

    auto button = new QPushButton();
    button->setText("set");
    layout->addWidget(text, 0, 0);
    layout->addWidget(button, 0, 1);
    
    //QObject::connect(&button, SIGNAL(SetN())„ this, SLOT(ArrayEdit())); //no matching member function for call to 'connect' 
    //you dont need the QObject cos Textlayout is a QObject so just call connect:
   

    connect(this, &TextLayout::SetN, this, &TextLayout::ArrayEdit); 

    //SetN is not a signal of QButton so you cant connect that like that... instead
    connect(&button, &QPushButton::clicked, [this]()
    {
        int N = text->text().toInt();
        if(N > 0)
        {
            emit SetN(N); 
        }  
    }); 
}