Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Widget copy problem
Forum Updated to NodeBB v4.3 + New Features

Widget copy problem

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dbzhang800
    wrote on last edited by
    #3

    [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()));@

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xentrum
      wrote on last edited by
      #4

      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

      1 Reply Last reply
      0
      • EddyE Offline
        EddyE Offline
        Eddy
        wrote on last edited by
        #5

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

        Qt Certified Specialist
        www.edalsolutions.be

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #6

          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.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xentrum
            wrote on last edited by
            #7

            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 ?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #8

              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.)

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xentrum
                wrote on last edited by
                #9

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #10

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

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    Xentrum
                    wrote on last edited by
                    #11

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

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      Xentrum
                      wrote on last edited by
                      #12

                      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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved