Widget copy problem
-
[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()));@ -
This "link":http://qt-project.org/doc/qt-4.8/object.html#identity-vs-value can help understanding why.
-
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 ?
-
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);
}
@ -
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);
}
...
@