gpt4 book ai didi

c++ - 无法将QWidget扩展的实例添加到QVector

转载 作者:行者123 更新时间:2023-12-02 10:37:06 26 4
gpt4 key购买 nike

我有一个Variable类,是QWidget的扩展。我想将其添加到QVector <Variable> *list_variables vector 中,但是当我尝试时,出现错误

Attempt to refer to a deleted function

这是代码:
QVector <Variable> *list_variables;
QVBoxLayout *layout = new QVBoxLayout (scroll_area);

for (int i = 0; i < 5; i ++)
{
Variable *variable = new Variable;
list_variables->append(*variable); // Error happens here
layout->addWidget(variable);
}

Variable.h:
#ifndef VARIABLE_H
#define VARIABLE_H

#include "constant.h"

class Variable : public QWidget
{
public :
Variable();

private :
QLineEdit *line_edit_name, *line_edit_value, *line_edit_imprecision;
};

#endif // VARIABLE_H

Variable.cpp:

#include "constant.h"

Variable::Variable()
{
this->setFixedSize(240, 30);

line_edit_name = new QLineEdit(this);
line_edit_name->setGeometry(0, 0, 50, 20);
line_edit_name->setPlaceholderText("Name");

line_edit_value = new QLineEdit (this);
line_edit_value->setGeometry(60, 0, 80, 20);
line_edit_value->setPlaceholderText("Value");

line_edit_imprecision = new QLineEdit (this);
line_edit_imprecision->setGeometry(150, 0, 80, 20);
line_edit_imprecision->setPlaceholderText("Imprecision");
}

所有包含内容都在“constant.h”中完成

最佳答案

我找到了一个解决方案:将QVector声明为Variable指针的 vector 。

所以我有:

QVector <Variable*> list_variables; // Instead of QVector <Variable> *list_variables;
QVBoxLayout *layout = new QVBoxLayout (scroll_area);

for (int i = 0; i < 5; i ++)
{
Variable *variable = new Variable;
list_variables.append(variable); // Instead of list_variables->append(*variable);
layout->addWidget(variable);
}

关于c++ - 无法将QWidget扩展的实例添加到QVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59798687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com