Widget copy problem



  • Hello,
    In my application, i need to get one widget from a QList and add it into a layout. So i have : @layout->addWidget(myList.at(i));@ .

    After modifiying my code in
    @layout->addWidget(new QLabel(myList.at(i).text()));@

    i've got some critical error : @In file included from ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qobject.h:50,
    from ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qabstractanimation.h:45,
    from ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/QtCore:3,
    from ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/QtGui:3,
    from ..\CSS_Explicit/mainwidget.h:4,
    from ..\CSS_Explicit\mainwidget.cpp:1:
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = QLineEdit]':
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:512: instantiated from 'void QList<T>::append(const T&) [with T = QLineEdit]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:334: instantiated from 'QList<T>& QList<T>::operator<<(const T&) [with T = QLineEdit]'
    ..\CSS_Explicit\mainwidget.cpp:11: instantiated from here
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h:279: error: 'QLineEdit::QLineEdit(const QLineEdit&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:372: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h:279: error: 'QLineEdit::QLineEdit(const QLineEdit&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:373: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h:279: error: 'QLineEdit& QLineEdit::operator=(const QLineEdit&)' is private@



  • @....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:378: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = QLabel]':
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:512: instantiated from 'void QList<T>::append(const T&) [with T = QLabel]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:334: instantiated from 'QList<T>& QList<T>::operator<<(const T&) [with T = QLabel]'
    ..\CSS_Explicit\mainwidget.cpp:13: instantiated from here
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h:165: error: 'QLabel::QLabel(const QLabel&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:372: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h:165: error: 'QLabel::QLabel(const QLabel&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:373: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h:165: error: 'QLabel& QLabel::operator=(const QLabel&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:378: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h: In member function 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QLineEdit]':
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:681: instantiated from 'QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = QLineEdit]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:510: instantiated from 'void QList<T>::append(const T&) [with T = QLineEdit]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:334: instantiated from 'QList<T>& QList<T>::operator<<(const T&) [with T = QLineEdit]'
    ..\CSS_Explicit\mainwidget.cpp:11: instantiated from here
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h:279: error: 'QLineEdit::QLineEdit(const QLineEdit&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:399: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtGui/qlineedit.h:279: error: 'QLineEdit::QLineEdit(const QLineEdit&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:412: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h: In member function 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QLabel]':
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:681: instantiated from 'QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = QLabel]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:510: instantiated from 'void QList<T>::append(const T&) [with T = QLabel]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:334: instantiated from 'QList<T>& QList<T>::operator<<(const T&) [with T = QLabel]'
    ..\CSS_Explicit\mainwidget.cpp:13: instantiated from here
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h:165: error: 'QLabel::QLabel(const QLabel&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:399: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qlabel.h:165: error: 'QLabel::QLabel(const QLabel&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:412: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qboxlayout.h: In member function 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QHBoxLayout]':
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:711: instantiated from 'void QList<T>::detach_helper(int) [with T = QHBoxLayout]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:725: instantiated from 'void QList<T>::detach_helper() [with T = QHBoxLayout]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:446: instantiated from 'QList<T>& QList<T>::operator=(const QList<T>&) [with T = QHBoxLayout]'
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:766: instantiated from 'void QList<T>::clear() [with T = QHBoxLayout]'
    ..\CSS_Explicit\mainwidget.cpp:15: instantiated from here
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qboxlayout.h:146: error: 'QHBoxLayout::QHBoxLayout(const QHBoxLayout&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:399: error: within this context
    ....\Qt\Desktop\Qt\4.8.1\mingw\include\QtGui/qboxlayout.h:146: error: 'QHBoxLayout::QHBoxLayout(const QHBoxLayout&)' is private
    ....\Qt\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:412: error: within this context
    mingw32-make.exe[1]: *** [debug/mainwidget.o] Error 1
    mingw32-make.exe: *** [debug] Error 2@

    All of those errors are in qt files and i can't copy/add widget to another widget !

    I need help !
    Thanks.
    Xentrum



  • [quote author="Xentrum" date="1339606742"]Hello,
    In my application, i need to get one widget from a QList and add it into a layout. So i have : @layout->addWidget(myList.at(i));@ .

    After modifiying my code in
    @layout->addWidget(new QLabel(myList.at(i).text()));@

    [/quote]
    Yes, If I am right, what you want it is
    @layout->addWidget(new QLabel(myList.at(i)->text()));@



  • Yes. Principal error is about copying the widget : the overloaded constructor for copying QLabel/QLineEdit... Is suggested by Qt Creator but the debbuger says that this constructor is private


  • Moderators

    This "link":http://qt-project.org/doc/qt-4.8/object.html#identity-vs-value can help understanding why.


  • Moderators

    Copying QWidgets is inherently prohibited. (What is a "copy" of a widget supposed to include?)

    What is the purpose of your list of widgets? Would you be better off keeping a list of widget pointers?

    That way you could work without having to make copies of the widgets themselves.



  • I want to do dynamics lines with 2 QLineEdit and 1 Label. The number of lines depend of the size of my QList which is the same as two other QList. Each list contains one widget(QLineEdit/Qlabel) and i'm doing "for" loop for creating my layout :
    @for(int i = 0;i <= propertyList.size();i++){
    QHBoxLayout *layoutExample = new QHBoxLayout();
    layoutExample->addWidget(new QLabel(labelList.at(i).text()));
    layoutExample->addWidget(new QLineEdit(propertyList.at(i).text()));
    layoutExample->addWidget(new QLineEdit(valueList.at(i).text()));
    layout->addLayout(layoutExample);
    }@

    EDIT : I just recompiled my codes and even when i've deleted this part the pb stay. Maybe should i repair my Qt installation ?


  • Moderators

    So what types are the QLists of? Where do they come from? Is it sufficient for the QLists to just contain QString values for the text, rather than widgets?

    [Edit to add:]

    You might also consider creating a custom widget class which contains your Label and TextEntry fields. Might make things a little tidier in working with things. (Just a suggestion, though.)



  • this is already a new widget. here's my complete code :
    mainwidget.h:
    @#ifndef MAINWIDGET_H
    #define MAINWIDGET_H

    #include <QtGui>

    class MainWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit MainWidget(QWidget *parent = 0);
    MainWidget(QString name);

    signals:

    public slots:

    private:
    QVBoxLayout *layout;
    QString tabName;
    QList<QLineEdit> propertyList;
    QList<QLineEdit> valueList;
    QList<QLabel> labelList;
    QList<QHBoxLayout> *linePropertyList;
    };

    #endif // MAINWIDGET_H
    @

    and mainwidget.cpp :

    @
    #include "mainwidget.h"

    MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
    {
    linePropertyList = new QList<QHBoxLayout>;
    QHBoxLayout *mainlayout = new QHBoxLayout;
    QString tabName = "New";
    layout = new QVBoxLayout;
    if(propertyList.isEmpty()){
    propertyList <<QLineEdit("color");
    valueList << QLineEdit("black");
    labelList << QLabel(tr("Property 1"));
    }
    linePropertyList->clear();
    for(int i = 0;i <= propertyList.size();i++){
    QHBoxLayout *layoutExample = new QHBoxLayout();
    layoutExample->addWidget(new QLabel(labelList.at(i).text()));
    layoutExample->addWidget(new QLineEdit(propertyList.at(i).text()));
    layoutExample->addWidget(new QLineEdit(valueList.at(i).text()));
    layout->addLayout(layoutExample);
    }
    mainlayout->addLayout(layout);
    mainlayout->addWidget(new QTextEdit());
    setLayout(mainlayout);
    }
    MainWidget::MainWidget(QString name)
    {
    QHBoxLayout *mainlayout = new QHBoxLayout;
    QString tabName = name;
    layout = new QVBoxLayout;
    mainlayout->addLayout(layout);
    mainlayout->addWidget(new QTextEdit());
    setLayout(mainlayout);
    setMinimumSize(400,800);
    }
    @


  • Moderators

    Again, I ask...

    Why are your lists of widgets, instead of QStrings? You only appear to be using the text values of those widgets in the lists.

    Why not:
    @
    ...
    QList<QString> propertyList; // Or QStringList propertyList;
    QList<QString> valueList;
    QList<QString> labelList;
    ...
    if(propertyList.isEmpty()){
    propertyList <<"color";
    valueList << "black";
    labelList << tr("Property 1");
    }
    ...
    for(int i = 0;i <= propertyList.size();i++){
    QHBoxLayout *layoutExample = new QHBoxLayout();
    layoutExample->addWidget(new QLabel(labelList.at(i)));
    layoutExample->addWidget(new QLineEdit(propertyList.at(i)));
    layoutExample->addWidget(new QLineEdit(valueList.at(i)));
    layout->addLayout(layoutExample);
    }
    ...
    @



  • i tried to directly create my object in a QList. But maybe your solution is better. I'll try it. Thanks



  • Now, i got another pb : because we are in a for loop, no layout is showed at the end. Do you have a solution ?

    EDIT : In fact, there was no pb : i just forget to copy the code in my overloaded constructor


Log in to reply
 

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