如何将多个QLineEdit的值获取到一个矩阵中



我对QT很陌生,我想做以下事情:

布局最初由1个用于输入的QLineEdit(text(和1个用于确认的按钮(button(组成。

然后,我想在点击按钮时发送信号SetN(N),这个信号应该激活ArrayEdit(N)插槽,这将改变布局,使其具有N个输入(QLineEdit(,并通过1个按钮将其中的内容(作为一个数组(发送给进一步处理。

我设法完成了第一部分,但是。。。它不起作用,我不知道如何处理这个no matching member function错误这是我班的代码:

#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); 
}
}

及其头文件:

#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 

我知道实际上现在我不会在点击时激活这个信号,但是。。。之前我没有意识到这一点,也不知道如何用onclick信号发送参数。。。我该如何解决它,以及如何修复这个类?

你离做这件事不远了……让我帮你一点:(

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); 
}  
}); 
}

如果处理数值,请使用QSpinBox而不是QLineEdit。然后,您还可以设置限制,用户不能输入无效数据。对文本使用QLineEdit。

小工具.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;
};

小工具.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);
}
}

相关内容

  • 没有找到相关文章

最新更新