[SOLVED]error: 'QLineEdit::QLineEdit(const QLineEdit&)' is private



  • I want to write a GPA calculator.When I wrote part of codes,I wanted to build to try.Then Qt Creator showed " error: 'QLineEdit::QLineEdit(const QLineEdit&)' is private"
    Here is my unfinished code.

    mainwindow.ui
    @<?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>520</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralWidget">
    <widget class="QWidget" name="verticalLayoutWidget">
    <property name="geometry">
    <rect>
    <x>50</x>
    <y>110</y>
    <width>91</width>
    <height>80</height>
    </rect>
    </property>
    <layout class="QVBoxLayout" name="VL1">
    <item>
    <widget class="QLineEdit" name="LEX1"/>
    </item>
    <item>
    <widget class="QLineEdit" name="LEX2"/>
    </item>
    </layout>
    </widget>
    <widget class="QWidget" name="verticalLayoutWidget_2">
    <property name="geometry">
    <rect>
    <x>200</x>
    <y>110</y>
    <width>91</width>
    <height>80</height>
    </rect>
    </property>
    <layout class="QVBoxLayout" name="VL2">
    <item>
    <widget class="QLineEdit" name="LEJ1"/>
    </item>
    <item>
    <widget class="QLineEdit" name="LEJ2"/>
    </item>
    </layout>
    </widget>
    <widget class="QLabel" name="label">
    <property name="geometry">
    <rect>
    <x>80</x>
    <y>80</y>
    <width>54</width>
    <height>12</height>
    </rect>
    </property>
    <property name="text">
    <string>瀛﹀垎</string>
    </property>
    </widget>
    <widget class="QLabel" name="label_2">
    <property name="geometry">
    <rect>
    <x>230</x>
    <y>80</y>
    <width>54</width>
    <height>12</height>
    </rect>
    </property>
    <property name="text">
    <string>缁╃偣</string>
    </property>
    </widget>
    <widget class="QLabel" name="label_3">
    <property name="geometry">
    <rect>
    <x>70</x>
    <y>30</y>
    <width>54</width>
    <height>12</height>
    </rect>
    </property>
    <property name="text">
    <string>骞冲潎缁╃偣</string>
    </property>
    </widget>
    <widget class="QLabel" name="pjjd">
    <property name="geometry">
    <rect>
    <x>180</x>
    <y>30</y>
    <width>54</width>
    <height>12</height>
    </rect>
    </property>
    <property name="text">
    <string>TextLabel</string>
    </property>
    </widget>
    <widget class="QPushButton" name="add">
    <property name="geometry">
    <rect>
    <x>370</x>
    <y>70</y>
    <width>75</width>
    <height>23</height>
    </rect>
    </property>
    <property name="text">
    <string>澧炲姞</string>
    </property>
    </widget>
    <widget class="QPushButton" name="cal">
    <property name="geometry">
    <rect>
    <x>370</x>
    <y>150</y>
    <width>75</width>
    <height>23</height>
    </rect>
    </property>
    <property name="text">
    <string>璁$畻</string>
    </property>
    </widget>
    </widget>
    <widget class="QMenuBar" name="menuBar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>520</width>
    <height>23</height>
    </rect>
    </property>
    </widget>
    <widget class="QToolBar" name="mainToolBar">
    <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
    </attribute>
    <attribute name="toolBarBreak">
    <bool>false</bool>
    </attribute>
    </widget>
    <widget class="QStatusBar" name="statusBar"/>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <resources/>
    <connections/>
    </ui>
    @

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include<QLineEdit>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    double result;
    QList<QLineEdit> LX,LJ;

    public slots:
    void slot_add() ;
    void slot_cal();
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QList>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    LX.append(*(ui->LEX1));
    LX.append(*(ui->LEX2));
    LJ.append(*(ui->LEJ1));
    LJ.append(*(ui->LEJ1));
    connect(ui->cal,SIGNAL(clicked()),this,SLOT(slot_cal()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::slot_add()
    {
    //
    }

    void MainWindow::slot_cal()
    {

    ui->pjjd->setText(LX[0].text());
    

    }
    @

    main.cpp
    @#include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    Thank you for your attention.



  • You need to do some changes to your code.

    mainwindow.h

    1. Use @QList<QLineEdit *> LX,LJ;@

    mainwindow.cpp

    1. Change Line 11,12,13,14 to :-

    @LX.append(ui->LEX1);
    LX.append(ui->LEX2);
    LJ.append(ui->LEJ1);
    LJ.append(ui->LEJ1);@

    1. Change line 32 to :-

    @QLineEdit *lineEdit = LX.at(0);
    ui->pjjd->setText(lineEdit->text());@

    Edit: As suggested by DerManu, Changed QLineEdit *lineEdit = static_cast<QLineEdit *> (LX.at(0));



  • Some explanation to Sams post: The central thing is that you can't have QObjects stored by value in containers. Because this implies that the objects are copyable – QObject isn't, that's why the copy constructor is private (see your error message). QObject was made not-copyable because it should not be thought of as a standalone data-carrier with some associated functions, but actually an entity in a larger network (see Qts parenting system and signal/slot connections). If you could copy a QObject, would the copy also have the same parent? Would it also have the same connections set?

    That's why you save pointers to objects in containers rather than QObjects themselves. Pointers are just integer numbers basically. They can be stored by value without trouble.

    bq. QLineEdit *lineEdit = static_cast<QLineEdit *> (LX.at(0));

    That's unnecessary since LX is already a list of QLineEdit pointers.



  • Thanks for the explanations DerManu :)



  • Thank you all very much


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.